Wednesday, 27 March 2013

How to get Screen Resolution in Android

Today, We are going to see how we can get device display screen resolution. Each mobile device in the market comes with different size and resolution. There will be situation in your project where you need to know the device display screen resolution. Let's demonstrate this by providing a example

We will be displaying a "Get Screen Resolution" Button, by clicking on that button we will be displaying the screen width and height information

Create a new android application project from eclipse with these below settings

Application Name: DisplayScreen
Project Name: DisplayScreen
Package Name: com.example.displayscreen
Activity Name: ScreenActivity
Target SDK: Android 4.2 (Choose a SDK that suits your test devices and favored emulator configuration)

In Package Explorer, create activity_screen.xml file under res->layout folder and paste the below xml code.

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="get Screen Resolution" />
<TextView
    android:id="@+id/hello"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Your Screen Resolution">
</TextView>
<TextView
    android:id="@+id/width"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</TextView>
<TextView
    android:id="@+id/height"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</TextView>

</LinearLayout>

In Package Explorer, Create ScreenActivity.java file under src->com.example.displayscreen folder and paste the below code
package com.example.displayscreen;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Display;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ScreenActivity extends Activity {

  @Override

  protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_screen);
    Button button = (Button) findViewById(R.id.button1);

    button.setOnClickListener(new OnClickListener() {

    @Override

    public void onClick(View arg0) {

    // Perform action on click

    getScreenResolution();

    }

    });      

  }

  @Override

  public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if present
    getMenuInflater().inflate(R.menu.main, menu);

    return true;

  }

  @SuppressLint("NewApi")

  public void getScreenResolution(){
    Display display = getWindowManager().getDefaultDisplay();

    Point size = new Point();

    display.getSize(size);

    TextView widthView = (TextView) findViewById(R.id.width);

    TextView heightView = (TextView) findViewById(R.id.height);

    widthView.setText("Width: "+size.x);

    heightView.setText("Height: "+size.y);

  }
}

In AndroidManifest.xml, Place the below activity XML code inside <application>

<activity
            android:name="com.example.displayscreen.ScreenActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
</activity>

Thats it, Now you can run your application, by right clicking Package Name, then select Run As->Android Application. Based on my Android Emulator Configuration, i am getting the below Output.


Hope, you enjoyed this Post.

No comments:

Post a Comment