Android – Am I Doing this Right? - java

I am building an android app that requires a layout to be added programmatically. This is the line that seems to be giving me a hard time:
RelativeLayout.LayoutParams rightScreenParams = new RelativeLayout.LayoutParams(
(int) (screenWidth * 0.25), (int) (screenHeight * 0.94));
The variables screenWidth and screenHeight are the width and height of the screen in pixels. The layout is supposed to be 25% of the width of the parent layout, and 94% of the height. When I add the parameters to the layout and then add the layout, it takes up the entire screen instead of the dimensions I mentioned.
So am I using this constructor correctly? Any help is appreciated. Thank you for your time.
Edit:
I get screenWidth and screenHeight in onCreate():
DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager wm = (WindowManager) getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(displayMetrics);
screenWidth = displayMetrics.widthPixels;
screenHeight = displayMetrics.heightPixels
The variable screenHeight is returning as 1184 even though the actual height in pixels is 1280. The variable screenWidth seems to be fine. So how can I get an actual screen height in pixels?

RelativeLayout.LayoutParams rightScreenParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.Wrap_Parent,samehere);
View.setLayoutParams(rightScreenParams);//your view
//after you add it to the layout then you call
rightScreenParams.width = (int) (screenWidth * 0.25);
rightScreenParams.height = (int) (screenHeight * 0.94);
its the same thing but it forces android.

Related

Android layout for specific device not show up

I prepared for my two devices two different layouts. First I checked the width and height with this code:
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics ();
display.getMetrics(outMetrics);
float density = getResources().getDisplayMetrics().density;
float dpHeight = outMetrics.heightPixels / density;
float dpWidth = outMetrics.widthPixels / density;
It showed me that, first device has 360dp width and 592dp height and the second 360dp width and 692dp height. So I prepared two layouts in such folders layout-w360dp-h592dp and layout-w360dp-h692dp.
And it turns out the devices don't use the layout that they are dedicated to. 360dp width and 592dp height device uses default layout and 360dp width and 692dp height device use layout for 360dp width and 592dp height.
Where is the problem? Thank you in advance!

Set a view's width to be relative to device screen programmatically

I have a horizontal RecyclerView with child elements which I want to change the width of to be a bit less than the current match_parent set via the layout. I want to do this so that the next element in list is a bit visible on screen.
In the adapter onCreateViewHolder I am doing this:
ViewGroup.LayoutParams params = view.getLayoutParams();
params.width = MainActivity.deviceWidth - (int) MainActivity.dpToPx(40.0f);
view.setLayoutParams(params);
where MainActivity.deviceWidth is:
DisplayMetrics displaymetrics = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
deviceWidth = displaymetrics.widthPixels;
screenDensity = Resources.getSystem().getDisplayMetrics().density;
and dpToPx is this function:
public static float dpToPx(float dp) {
return (dp * screenDensity + 0.5f);
}
This works. however, I noticed that on different devices, the "margin" I am setting is not always the same. I.e. the next card in the RecyclerView is not always showing the same amount.
Is the dpToPx conversion correct? I might be missing something else in my calculation but I am not sure what.
Update
Changing:
params.width = MainActivity.deviceWidth - (int) MainActivity.dpToPx(40.0f);
To:
params.width = (int) (MainActivity.deviceWidth *0.9);
works!. I am fine with this solution. I just want to know why the previous one does not work...

Android add imageview programmatically fix size all screen

