How to get mobile screen size in Android _android

Source: Internet
Author: User

This article for everyone to explain how to get the mobile phone screen size, provide a solution, share for everyone to reference, the specific content as follows

Run Effect chart:

After running the program, when we click the button, we can see the following effect chart:

Specific code:

We can get the resolution size of the phone screen by using class Displaymetrics. The Displaymetrics class is a key class for getting a variety of properties on a mobile screen. Here's an example to show how to get the resolution of the phone screen.
Adds a TextView object, a button object, to the layout file main.xml. The TextView object is used to display the obtained resolution value, and the button object is to obtain the resolution when clicked. The code for Main.xml is as follows:

<?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:id= "@+id/tv" android:layout_width= "Fill_parent"  
   android:layout_height= "Wrap_content"  
   android:text= Mobile Resolution: "/> <button android:id= 
    " @+ Id/btnok " 
    android:layout_width=" fill_parent " 
    android:layout_height=" wrap_content " 
    android:text=" "Get mobile resolution"/> 
</LinearLayout> 

The code in Testactivity is as follows:

public class Testactivity extends activity { 
     
  private TextView TV; 
  Private Button btn; 
   
  Get the mobile screen resolution class 
   private displaymetrics DM; 
   
  public void OnCreate (Bundle savedinstancestate) { 
    super.oncreate (savedinstancestate); 
    Setcontentview (r.layout.main);  
     
    TV = (TextView) Findviewbyid (r.id.tv); 
    BTN = (Button) Findviewbyid (R.id.btnok); 
    Btn.setonclicklistener (New View.onclicklistener () {public 
       
     void OnClick (View v) { 
       dm = new Displaymetrics (); C13/>getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM); 
         Get cell phone width and height pixel units for px 
     String strpm = "Mobile screen resolution is:" + dm.widthpixels+ "*" +dm.heightpixels; 
     Tv.settext (strpm);}} 
    ); 
  } 
  
 

I hope this article will help you learn about Android software programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.