Feb 4, 2011

Get screen resolution of Android device

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

Get screen resolution of Android device

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>


2 comments:

  1. the code is quite useful. can you please help me getting the coordinates of any arbitrary shape like trapezium.

    ReplyDelete
  2. a really useful snippet of code!!. . .thanks alot!!

    ReplyDelete

Infolinks In Text Ads