Android Programming Basics Get phone screen size (displaymetrics) sample _android

Source: Internet
Author: User
Tags sqlite database

This article illustrates how Android gets the size of a mobile screen. Share to everyone for your reference, specific as follows:

There are mainly three objects TextView, buttons, and Displaymetrics, where Displaymetrics is the key class to get the size of the phone screen , this example is very simple when we click on the button to trigger the event, Display the wide and high resolution of the mobile screen in TextView.

Look at the effect chart:

Before the button is fired:

After the button is triggered:

Of these we added two lines to the Res->layout->values->string.xml as follows:

<string name= "Resolution" > Mobile resolution for:</string>
<string name= "Pressme" > Press my resolution </string>

The specific code is as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
  <string name= "Hello" >hello world, displaymetricsdemo!</string>
  <string name= "app_name" >DisplayMetricsDemo</string>
  < String Name= "Resolution" > Mobile resolution for:</string>
  <string name= "Pressme" > Press my resolution </string>
</resources>

The layout file Main.xml code 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/textview1" android:layout_width= "Fill_"
  Parent "
  android:layout_height=" wrap_content "
  android:text=" @string/resolution "
  />
< Button
  android:id= "@+id/button1"
  android:layout_width= "wrap_content"
  android:layout_height= " Wrap_content "
  android:text=" @string/pressme "
/>
</LinearLayout>

Finally, our main class Displaymetricsdemo.java, the code is as follows:

Package com.android.test;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.util.DisplayMetrics;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.TextView;
  public class Displaymetricsdemo extends activity {private TextView textview1;
  Private Button button1;
  Get the mobile screen resolution class private displaymetrics DM;
    public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main);
    Get the layout of Textview,button to like Textview1 = (TextView) Findviewbyid (R.ID.TEXTVIEW1);
    Button1 = (Button) Findviewbyid (R.id.button1);
        Add Button Event Response Button1.setonclicklistener (new Button.onclicklistener () {public void OnClick (View v) {
        DM = new Displaymetrics ();
        Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);
        Access to the mobile phone's broadband and height pixel units for px String str = "Mobile screen resolution:" + Dm.widthpixels + "*" +dm.heightpixels;
      Textview1.settext (str);
  }
    });}
}

 

This example is relatively simple, the core is the onclick inside a few lines of code

More interested readers of Android-related content can view the site: Android Development Primer and Advanced tutorials, the Android View View tips Summary, the activity tips summary of Android programming, Android Operation SQLite Database skills Summary, "Android operation JSON format Data Skills summary", "Android Database Operation skills Summary", "Android File Operation skills Summary", "Android programming development of SD card operation method Summary", " Android Resource Operation tips Summary and the "Android Controls usage Summary"

I hope this article will help you with the Android program.

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.