Pages

Tuesday, April 15, 2014

How to get screen height, width and density of the android device?


Hello Friends, in this post, I write code for getting Screen Density and Screen DensityDPI, width and height of the android device using DisplayMetrics

DisplayMetrics
A structure describing general information about a display, such as its size, density, and font scaling.
To access the DisplayMetrics members, initialize an object like this:

 DisplayMetrics metrics = new DisplayMetrics();
 getWindowManager().getDefaultDisplay().getMetrics(metrics);

density
The logical density of the display. This is a scaling factor for the Density Independent Pixel unit, where one DIP is one pixel on an approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen), providing the baseline of the system's display. Thus on a 160dpi screen this density value will be 1; on a 120 dpi screen it would be .75; etc.

This value does not exactly follow the real screen size (as given by xdpi and ydpi, but rather is used to scale the size of the overall UI in steps based on gross changes in the display dpi. For example, a 240x320 screen will have a density of 1 even if its width is 1.8", 1.3", etc. However, if the screen resolution is increased to 320x480 but the screen size remained 1.5"x2" then the density would be increased (probably to 1.5).

densityDpi
The screen density expressed as dots-per-inch. May be either DENSITY_LOW, DENSITY_MEDIUM, or DENSITY_HIGH.

scaledDensity
A scaling factor for fonts displayed on the display. This is the same as density, except that it may be adjusted in smaller increments at runtime based on a user preference for the font size.

heightPixels
The absolute height of the display in pixels.

widthPixels
The absolute width of the display in pixels.


1. MainActivity.java Class

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.widget.TextView;

public class MainActivity extends Activity 
{
 @Override
 protected void onCreate(Bundle savedInstanceState) 
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  TextView txta = (TextView) findViewById(R.id.textView);

  DisplayMetrics metrics = new DisplayMetrics();
  getWindowManager().getDefaultDisplay().getMetrics(metrics);
  float screenDensity = metrics.density;
  int screenDensityDPI = metrics.densityDpi;
  float screenscaledDensity = metrics.scaledDensity;
  int width = metrics.widthPixels;
  int Height = metrics.heightPixels;

  txta.setText("Screen Density=" + screenDensity + "\n"
    + "Screen DensityDPI=" + screenDensityDPI + "\n"
    + "Screen Scaled DensityDPI=" + screenscaledDensity + "\n"
    + "Height="+ Height + "\n"
    + "Width=" + width);
 }
}


2. activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="30dp" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="14sp"
        android:textStyle="bold" />

</LinearLayout>


3. Screenshot(HTC Desire X)





1 comment: