Open In App

How to Get Screen Width and Height in Android?

Last Updated : 20 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Android applications are being developed for different device orientations to support a huge range of devices. So that users with different device size configurations can use the application. Many applications need to get the height and width of the device screen to create UI. In this article, we will take a look at How to get Screen Width and Height in Android. 

Note: This Android article covers in both Java and Kotlin languages. 

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.

Step 2: Working with the activity_main.xml file

Navigate to app > res > layout > activity_main.xml and add the below code to it. Comments are added in the code to get to know in detail. 

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
 
    <!--on below line we are creating
        a text for heading of our app-->
    <TextView
        android:id="@+id/idTVHeading"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/idTVMsg"
        android:layout_margin="20dp"
        android:gravity="center"
        android:padding="4dp"
        android:text="Screen Width and Height for Android Device"
        android:textAlignment="center"
        android:textColor="@color/purple_200"
        android:textSize="20sp"
        android:textStyle="bold" />
 
    <!--on below line we are creating a text view
        for displaying screen height and width-->
    <TextView
        android:id="@+id/idTVMsg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:textAlignment="center"
        android:textAllCaps="false"
        android:textColor="@color/black"
        android:textSize="18sp"
        android:textStyle="bold" />
 
</RelativeLayout>


Step 3: Working with the MainActivity file 

Navigate to app > java > your app’s package name > MainActivity file and add the below code to it. Comments are added in the code to get to know in detail. 

Kotlin




package com.gtappdevelopers.kotlingfgproject
 
import android.os.Bundle
import android.util.DisplayMetrics
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
 
class MainActivity : AppCompatActivity() {
 
    // on below line we are creating
    // variable for our view.
    lateinit var msgTV: TextView
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
 
        // on below line we are initializing
        // our variable with id.
        msgTV = findViewById(R.id.idTVMsg)
 
        // on below line we are creating and
        // initializing variable for display metrics.
        val displayMetrics = DisplayMetrics()
 
        // on below line we are getting metrics
        // for display using window manager.
        windowManager.defaultDisplay.getMetrics(displayMetrics)
 
        // on below line we are getting height
        // and width using display metrics.
        val height = displayMetrics.heightPixels
        val width = displayMetrics.widthPixels
 
        // on below line we are setting height and width to our text view.
        msgTV.text = "Width : " + width.toString() + "\n" + "Height : " + height.toString()
 
    }
}


Java




package com.gtappdevelopers.kotlingfgproject;
 
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.widget.TextView;
 
import androidx.appcompat.app.AppCompatActivity;
 
public class MainActivity extends AppCompatActivity {
 
    // on below line we are creating a variable.
    private TextView msgTV;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         
        // on below line we are initializing
        // variables with ids.
        msgTV = findViewById(R.id.idTVMsg);
 
        // on below line we are creating and initializing
        // variable for display metrics.
        DisplayMetrics displayMetrics = new DisplayMetrics();
 
        // on below line we are getting metrics for display using window manager.
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
 
        // on below line we are getting height
        // and width using display metrics.
        int height = displayMetrics.heightPixels;
        int width = displayMetrics.widthPixels;
 
        // on below line we are setting height and width to our text view.
        msgTV.setText("Width : " + String.valueOf(width) + "\n" + "Height : " + String.valueOf(height));
 
    }
}


Now run your application to see the output of it. 

Output:

Output

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads