Open In App

How to Get Screen Dimensions as Pixel in Android?

Last Updated : 31 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Many times while building an android application we want to place elements dynamically within our android application. We can place these elements by specifying their position relative to the screen dimensions within the android application. For this, we will need to get device screen dimensions. In this article, we will take a look at How to get screen dimensions as pixel in Android. 

Note: This Android article covered 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/idRLContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
  
    <!--displaying a simple text view-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/idTVDimensions"
        android:layout_margin="15dp"
        android:text="Screen Dimensions in Android"
        android:textAlignment="center"
        android:textColor="@color/black"
        android:textSize="20sp"
        android:textStyle="bold" />
  
    <!--on below line creating 
        textview for displaying id-->
    <TextView
        android:id="@+id/idTVDimensions"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:padding="4dp"
        android:textAlignment="center"
        android:textSize="20sp"
        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.graphics.Point
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
  
class MainActivity : AppCompatActivity() {
  
    // on below line we are creating variables.
    lateinit var dimensionsTV: TextView
  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
          
        // on below line we are initializing our variables.
        dimensionsTV = findViewById(R.id.idTVDimensions)
          
        // on below line we are getting screen dimensions.
        val display = windowManager.defaultDisplay
        val size = Point()
        display.getSize(size)
        val width: Int = size.x
        val height: Int = size.y
          
        // on below line creating string specifying height and width
        val dimensions = "Width : " + width + " px\n" + "Height : " + height + " px"
          
        // on below line we are setting text 
        // as our dimensions to our text view.
        dimensionsTV.text = dimensions
  
    }
}


Java




package com.gtappdevelopers.googlemapsroutes;
  
import android.graphics.Point;
import android.os.Bundle;
import android.view.Display;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
  
    // on below line we are creating variables.
    private TextView dimensionsTV;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          
        // on below line we are initializing our variables.
        dimensionsTV = findViewById(R.id.idTVDimensions);
         
        // on below line we are getting screen simensions
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int width = size.x;
        int height = size.y;
        String dimensions = "Width : " + width + " px\n" + "Height : " + height + " px";
          
        // on below line setting dimensions
        // to our text view.
        dimensionsTV.setText(dimensions);
    }
}


Now run your project to see the output of your application. 

Output:

Get Screen Dimensions as Pixel in Android Output

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads