5

As there are two methods for getting size. From API level above 13 this method works for me

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

And below 14 this method works for me

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();  // deprecated
int height = display.getHeight();  // deprecated

Can i get screen size for all API levels??? Kindly help me.

Abhinav
  • 8,028
  • 12
  • 48
  • 89
  • Just use those two approaches depending on what API the device has. [This question](http://stackoverflow.com/questions/3993924/get-android-api-level-of-phone-currently-running-my-application) will show you how to do so. – MalaKa Apr 07 '14 at 11:24

6 Answers6

1

try with this dude

int density;
    private DisplayMetrics metrics;
    private int widthPixels;
    private float scaleFactor;
    private float widthDp;
private DisplayMetrics metrics;
          metrics = new DisplayMetrics();
     getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);

     density= getResources().getDisplayMetrics().densityDpi;

     widthPixels = metrics.widthPixels;

     scaleFactor = metrics.density;

     widthDp = widthPixels / scaleFactor;
Bhanu Sharma
  • 5,135
  • 2
  • 24
  • 49
0

To support all version of android use below code

int Measuredwidth = 0;
int Measuredheight = 0;
Point size = new Point();
WindowManager w = getWindowManager();

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2){
    w.getDefaultDisplay().getSize(size);
    Measuredwidth = size.x;
    Measuredheight = size.y; 
}else{
    Display d = w.getDefaultDisplay(); 
    Measuredwidth = d.getWidth(); 
    Measuredheight = d.getHeight(); 
}
John
  • 837
  • 3
  • 14
  • 34
0
public void ScreenSize()
{
 int Measuredwidth = 0;
    int Measuredheight = 0;
    Point size = new Point();
    WindowManager w = getWindowManager();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2)
    {
        w.getDefaultDisplay().getSize(size);

        Measuredwidth = size.x;
        Measuredheight = size.y;
    }
    else
    {
        Display d = w.getDefaultDisplay();
        Measuredwidth = d.getWidth();
        Measuredheight = d.getHeight();
    }
}
codeRider
  • 685
  • 1
  • 5
  • 14
0

Try this code, i use to display the screen specification:

this example tested on API 8 and you can easy understand Display Matrix ... hope this helps

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN );
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);


    TextView tv=(TextView) findViewById(R.id.tv);
    TextView tv1=(TextView) findViewById(R.id.TextView01);
    TextView tv2=(TextView) findViewById(R.id.TextView02);
    TextView tv3=(TextView) findViewById(R.id.TextView03);
    TextView tv4=(TextView) findViewById(R.id.TextView04);    
    TextView tv5=(TextView) findViewById(R.id.TextView05);
    TextView tv6=(TextView) findViewById(R.id.TextView06);
    TextView tv7=(TextView) findViewById(R.id.TextView07);
    TextView tv8=(TextView) findViewById(R.id.TextView08);
    TextView tv9=(TextView) findViewById(R.id.TextView09);
    TextView tv10=(TextView) findViewById(R.id.TextView10);
    TextView tv11=(TextView) findViewById(R.id.TextView11);
    TextView tv12=(TextView) findViewById(R.id.TextView12);


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

        float height=metrics.heightPixels/metrics.ydpi;
        float width=metrics.widthPixels/metrics.xdpi;

        tv.setText("The screen size is:"+Math.sqrt(height*height+width*width));

        tv1.setText("The screen height:"+height);
        tv2.setText("The screen width:"+metrics.densityDpi);
        tv3.setText("The screen metrics.heightPixels:"+metrics.heightPixels);
        tv4.setText("The screen metrics.xdpi:"+metrics.xdpi);

        tv5.setText("The screen height:"+height);
        tv6.setText("The screen width:"+metrics);
        tv7.setText("The screen metrics.widthPixels:"+metrics.widthPixels);
        tv8.setText("The screen metrics.ydpi:"+metrics.ydpi);

       tv9.setText("The screen Math.pow  x :"+Math.pow(metrics.widthPixels/metrics.xdpi,2));
       tv10.setText("The screen Math.pow  y :"+Math.pow(metrics.heightPixels/metrics.ydpi,2));
                float x = (float) Math.pow(metrics.widthPixels/metrics.xdpi,2);
                float y = (float) Math.pow(metrics.heightPixels/metrics.ydpi,2);
       tv11.setText("The screen metrics.widthPixels:"+Math.sqrt(x+y));

}
}

activity_main.xml

<RelativeLayout 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" >

<TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<TextView
    android:id="@+id/TextView01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
   android:layout_alignParentLeft="true"
    android:layout_below="@+id/tv"
    android:layout_marginTop="20dp"
    android:text="@string/hello_world" />

<TextView
    android:id="@+id/TextView02"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
 android:layout_alignParentLeft="true"
    android:layout_below="@+id/TextView01"
    android:text="@string/hello_world" />

<TextView
    android:id="@+id/TextView03"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
    android:layout_below="@+id/TextView02"
    android:text="@string/hello_world" />

<TextView
    android:id="@+id/TextView04"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
   android:layout_alignParentLeft="true"
    android:layout_below="@+id/TextView03"
    android:text="@string/hello_world" />

<TextView
    android:id="@+id/TextView05"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/TextView04"
    android:layout_marginTop="40dp"
    android:text="@string/hello_world" />

<TextView
    android:id="@+id/TextView06"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/TextView05"
    android:text="@string/hello_world" />

<TextView
    android:id="@+id/TextView07"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/TextView06"
    android:text="@string/hello_world" />

<TextView
    android:id="@+id/TextView08"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/TextView07"
    android:text="@string/hello_world" />

<TextView
    android:id="@+id/TextView09"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/TextView08"
    android:layout_marginTop="63dp"
    android:text="@string/hello_world" />

<TextView
    android:id="@+id/TextView10"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/TextView09"
    android:text="@string/hello_world" />

<TextView
    android:id="@+id/TextView11"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/TextView10"
    android:text="@string/hello_world" />

<TextView
    android:id="@+id/TextView12"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/TextView11"
    android:text="@string/hello_world" />

 <ImageView
     android:id="@+id/imageView1"
     android:layout_width="10dp"
     android:layout_height="158.77dp"
     android:layout_alignParentRight="true"
     android:layout_alignParentTop="true"
     android:layout_toRightOf="@+id/tv"
     android:adjustViewBounds="true"
     android:scaleType="fitXY"
     android:src="@drawable/inch1" />

  </RelativeLayout>
Elias Sh.
  • 510
  • 6
  • 11
0
Disply disply=((WindowManager)getSystemService(Context.WINDOWMANAGER)).getDefaultDiaply();
int width=display.getWidth();
int heighte=disply.getHeight();
Knut Herrmann
  • 30,880
  • 4
  • 31
  • 67
0

Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)) .getDefaultDisplay();

    int w = display.getWidth();
    int h = display.getHeight();

this code work for me try it.