How to Get Screen Dimensions as Pixel in Android?
Last Updated :
31 Aug, 2022
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
android:id = "@+id/idRLContainer"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:orientation = "vertical"
tools:context = ".MainActivity" >
< 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" />
< 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() {
lateinit var dimensionsTV: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
dimensionsTV = findViewById(R.id.idTVDimensions)
val display = windowManager.defaultDisplay
val size = Point()
display.getSize(size)
val width: Int = size.x
val height: Int = size.y
val dimensions = "Width : " + width + " px\n" + "Height : " + height + " px"
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 {
private TextView dimensionsTV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dimensionsTV = findViewById(R.id.idTVDimensions);
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" ;
dimensionsTV.setText(dimensions);
}
}
|
Now run your project to see the output of your application.
Output:
Share your thoughts in the comments
Please Login to comment...