Get Display Data Using Displaymetrics, Inwards Multi-Window Mode

Posted on

The flat android.util.DisplayMetrics is a construction describing full general data nearly a display, such equally its size, density, too font scaling. Here demo how to become display data (specially heightPixels and widthPixels) inwards 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;  world flat 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 live In Multi Window Mode ");         }else{             textPrompt.setText("onCreate live NOT In Multi Window Mode ");         }          Toast.makeText(MainActivity.this,                 "onCreate() called", Toast.LENGTH_LONG).show();          showDisplayInfo();     }      @Override     world 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     world void onConfigurationChanged(Configuration newConfig) {         super.onConfigurationChanged(newConfig);          Toast.makeText(MainActivity.this,                 "onConfigurationChanged() called", Toast.LENGTH_LONG).show();          showDisplayInfo();     }      private void showDisplayInfo(){         DisplayMetrics dm = novel DisplayMetrics();         getWindowManager().getDefaultDisplay().getMetrics(dm);          String strScreenDIP = "";         strScreenDIP += "The logical density of the display: " + dm.density + "\n";         strScreenDIP += "The covert density expressed equally dots-per-inch: " + dm.densityDpi +"\n";         strScreenDIP += "The absolute tiptop of the display inwards pixels: " + dm.heightPixels +"\n";         strScreenDIP += "The absolute width of the display inwards pixels: " + dm.widthPixels + "\n";         strScreenDIP += "A scaling element for fonts displayed on the display: "                  + dm.scaledDensity + "\n";         strScreenDIP += "The exact physical pixels per inch of the covert inwards the X dimension: "                  + dm.xdpi + "\n";         strScreenDIP += "The exact physical pixels per inch of the covert inwards 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="https://rootandroid.org//"         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>