Monday, July 16, 2012

Get screen resolution of Android device

DisplayMetrics ia a structure describing general information about a display, such as its size, density, and font scaling.



package com.GetScreenResolution;
 
import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.widget.TextView;
 
public class GetScreenResolution extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView screenWidth = (TextView)findViewById(R.id.screenwidth);
        TextView screenHeight = (TextView)findViewById(R.id.screenheight);
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
         
        screenWidth.setText("Screen Width = " + dm.widthPixels);
        screenHeight.setText("Screen Height = " + dm.heightPixels);
    }
}



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<TextView 
    android:id="@+id/screenwidth"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
<TextView 
    android:id="@+id/screenheight"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
</LinearLayout>

No comments:

Post a Comment