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);
}
}
0 Comment(s)