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