Android Get Screen Width & Density in Pixels

Learn how you can get screen width in Android at runtime. Here is a simple way you can get the screen width and height.

Look at the snippet of code below. I have provided the example in both Java and Kotlin. Will be beneficial for beginners in both Java and Kotlin.

Android Get Screen Width in Kotlin

The code for Kotlin is provided below. We have utilized the Kotlin extension to get height and width.

You can add the code anywhere in the project and it will just work.

fun Activity.getSize(): Point { val metrics = DisplayMetrics() if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { display?.getRealMetrics(metrics) } else { windowManager.defaultDisplay.getRealMetrics(metrics) } return Point(metrics.widthPixels, metrics.heightPixels) }
Code language: Kotlin (kotlin)

Now in your activity you can do the following. Just the highlighted lines are important.

class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val size = getSize() val width = size.x val height = size.y // use the values in whichever way you like } }
Code language: Kotlin (kotlin)

Android Get Screen Width in Java

Now let us look at the same code in Java. The implementation is same but here we will accomplish things using Java.

Create a file named ScreenUtil and copy paste the code below.

public abstract class ScreenUtil { public static Point getSize(Activity activity) { DisplayMetrics displayMetrics = new DisplayMetrics(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { activity.getDisplay().getRealMetrics(displayMetrics); } else { activity.getWindowManager().getDefaultDisplay().getRealMetrics(displayMetrics); } return new Point(displayMetrics.widthPixels, displayMetrics.heightPixels); } }
Code language: Java (java)

Now if you want to use it you can always do.

Point size = ScreenUtil.getSize(this) // where this is the activity int width = size.x; // the width of screen int height = size.y; // the height of screen
Code language: Java (java)

That is all we need to do. Now let us continue exploring how we can get the density of the screen.

Get Android Screen Density

The code is exactly same as above we will access only the density portion.

Add the code below in the Java file created above.

public abstract class ScreenUtil { public static float getDensity(Activity activity) { DisplayMetrics displayMetrics = new DisplayMetrics(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { activity.getDisplay().getRealMetrics(displayMetrics); } else { activity.getWindowManager().getDefaultDisplay().getRealMetrics(displayMetrics); } return displayMetrics.density; } }
Code language: Java (java)

Conclusion

You have learned how you can get the screen height, width and density using Java and Kotlin. If you really want a deeper understanding of the DisplayMetrics class.

Check out this official android documentation for in depth knowledge about the class. If you are just beginning with Android. You will find the following links helpful.

Suggested Read

Android Intent: Learn Implicit Intent and Explicit Intent

Push Notifications in Android Using One Signal

Show or Hide Soft Keyboard in Android Application And More

OnClickListener in Android RecyclerView: Example

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *