Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Get Screen Dimensions in Pixels for Android Device in the Main Activity

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 489
    Comment on it

    If you want to get the dimensions in pixel of your device then add the following code snippet in your Activity where you wish to get the width and height pixel of your android device and make use of it wherever you wish to, in this tutorial I'm making use of DisplayMetrics whose object helps me to find out the width and height pixels.

     

     

    There are two methods that return the value of pixels in integer format 

    1) DisplayMetrics object .heightPixels returns the integer pixels value for height.

    2) DisplayMetrics object .widthPixels returns the integer pixels value for width.

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
    
    
            DisplayMetrics metrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metrics);
    
            int height=metrics.heightPixels;
            int width=metrics.widthPixels;
    
            Log.e("Width and Height are",height+" "+width);
    
        }
    }

     

    How to Get Screen Dimensions in Pixels for Android Device in the Main Activity

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: