All CategoriesAndroid App Development

What's New in Android 8.0 Oreo

Android 8.0 (API level 26) version comes with new features and capabilities for users and developers which is very prevailing, smarter and faster in comparison with older versions.
Some of the new and exciting features of Android 8.0 are as follows;

  • Picture-in-Picture Mode
  • Autofill Framework
  • Notification Dots
  • New Emojis and others

Android Oreo starts tasks faster with 2X boot speed during powering up. It also has background limits to minimize the apps.
This article speaks about three main features o, i.e., Picture-in-Picture mode, Autofill framework, Notification dots in detail.

Picture-in-Picture Mode

PIP is a newly added feature which has a lot of benefits. Some the benefits are listed below

  • It avails multi-window support which means the user can see two apps at once.
  • It is mostly used for video playback, i.e. user can simultaneously use an application and watch the video. When the activity is in PIP mode, it hides the UI elements, and video playback is continued.
  •  The PIP window can be dragged wherever required and it adjusts accordingly on the screen
  • To get the PIP support into action, it has to be registered in the manifest file by setting android:supportsPictureInPicture and android:resizeableActivity to true also have to handle the layout configuration changes.

Methods:

enterPictureInPictureMode(): This method is called when user enter into PIP mode. An activity must call this method to enter into PIP mode.
onPictureInPictureModeChanged(): This is an override method it is called when user enters or exits Picture-in-Picture mode.

Notification Dots

Notification Dots also known as Notification Badges. It is one of the unique features in Android 8.0 (API level 26).  It displays dots on app icons on the launcher screen. Notification dots displays notifications which are associated with one or more notification channels in an app.

Instruction to use the Notification Badge:

By long press, on app icons, the user can see the notifications associated with it and then take necessary actions accordingly, like swipe away to dismiss it. The user can adjust the Notification Badges i.e. turn off or on the badge from settings

Methods:

setShowBadge(): This method is used to set the presence of Notification Badges. By setting it to true means the notifications from a channel being reflected by the badge and false means it stops the presence of notifications by the badge.
setNumber(): This method is used to show the count of notifications which is display on app icon by long press in launcher screen.

AutoFill Framework

It is one of the defining features of Android 8.0. This framework manages the communication between the app and autofill service. The user can save their time while filling forms like a credit card, account, etc. by using autofill in their devices.
Autofill framework has many benefits like :

  • Saving users time from re-typing information,
  • Removing errors while typing information on mobile devices.

Autofill feature can be enabled or disabled from settings. Upon user’s permission autofill saves or remembers the logins which increase the speed of app accessibility.
Let’s see how we can do it with the help of an example.

Example of code

Here, we will create an application which implements all the above features, i.e., Picture-in-Picture mode, Autofill framework and Notification Channels.

  • Create an application AndroidOreoUpdateExample in Android Studio with updated version and API level 26.
  • Add the dependencies in build.gradle
  • Register the video activity in manifest file and services for Autofill as I discussed above.

build.gradle:

Add the dependencies in this file.

compile 'com.android.support:support-v4:25.1.1'
compile 'com.android.support:support-v13:25.1.1'
compile 'com.android.support:cardview-v7:25.1.1'
compile 'com.android.support:appcompat-v7:25.1.1'
compile 'com.android.support:design:25.1.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.1'

AndroidManifest.xml:

Register the video activity here and also add service for Autofill.

<activity
    android:name=".PictureInPictureModeActivity"
    android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"
    android:supportsPictureInPicture="true" />
<service
    android:name=".multidatasetservice.MyAutofillService"
    android:label="Multi-Dataset Autofill Service"
    android:permission="android.permission.BIND_AUTOFILL">
    <meta-data
        android:name="android.autofill"
        android:resource="@xml/multidataset_service" />
    <intent-filter>
        <action android:name="android.service.autofill.AutofillService" />
    </intent-filter>
</service>

Now, Create Java and XML files for every feature one by one.

Picture-in-Picture Mode

activity_picture_in_picture_mode.xml:

Create this XML file in your project. Here we add MovieView for video, Button to enter into PIP mode and TextView for an explanation.

