English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Android Programming Basics: Example of Getting Mobile Screen Size (DisplayMetrics Application)

This article describes the method of Android to get the size of the phone screen. Share it with everyone for reference, as follows:

Here, mainly three objects TextView, Button, and DisplayMetrics are used. DisplayMetrics is the key class to get the size of the phone screen. This example is very simple. When we click the button, trigger the event, and display the width and height resolution of the phone screen in the TextView.

Let's take a look at the effect diagram:

Before the button is triggered:

After the button is triggered:

Among them, we have used three objects TextView, Button, and DisplayMetrics. DisplayMetrics is the key class to get the size of the phone screen. This example is very simple. When we click the button, trigger the event, and display the width and height resolution of the phone screen in the TextView.->layout->values->string.xml has been added with two lines as follows:

<string name="resolution">The screen resolution of the phone is:</string>
<string name="pressme">Press me to get the 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">The screen resolution of the phone is:</string>
  <string name="pressme">Press me to get the resolution</string>
</resources>

Here is the layout file main.xml code 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, here is 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;
  //class to get the screen resolution of the phone
  private DisplayMetrics dm;
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //get TextView and Button objects in the layout
    textview1 = (TextView)findViewById(R.id.textview1);
    button1 = (Button)findViewById(R.id.button1);
    //increase button event response
    button1.setOnClickListener(new Button.OnClickListener() {
      public void onClick(View v)
      public void onClick(View v)
        {
        dm = new DisplayMetrics();
        //getWindowManager().getDefaultDisplay().getMetrics(dm);
        Get the width and height of the phone screen in pixels (px) + String str = "手机屏幕分辨率为:"
        +" * "+dm.heightPixels;
        textview1.setText(str);
      }
    });
  }
}

This example is relatively simple, the core is just a few lines of code in onClick

Readers who are interested in more about Android-related content can check out the special topics on this site: 'Android Development入门与进阶教程', 'Android视图View技巧总结', 'Android编程之activity操作技巧总结', 'Android操作SQLite数据库技巧总结', 'Android操作json格式数据技巧总结', 'Android数据库操作技巧总结', 'Android文件操作技巧汇总', 'Android编程开发之SD卡操作方法汇总', 'Android资源操作技巧汇总' and 'Android控件用法总结'

I hope this article will be helpful to everyone in Android program design.

Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously, and this website does not own the copyright, has not been edited by humans, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)

You May Also Like