Android get screen size

If you create an Android application that needs to know the screen dimension before doing something, this tip is helpful to you. For example, if you want to draw an image to fit the device's screen, you must know the width and height of the screen.

Android stores information about screen size and density in a Display object. You can get the Display object by using the getDefaultDisplay() method of the WindowManager class. The Display object has a method called getMetrics() that returns display metrics object. From the display metrics object, you can access size and density of the device's screen.



Now to have a workable example application to display the size of the screen of a device, you need to create a new Androject project. Then modify the activity_main.xml file to add a TextView as shown below. The TextView is used to display the size of the screen.

<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"
   tools:context=".MainActivity"
   android:orientation="vertical"
   >
   <TextView
      android:id="@+id/txtview"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:textSize="20sp"      
      
      />
</LinearLayout>


Then open the MainActiviy.java file. In the onCreate() method, you write code to access the size of the screen as shown below:

package com.example.androidexample;

import android.os.Bundle;
import android.app.Activity;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Menu;
import android.view.WindowManager;
import android.widget.TextView;

public class MainActivity extends Activity {

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv=(TextView)findViewById(R.id.txtview);
//create display metrics object
DisplayMetrics metrics=new DisplayMetrics();
//get the window manager for the current activity
WindowManager wm=this.getWindowManager();
//get the default display used by WindowManager
Display display=wm.getDefaultDisplay();
//Gets display metrics that describe the size and density of this display.
display.getMetrics(metrics);
//get screen size (width and height)
int screen_width=metrics.widthPixels;
int screen_height=metrics.heightPixels;
//display the width and height on the TextView
tv.setText("Screen dimension: "+screen_width+"x"+screen_height);

}



public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

Android-get-screen-size

Posted by: Dara | post date: 09-03-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.