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.