How to Get Screen Dimensions as Pixel in Android?

Many times while building an Android application we can dynamically add components to the Android application. We can place elements in a particular position on the screen. We will need to get the device screen dimensions. In the article, we will take an example of dimension as a pixel in an Android application.



Here is the step by step Implementation process:

Step 1: Create a new project in Android  or you can use an already created project.  



Step 2: Create an activity_main.xml file in your layout folder .(res>layout folder

 implement the same invoke the following code inside activity_main.xml file.

 <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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">

<TextView
android:id="@+id/txtTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginTop="80dp"
android:text="Candidroot solutions"
android:textAlignment="center"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.842"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/txtDimensions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="116dp"
android:layout_marginEnd="20dp"
android:textAlignment="center"
android:textSize="18sp"
android:textStyle="bold"
android:text="Screen Dimensions in Android"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txtTitle" />

<TextView
android:id="@+id/txtDimensionsValue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="76dp"
android:layout_marginEnd="20dp"
android:textAlignment="center"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txtDimensions" />

</androidx.constraintlayout.widget.ConstraintLayout>
Step 3: Create an MainActivity.kt file in your package folder .

 implement the same invoke the following code inside MainActivity.kt file.

 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 {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_two);

// create dimension object
TextView txtDimensions = findViewById(R.id.txtDimensionsValue);

//object of Display class
Display display = getWindowManager().getDefaultDisplay();

//object of Point
Point size = new Point();

//set side of point object
display.getSize(size);

//set size of width of screen
int width = size.x;

//set size of height of screen
int height = size.y;

//set dimension value
String dimensions = "Width : " + width + " px\n" + "Height : " + height + " px";

//getting vlaue set on textview
txtDimensions.setText(dimensions);

}

}

 

Step 4: Output of above e​xample



Happy coding!



365Bloggy May 24, 2024
Share this post
Tags
SUBSCRIBE THIS FORM


Archive