Tuesday, July 26, 2016

onConfigurationChanged() called when window size changed in Multi-Window Mode

Last post show that onMultiWindowModeChanged() will be called when your app change from Full screen mode to Multi-Window Mode, or reverse. How about size changed in Multi-Window Mode?


This example show that onConfigurationChanged() will be called in case of size changed in Multi-Window Mode.


Modify MainActivity to override onConfigurationChanged() to show a Toast.
package com.blogspot.android_er.androidmultiwindow;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    TextView textPrompt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textPrompt = (TextView)findViewById(R.id.prompt);

        if(isInMultiWindowMode()){
            textPrompt.setText("onCreate run In Multi Window Mode ");
        }else{
            textPrompt.setText("onCreate run NOT In Multi Window Mode ");
        }

        Toast.makeText(MainActivity.this,
                "onCreate() called", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onMultiWindowModeChanged(boolean isInMultiWindowMode) {
        super.onMultiWindowModeChanged(isInMultiWindowMode);

        if(isInMultiWindowMode){
            textPrompt.setText("It is In Multi Window Mode ");
        }else{
            textPrompt.setText("It is NOT In Multi Window Mode ");
        }

        Toast.makeText(MainActivity.this,
                "onMultiWindowModeChanged() called", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        Toast.makeText(MainActivity.this,
                "onConfigurationChanged() called", Toast.LENGTH_LONG).show();
    }
}


The layout file keep using that in last post.

The AndroidManifest.xml in this example use the default setting by Android Studio without any changed.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.blogspot.android_er.androidmultiwindow">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Next:
Get display information using DisplayMetrics, in Multi-Window Mode

No comments: