-->

Get display information using DisplayMetrics, in Multi-Window Mode

Advertisemen

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>


Advertisemen

Disclaimer: Gambar, artikel ataupun video yang ada di web ini terkadang berasal dari berbagai sumber media lain. Hak Cipta sepenuhnya dipegang oleh sumber tersebut. Jika ada masalah terkait hal ini, Anda dapat menghubungi kami disini.
Related Posts
Disqus Comments
© Copyright 2017 GENERAL INFO FOR ANDROID DEVELOPMENT - All Rights Reserved - Template Created by goomsite - Proudly powered by Blogger