unknown os platform
android get screen width and height programmatically | Whats-online.info
whats-online-info

android get screen width and height programmatically


android
2017/01/20 / Science and Tutorials

android code: learn how to get the width and height of your Android phone screen programmatically. See an example.





Android get screen size


Hi, this another great moment I would like to discuss how to get the width and height of your Android phone screen programmatically.
Just very simple steps and we are done.



We will create a function that returns the screen width and height sizes as an array of integers. We print out the results on our app launch as shown below.

Function to get the screen sizes

private int[] getScreenSIze(){         DisplayMetrics displaymetrics = new DisplayMetrics();         getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);         int h = displaymetrics.heightPixels;         int w = displaymetrics.widthPixels;         int[] size={w,h};         return size;     }

Print out the width and height of the screen in DP onCreate function

 int[] screenSize= getScreenSIze();         int width=screenSize[0];         int height=screenSize[1];         screenSizes.setText("Phone Screen sizes \n\n  width = "+width+" \n Height = "+height);

Entire Java code


package info.whats_online.androidscreensizes; import android.app.Activity; import android.os.Bundle; import android.util.DisplayMetrics; import android.widget.TextView; public class MainActivity extends Activity {     private TextView screenSizes;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         screenSizes=(TextView) findViewById(R.id.screenSizes);         int[] screenSize= getScreenSIze();         int width=screenSize[0];         int height=screenSize[1];         screenSizes.setText("Phone Screen sizes \n\n  width = "+width+" \n Height = "+height);     }     private int[] getScreenSIze(){         DisplayMetrics displaymetrics = new DisplayMetrics();         getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);         int h = displaymetrics.heightPixels;         int w = displaymetrics.widthPixels;         int[] size={w,h};         return size;     } }


Entire XML code Design 


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"      android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical"        tools:context=".MainActivity">     <LinearLayout         android:layout_width="match_parent"         android:layout_height="wrap_content">             <TextView android:text="http://www.whats-online.info"                 android:layout_width="wrap_content"                 android:textSize="30dp"                 android:layout_height="wrap_content" />     </LinearLayout>     <LinearLayout         android:layout_width="match_parent"         android:background="#f0f0f0"         android:layout_height="match_parent">         <TextView             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_gravity="center"             android:textSize="20dp"             android:id="@+id/screenSizes"/>     </LinearLayout> </LinearLayout>

Thanks for your time once more. Subscribe to our newsletter to get the latest tuts updates directly to your mailbox.












You may also like:


Leave a comment












Subscribe to get replies direct to your mailbox

Submit comment