Get the size of the screen.

Quickly share your Android Code Snippets here...

Get the size of the screen.

Postby MrSnowflake » Thu Oct 16, 2008 9:01 pm

Get the size of the screen, called from an Activity:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. DisplayMetrics dm = new DisplayMetrics();
  3.  
  4. getWindowManager().getDefaultDisplay().getMetrics(dm);
  5.  
  6. int height = dm.heightPixels;
  7.  
  8. int width = dm.widthPixels;
  9.  
  10.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
Last edited by MrSnowflake on Fri Oct 17, 2008 11:37 am, edited 1 time in total.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Top

Postby Danuubz » Fri Oct 17, 2008 2:12 am

Here's another way:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package de.android.screentest;
  3.  
  4.  
  5.  
  6. import de.android.screentest.R;
  7.  
  8. import android.app.Activity;
  9.  
  10. import android.os.Bundle;
  11.  
  12. import android.widget.LinearLayout;
  13.  
  14. import android.graphics.Color;
  15.  
  16. import android.graphics.drawable.Drawable;
  17.  
  18. import android.widget.TextView;
  19.  
  20. import android.view.Display;
  21.  
  22.  
  23.  
  24. import android.view.WindowManager;
  25.  
  26.  
  27.  
  28. public class ScreenTest extends Activity
  29.  
  30. {    
  31.  
  32.         private LinearLayout main;
  33.  
  34.         private TextView tv;
  35.  
  36.        
  37.  
  38.         Display display;
  39.  
  40.        
  41.  
  42.         String txt;
  43.  
  44.        
  45.  
  46.     @Override
  47.  
  48.     public void onCreate(Bundle savedInstanceState)
  49.  
  50.     {
  51.  
  52.         super.onCreate(savedInstanceState);
  53.  
  54.        
  55.  
  56.         main = new LinearLayout(this);
  57.  
  58.         main.setBackgroundColor(Color.BLACK);
  59.  
  60.         main.setOrientation(LinearLayout.VERTICAL);
  61.  
  62.        
  63.  
  64.         tv = new TextView(this);
  65.  
  66.         tv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
  67.  
  68.         tv.setText(getScreenProps());
  69.  
  70.         main.addView(tv);
  71.  
  72.        
  73.  
  74.         setContentView(main);
  75.  
  76.     }
  77.  
  78.    
  79.  
  80.     private String getScreenProps()
  81.  
  82.     {
  83.  
  84.         String s = "screen property: ";
  85.  
  86.        
  87.  
  88.         display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
  89.  
  90.        
  91.  
  92.         int height = display.getHeight();
  93.  
  94.         int width = display.getWidth();
  95.  
  96.        
  97.  
  98.         s = s + width + " " + height;
  99.  
  100.        
  101.  
  102.         return s;
  103.  
  104.     }
  105.  
  106.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
User avatar
Danuubz
Experienced Developer
Experienced Developer
 
Posts: 78
Joined: Wed Dec 19, 2007 10:44 pm
Location: Germany

Re: Get the size of the screen.

Postby plusminus » Fri Oct 17, 2008 5:08 am

MrSnowflake wrote:Get the size of the screen:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. DisplayMetrics dm = new DisplayMetrics();
  2. mParent.getWindowManager().getDefaultDisplay().getMetrics(dm);
  3. int height = dm.heightPixels;
  4. int width = dm.widthPixels;
  5.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Could you specify what [font=Courier New]mParent[/font] (probably the Activity?)

I usually do it with the WindowManager-Service.
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby Danuubz » Fri Oct 17, 2008 6:35 am

Could you specify what mParent (probably the Activity?)


WindowManager getWindowManager() is also part of android.view.Window
User avatar
Danuubz
Experienced Developer
Experienced Developer
 
Posts: 78
Joined: Wed Dec 19, 2007 10:44 pm
Location: Germany

Re: Get the size of the screen.

Postby MrSnowflake » Fri Oct 17, 2008 11:37 am

plusminus wrote:
MrSnowflake wrote:Get the size of the screen:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. DisplayMetrics dm = new DisplayMetrics();
  2. mParent.getWindowManager().getDefaultDisplay().getMetrics(dm);
  3. int height = dm.heightPixels;
  4. int width = dm.widthPixels;
  5.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Could you specify what [font=Courier New]mParent[/font] (probably the Activity?)

I usually do it with the WindowManager-Service.
Woops that was a direct copy-paste from my code. I'll edit first post.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby Timaphei » Tue Feb 09, 2010 12:33 pm

Hi guys,
I used all above suggestions of how to determine the size of the screen.
This is what I get:
on Motorola Droid: 569x320 (actual size is 854x480)
on Nexus One: 533x320 (actual size is 800x480)

What is wrong?
Timaphei
Freshman
Freshman
 
Posts: 8
Joined: Sat Jul 18, 2009 2:51 pm

Top

Postby wizlon » Tue Feb 23, 2010 1:50 pm

The problem is that in SDK 1.5 the screensize was a bit knackered. You can either target your build for 1.6 and above or do what is suggested here:

http://stackoverflow.com/questions/2013 ... ersion-1-5

hope that helps.
wizlon
Junior Developer
Junior Developer
 
Posts: 23
Joined: Mon Jan 07, 2008 2:17 pm

Re:

Postby freewheelnat » Tue Nov 09, 2010 6:21 pm

Timaphei wrote:Hi guys,
I used all above suggestions of how to determine the size of the screen.
This is what I get:
on Motorola Droid: 569x320 (actual size is 854x480)
on Nexus One: 533x320 (actual size is 800x480)

What is wrong?


These values are correct. The methods to determine the screen size gives the sizes in dip. To get the pixels (sizes quoted by manufacturers), you need to do the operation below.

pixels = dips * (density / 160)

The 2 phones have a density of 240 so, if you take the height of the Motorola for example, your height in pixels is going to be 569 * 240 / 160 = 854.

Refer to Supporting Multiple Screens - scroll down to Overview of Screen support/terms and concepts and read the "density-independent pixel (dip)" paragraph for info.

This is slightly confusing but it stems back from when Google Android only supported 160 density screens.
freewheelnat
Developer
Developer
 
Posts: 40
Joined: Sat Aug 08, 2009 11:48 am
Location: London

Top

Return to Code Snippets for Android

Who is online

Users browsing this forum: No registered users and 2 guests