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
Related
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.
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());
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
i need to edit absolute values of ImageView:
tools:layout_editor_absoluteY="0dp"
to
tools:layout_editor_absoluteY="50dp"
but in java if I just do
ImageView img = (ImageView)findViewById(R.id.mainView);
ConstraintLayout.LayoutParams params1 = (ConstraintLayout.LayoutParams)(img.getLayoutParams());
params1.editorAbsoluteY = 50;
img.setLayoutParams(params1);
it doesn't work.
Convert your value from DP to pixels:
public int dpToPx(int dp) {
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
return Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
}
before setting the params1.editorAbsoluteY.
LayoutParams always deal with pixel values.
So you'll need to convert your DP to PX values before setting the values on the LayoutParams. There are many examples of this, a quick google should do it.
If that doesn't work, you'll need to give more information than simply saying "it doesn't work".
I have a view which is bigger than the screen.
How can I get the width of its currently visible area?
getWidth() returns the absolut size.
You can get the screen width using the following code:
If you're calling this outside of an Activity, you'll need to pass the context in (or get it through some other call). Then use that to get your display metrics:
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
Using services you might do:
Display display= ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();