How to get usable size screen size in android studio? - java

So I want to make an app compatible with multiple screen sizes, however the positioning of some buttons is done programmatically and it has to align with the background (timetable app, task aligned with proper day, start time and end time on a grid display). Hence, I need to get the screen dimensions.
Currently I'm testing this in Nokia 8.1 with height 2246px and width 1080px, however what I need is the actual usable height not the total height (minus the bar at the top and the bottom). After manually testing the usable height it seems to be 1887px, but I have not found a way to retrieve this number.
I tried these codes so far:
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getRealMetrics(displayMetrics);
int displayHeight = displayMetrics.heightPixels; //2246px
int displayWidth = displayMetrics.widthPixels; //1080px
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int displayHeight = displayMetrics.heightPixels; //2034px
int displayWidth = displayMetrics.widthPixels; //1080px
WindowManager windowManager = (WindowManager) mainActivity.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int height = size.y; //2034px
int width = size.x; //1080px
I thought maybe navigation bar is the problem
Resources resources = mainContext.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
int navigation_bar_height = resources.getDimensionPixelSize(resourceId); //126px
2034 - 126 = 1908px != 1887px (which is close but still to big, it goes off screen)
Furthermore, when I tried to make a new hardware profile with the phone dimensions (2246 x 1080), using 1887px goes off screen on the xml Design display, even though it shows up exactly right on my phone...
So yeah I'm lost, no idea how to get the real size or why the profile doesn't match. Please help.

Related

Adding an ImageView at a random position in a LinearLayout

I want to add image views dynamically to a full screen linear layout. I have written some code but some image views are either partially or totally going outside the screen. Some times image views are well within the phone screen.
How can I ensure that they remain totally inside the screen.
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
final int widthArea = displayMetrics.widthPixels;
final int heightArea = displayMetrics.heightPixels;
final LinearLayout home = (LinearLayout) findViewById(R.id.llHome);
ImageView imgView = new ImageView(MainActivity.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(200, 200);
imgView.setLayoutParams(lp);
imgView.setImageResource(R.drawable.img_1);
imgView.setX(r.nextInt(widthArea));
imgView.setY(r.nextInt(heightArea));
home.addView(imgView);
You should take into account the dimension of the image itself. Settings X and Y place its starting point within the screen but it might leak on a side.
First, load your image as a Drawable, then include drawable.getWidth() (and maybe even height, that depends on your layout) to your X position.
imgView.setX(r.nextInt(widthArea - drawable.getWidth()) + drawable.getWidth());

How to convert dp into % of the screen (weight)?

I have multiple layouts. By all I have hight=0 and and weigth=something. But now I want to add a layout that will allways be 60px. All works perfectly but now I need to know how much weight is 60px because I neet it for something else I am working on. So is it possible to get screen dimensions in dp. NOTE: I need to know the dp for each section. image
How exact can this be?
you can get the screen width and height like this:
DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(displayMetrics);
int screenWidth = displayMetrics.widthPixels;
int screenHeight = displayMetrics.heightPixels;
then you just divide the height you get by the number of layouts you want to display
EDIT: another option would be(if you know the number of layouts at compile time) to give each layout the same layout_weight value f.e. layout_weight=1

in a scroll view i need to set image height as activity height of the device

i need to set image height as activity height of the device programatically
So i need to retrieve activity size of the device, this size varies from device to device
i tried this
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
but this gives the entire size(including navigationbar and everthing) of the phone screen but i need the activity size
use a method like this:
public static Point getAppUsableScreenSize(Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
return size;
}
to get the usable screen size
If you want visible activity height excluding toolbar. Calculate tool/action bar height and set your image view layout_height as
required height = activity height-toolbar height

Camera not stretching to width of android device

In my android camera app, Im trying to make it so that the camera layout view fills the width of the screen and then is proportional (to a square) for the height of the layout. But its cropping it too small for some reason:
example of what is happening..
The green arrow represents where it should go to.
Ive tried fill_parent, etc. So, how can I make it go to the end of the screen?
I believe this is the problem. I know I did this because I was not sure how to make it automatically stretch to the right proportion.
int width = 352;
int height = 288;
camPreview = new CameraSurfaceView(width, height);
myCamHolder.addCallback(camPreview);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = width*288/352;
Maybe you don't need 288/352, but rather preview size.height/preview size.width

unable to get display metrics

I am making a game that displays animated characters on the screen
Now I am at the stage where i want it to display correctly on other devices as well
increase the screen resolution on my own device and have it still display correctly
for this I want to get the scale factor so I can change all my pixel values to DP values
however the display metrics does not change no matter what I change the screen resolution to
for example
this.getHolder().setFixedSize(1024, 768);
scale = getResources().getDisplayMetrics().density;
DisplayMetrics dm = new DisplayMetrics();
((Activity) getContext()).getWindowManager().getDefaultDisplay()
.getMetrics(dm);
float xDpi = dm.xdpi;
float yDpi = dm.ydpi;
Now no matter what I change 'setfixedsize ' to, both xdpi and scale stay the same even though it does show the increased screen resolution on the display
You can get display metrics using below line.
DisplayMetrics metrics = getResources().getDisplayMetrics();
This is my code for display wallpaper may be its help full=====>
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
Point size = new Point();
display.getSize(size);
float w = size.x;
float h = size.y;
width = ((w)/1000);
height = ((h)/1000);
Log.d("width===>", String.valueOf(width));
Log.d("height===>", String.valueOf(height));
(OR)
metrics = Resources.getSystem().getDisplayMetrics();
float w=metrics.widthPixels;
float h=metrics.heightPixels;
width = ((w)/1000);
height = ((h)/1000);
if(width == 0.72f && height == 1.28f){
width=1.08f;
height=1.776f;
}
Log.d("width===>", String.valueOf(width));
Log.d("height===>", String.valueOf(height));

Categories

Resources