Showing posts with label MultiWindowMode. Show all posts
Showing posts with label MultiWindowMode. Show all posts

Thursday, July 28, 2016

Apply Material Theme to Activity


This video show how to apply Android provided Material Theme to your Activity.


- Edit values/styles.xml to add a new style inherits from Android provided Material Theme: android:Theme.Material, android:Theme.Material.Light or android:Theme.Material.Light.DarkActionBar.

- Edit AndroidManifest.xml to use the new style.

- You have to change your MainActivity extends Activity. Otherwise the following error will happen:
You need to use a Theme.AppCompat theme (or descendant) with this activity.

The video also show how it display on Multi-Window Mode.


Next:
- Customize theme's colors

Wednesday, July 27, 2016

Get display information using DisplayMetrics, in Multi-Window Mode


The class android.util.DisplayMetrics is a structure describing general information about a display, such as its size, density, and font scaling. Here show how to get display information (specially heightPixels and widthPixels) in Multi-Window Mode.


package com.blogspot.android_er.androidmultiwindow;

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

public class MainActivity extends AppCompatActivity {

    TextView textPrompt;
    TextView textInfo;

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

        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();

        showDisplayInfo();
    }

    @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();

        showDisplayInfo();
    }

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

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

        showDisplayInfo();
    }

    private void showDisplayInfo(){
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);

        String strScreenDIP = "";
        strScreenDIP += "The logical density of the display: " + dm.density + "\n";
        strScreenDIP += "The screen density expressed as dots-per-inch: " + dm.densityDpi +"\n";
        strScreenDIP += "The absolute height of the display in pixels: " + dm.heightPixels +"\n";
        strScreenDIP += "The absolute width of the display in pixels: " + dm.widthPixels + "\n";
        strScreenDIP += "A scaling factor for fonts displayed on the display: " 
                + dm.scaledDensity + "\n";
        strScreenDIP += "The exact physical pixels per inch of the screen in the X dimension: " 
                + dm.xdpi + "\n";
        strScreenDIP += "The exact physical pixels per inch of the screen in the Y dimension: " 
                + dm.ydpi + "\n";

        textInfo.setText(strScreenDIP);
    }
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:orientation="vertical"
    tools:context="com.blogspot.android_er.androidmultiwindow.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold"/>
    <TextView
        android:id="@+id/prompt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:textStyle="bold"/>
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/info"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="20dp"/>
    </ScrollView>

</LinearLayout>


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

Monday, July 25, 2016

Detect and check if your app run in multi window mode


This example show how to detect and check if your app run in multi window mode, target Android N with Multi-Window Support, by calling isInMultiWindowMode() and override onMultiWindowModeChanged() methods.


package com.blogspot.android_er.androidmultiwindow;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

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 ");
        }
    }

    @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 ");
        }
    }
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:orientation="vertical"
    tools:context="com.blogspot.android_er.androidmultiwindow.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold"/>
    <TextView
        android:id="@+id/prompt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="28dp"
        android:textStyle="bold"/>
</LinearLayout>


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