I'm creating several ImageViews programmatically, but I'm running into an issue where there are different ImageView sizes on different displays.
I want the ImageView size to be fixed on all screens. Here is how I am generating those ImageViews:
for (int i = 0; i < myImageList.size(); i++) {
ImageView iv = new ImageView(this);
iv.setImageResource(myImageList.get(i));
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(420, 210);
lp.gravity = Gravity.CENTER;
iv.setLayoutParams(lp);
float angleDeg = i * 360.0f / myImageList.size() - 70.0f;
float angleRad = (float) (angleDeg * Math.PI / 180.0f);
iv.setTranslationX(320 * (float) Math.cos(angleRad));
iv.setTranslationY(320 * (float) Math.sin(angleRad));
iv.setRotation(angleDeg + 80.0f);
main.addView(iv);
final int finalI = i;
}
You could introduce new class derived from ImageView and make all those adjustments in that class. Then just replaces ImageView to your class implementation in layouts etc
There are multiple interpretations of your questions.
The ImageView should have same size on all the devices - use dp. You are using pixels right now in the LayoutParams.
The ImageView should look that it has same size on all screens - find the width and height of the screen, set an aspect ration, eg. 0.33 and use that factored width and height in LayoutParams.
Use MATCH_PARENT in LayoutParams for width as well as height and the ImageView will take the full size (of the parent).
This question has a lot of great explanation about dp, px, sp that could be useful - What is the difference between "px", "dip", "dp" and "sp"?
To find screen sizes
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
// these are sizes in pixels
metrics.widthPixels, metric.heightPixels
Then, you can use this
iv.setScaleType(ImageView.ScaleType.FIT_XY);
See this link for all ScaleType values
Android ImageView ScaleType: A Visual Guide

Getting the actual screen height (android)

I want to get the actual screen height of the device that runs my app. To achieve this i try the following:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int Height = metrics.heightPixels;
TextView HeightView = (TextView) findViewById(R.id.screenHeight);
HeightView.setText("Screen Height: " + Height);
int Width = metrics.widthPixels;
TextView WidthView = (TextView) findViewById(R.id.screenWidth);
WidthView.setText("Screen Width: " + Width);
The device that I run using the emulator has a screen width of 1080 pixels and a screen height of 1920 pixels. The width is displayed correctly (1080 pixels) but the height is according to the app only 1776 pixels. What do I need to make my app display the correct screen height? Is metrics.heightPixels a bad way of getting the screen height?
see this answer
if your API level > 13 try this if you are in activity
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
If you're not in an Activity you can get the default Display via WINDOW_SERVICE:
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
getWidth and getHeight is for API level 13 or less
UPDATE
For API 17 and higher method Display.getRealSize() returns full size of screen, as mentioned in documentation:
Gets the real size of the display
without subtracting any window decor or applying any compatibility
scale factors.
The size is adjusted based on the current rotation of the display.
The real size may be smaller than the physical size of the screen when
the window manager is emulating a smaller display (using adb shell am
display-size).
int width=Resources.getSystem().getDisplayMetrics().widthPixels;
int height=Resources.getSystem().getDisplayMetrics().heightPixels;
I needed to get the actual available height, and out of all the options this is the only thing that worked for me.
Create a rectangle, and get its dimensions:
Rect r = new Rect();
activityRootView.getWindowVisibleDisplayFrame(r);
int screenHeight = r.bottom - r.top;
where activityRootView is the root view of your layout.
If you need to extract the toolbar height:
int availableScreenHeight = screenHeight - toolbar.getHeight();
where toolbar is your toolbar view.
availableScreenHeight will now be without the statusbar, without the toolbar, and without the navigation bar (if the device is using it).
you can use this code:
val display = windowManager.defaultDisplay
val size = Point()
display.getSize(size)
val width: Int = size.x
val height: Int = size.y
You can try this:
WindowManager wm = getWindowManager();
Display d = wm.getDefaultDisplay();
Point size = new Point();
d.getSize(size);
Log.i("LOG", "W=" + size.x + " , H=" + size.y);

Set button size with pixels

In my application I control the size of my button proportionally with the screen size:
int screenWidth = displaymetrics.widthPixels;
int screenHeight = displaymetrics.heightPixels;
int buttonWidth = (int) (screenWidth * 0.07);
int buttonHeight = (int) (buttonWidth * 1.2);
The button takes the same space (for example 5 %) of the device's screen size. What I want now is that the button's text takes the same space of the button area on every device.
I tried this:
int textSize = (int) (buttonHeight * 0.145);
myButton.setTextSize((float) (textSize));
I don't know why but on bigger screens (such as tablets) the text uses a lot less space of the button than on smaller screens.
Thanks !
Maybe
Try specifying the unit of measurement like this:
myButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, (float) (textSize));
setTextSize uses sp units by default instead of px, so that might be causing your issue!

Categories