Add the code here:

<com.example.shriyanshu.androidoreoupdateexample.MovieView
    android:id="@+id/movie_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:adjustViewBounds="true"
    android:src="@raw/vid_bigbuckbunny"/>
<ScrollView
    android:id="@+id/scroll_view"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">
    <LinearLayout
        android:id="@+id/ll_vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingEnd="@dimen/activity_horizontal_margin"
        android:paddingStart="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin">
        <Button
            android:id="@+id/btn_PIP"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/enter_picture_in_picture"/>
        <TextView
            android:id="@+id/tv_explanation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/activity_vertical_margin"
            android:text="@string/explanation"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1"/>
    </LinearLayout>
</ScrollView>

Create class MovieView.java in your project which implements all the controls, Media Player, etc. to play the video.

PictureinPictureModeActivity.java:

Create this Activity in your project here we add all the methods as discussed above when we enter into PIP mode.

Add the code in onCreate(..){..}:

Take the reference of MovieView, ScrollView and Button also set the listener on them.

mMovieView = (MovieView) findViewById(R.id.movie_view);
mScrollView = (ScrollView) findViewById(R.id.scroll_view);
mMovieView.setMovieListener(mMovieListener);
findViewById(R.id.btn_PIP).setOnClickListener(mOnClickListener);

Now implement the method onPictureInPictureModeChanged() and add the code here.

@Override
public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode) {
    super.onPictureInPictureModeChanged(isInPictureInPictureMode);
    if (isInPictureInPictureMode) {
        mReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent == null || !ACTION_MEDIA_CONTROL.equals(intent.getAction())) {
                    return;
                }
                final int controlType = intent.getIntExtra(EXTRA_CONTROL_TYPE, 0);
                switch (controlType) {
                    case CONTROL_TYPE_PLAY:
                        mMovieView.play();
                        break;
                    case CONTROL_TYPE_PAUSE:
                        mMovieView.pause();
                        break;
                }
            }
        };
        registerReceiver(mReceiver, new IntentFilter(ACTION_MEDIA_CONTROL));
    } else {
        unregisterReceiver(mReceiver);
        mReceiver = null;
        if (mMovieView != null && !mMovieView.isPlaying()) {
            mMovieView.showControls();
        }
    }
}

Add the code when entering into PIP mode.

mMovieView.hideControls();
Rational aspectRatio = new Rational(mMovieView.getWidth(), mMovieView.getHeight());
mPictureInPictureParamsBuilder.setAspectRatio(aspectRatio).build();
enterPictureInPictureMode(mPictureInPictureParamsBuilder.build());

Notification Dots:

activity_notification.xml:

Create this XML file in your project and add the code for both channels primary and secondary.

<GridLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:rowCount="7"
    android:columnCount="2"
    android:orientation="horizontal">
    <EditText
        android:id="@+id/ed_main_primary_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_columnSpan="2"
        android:layout_gravity="fill_horizontal"
        android:hint="@string/main_primary_title"
        android:text="@string/main_primary_title" />
    <Button
        android:id="@+id/btn_main_primary_send1"
        android:layout_columnWeight="1"
        android:layout_height="wrap_content"
        android:text="@string/main_primary_send1" />
    <Button
        android:id="@+id/btn_main_primary_send2"
        android:layout_columnWeight="1"
        android:layout_height="wrap_content"
        android:text="@string/main_primary_send2" />
    <ImageButton
        android:id="@+id/btn_main_primary_config"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_columnSpan="2"
        android:layout_gravity="fill_horizontal"
        android:src="@android:drawable/ic_menu_preferences"
        android:contentDescription="@string/main_primary_config" />
    <Space
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_columnSpan="2"
        android:layout_gravity="fill_horizontal" />
    <EditText
        android:id="@+id/ed_main_secondary_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_columnSpan="2"
        android:layout_gravity="fill_horizontal"
        android:hint="@string/main_secondary_title"
        android:text="@string/main_secondary_title" />
    <Button
        android:id="@+id/btn_main_secondary_send1"
        android:layout_columnWeight="1"
        android:layout_height="wrap_content"
        android:text="@string/main_secondary_send1" />
    <Button
        android:id="@+id/btn_main_secondary_send2"
        android:layout_columnWeight="1"
        android:layout_height="wrap_content"
        android:text="@string/main_secondary_send2"/>
    <ImageButton
        android:id="@+id/btn_main_secondary_config"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_columnSpan="2"
        android:layout_gravity="fill_horizontal"
        android:src="@android:drawable/ic_menu_preferences"
        android:contentDescription="@string/main_secondary_config" />
</GridLayout>
<LinearLayout
    android:id="@+id/main_footer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_alignParentBottom="true">
    <Button
        android:text="@string/btnA_title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/btn_Settings"
        android:layout_weight="1" />
</LinearLayout>

Create the NotificationActivity.java class in your project and set the controls for sending test notifications.
NotificationHelper.java:
Create this helper class in your project which extends ContextWrapper. Here, we will manage notification channels and create notifications also we can set notification dots method as I discussed above.

Add the code here:

NotificationChannel chan1 = new NotificationChannel(PRIMARY_CHANNEL,
        getString(R.string.noti_channel_default), NotificationManager.IMPORTANCE_DEFAULT);
chan1.setLightColor(Color.GREEN);
chan1.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
chan1.setShowBadge(true);
getManager().createNotificationChannel(chan1);
NotificationChannel chan2 = new NotificationChannel(SECONDARY_CHANNEL,
        getString(R.string.noti_channel_second), NotificationManager.IMPORTANCE_HIGH);
chan2.setLightColor(Color.BLUE);
chan2.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
chan2.setShowBadge(true);
getManager().createNotificationChannel(chan2);

Autofill Framework

Here, we implement multiple login forms like a Credit card, Virtual View, Standard Sign In, Email Compose, Standard AutoComplete Sign In, etc. and one login forms would be considered in this example.

StandardAutoCompleteSignInActivity.java:

Create this activity in your project. Here, it implements an inner class of Autofill Callback which extends AutofillManager.AutofillCallback.

Initialize:

private AutofillManager.AutofillCallback mAutofillCallback;
private AutofillManager mAutofillManager;

Add the code in onCreate(..){..}:

mAutofillCallback = new MyAutofillCallback();
mAutofillManager = getSystemService(AutofillManager.class);

Now create the inner class and set other properties.

private class MyAutofillCallback extends AutofillManager.AutofillCallback {
    @Override
    public void onAutofillEvent(View view, int event) {
        super.onAutofillEvent(view, event);
        if (view instanceof AutoCompleteTextView) {
            switch (event) {
                case AutofillManager.AutofillCallback.EVENT_INPUT_UNAVAILABLE:
                                    case AutofillManager.AutofillCallback.EVENT_INPUT_HIDDEN:
                    if (!mAutofillReceived) {
                        ((AutoCompleteTextView) view).showDropDown();
                    }
                    break;
                case AutofillManager.AutofillCallback.EVENT_INPUT_SHOWN:
                    mAutofillReceived = true;
                    ((AutoCompleteTextView) view).setAdapter(null);
                    break;
                default:
                    Log.d(TAG, "Unexpected callback: " + event);
            }
        }
    }
}

Create all the Multi dataset services in your project which are necessary for Autofill framework.

Output:

Home Screen

 

PIP Screen

Enter into PIP mode

PIP Mode

 

Notification Dots

Long Press on App icon

Notification Channels

Various Autofill forms

Autofill

Conclusion:

Thus, we understood the application of the newly added features of Android Oreo which target API level 26. These features consume lesser time to deliver better performance with high speed. The features of PIP mode allows the user to use two apps at once. Autofill save time and fewer errors, Notification dots display the notifications on long press and take actions there either to see it or not by swipe away. Thus Android Oreo comes with some new and useful features like additional and fun emoji’s, 2X speed while booting up, etc. Henceforth it can be concluded that the newly added features are interactive and important in Android development.

Tags

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Articles

Close