Resize View in dp - java

I'm stuck trying to resize the width of a View in a ListView programmatically, because I want it to be in DP, not pixels.
My ListView generate instances of a RelativeLayout with a View that fills all the screen (match_parent), and a TextView and a button inside.
The View is set to width = match_parent to fill the whole screen, as intended
What I want, is to have different View width in my adapter. Like 25%, 50%, or 75% of the total width of the base View.
mRelativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(**[VALUE I NEED HERE]**, mRelativeLayout.getLayoutParams().height));
The ImageView is set to width = match_parent
So this are the relevant parts of code in my ViewList adapter :
RelativeLayout mRelativeLayout = (RelativeLayout) convertView.findViewById(R.id.test_layout);
mRelativeLayout.setBackgroundResource(arrayListName.myColor);
if (arrayListName.myColor == R.color.custom ) {
dayviewLayout.setLayoutParams(new RelativeLayout.LayoutParams( 25% , dayviewLayout.getLayoutParams().height));
}
if (days.mMoodColor == R.color.custom2) {
dayviewLayout.setLayoutParams(new RelativeLayout.LayoutParams( 50% , dayviewLayout.getLayoutParams().height));
}
if (days.mMoodColor == R.color.custom3) {
dayviewLayout.setLayoutParams(new RelativeLayout.LayoutParams( 75% , dayviewLayout.getLayoutParams().height));
}
The 25-50-75% are obviously wrong, as it takes only int values. I want it to fit any screen.
Thanks for your support.

dp units are just px units multiplied by the current screen density, which is available via the DisplayMetrics class:
float density = [some context].getResources().getDisplayMetrics().density;
int dp = (int) ([your px value] * density);

Related

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

View.getWidth() shows the correct width but cannot produce the right margin when added to View's x position

So I overlayed a view of another view. And try to position my arrow so that it points to the image underneath it:
The arrow is an ImageView and I set its top and left margins to position it in the right place. I did it by adding the width of the TextView underneath it that says "No photo taken today" to the TextView's left border position. But funnily, the arrow is pointing to the middle and not the end of that TextView.
int topMargin=(int)pxToDp(getRelativeTop(dailyPhotos)+(int)dailyPhotos.getHeight(),this);
int leftMargin=(int)pxToDp(getRelativeLeft(dailyPhotos)+dailyPhotos.getWidth(),this);
getRelativeLeft() I copied from this answer.
I show the width of that TextView and the screen's width in a Toast. They are both correct since that TextView's width is set to match_parent. I don't what is wrong.
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
Toast.makeText(this,"width="+dailyPhotos.getWidth()+", screen's width="+width,Toast.LENGTH_SHORT).show();
showNote.putExtra("photoCountTopMargin", topMargin);
showNote.putExtra("photoCountLeftMargin", leftMargin);
Reading/Changing the margin of a view needs to be done in the layoutparams of the view
LayoutParams layoutParams = (LayoutParams) view.getLayoutParams();
layoutParams.topMargin = ddd (in pixels)

Custom view and change the size of the child views

I have a custom view that extends from FrameLayout. I'd like one of the children to have a size equal to the 20% of the total size as shown in the image.
How can I achieve the imageview to always have this size even if the custom view changes its size in runtime?
Lets say you have your view:
FrameLayout view = (FrameLayout)findViewById(R.id.customView);
Then you can get the layoutParams of the view calculate 20% of it, and then create a new view which will be 20% of the view.
For example:
ImageView img = new ImageView(this);
int imgSize = view.getLayoutParams().width * 0.2; //20 % of the width
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(imgSize, imgSize);
//params.gravity = Gravity.TOP;//How to set gravity programmatically
img.setLayoutParams(params);
//Set img attirubutes like src etc.
view.addView(img)
Also because you are using a FrameLayout it might be better to create an instance of a LayoutParam first and the set it in img.setLayoutParams(params), because iam sure you want to change the layout_gravity of the different views, which you can do in the params vairable

Relative coordinates of an ImageView in Android

I have a square image in an ImageView contained within a FrameLayout that is right aligned (landscape layout). The FrameLayout is set to FillParent width and height and the ImageView is set to Adjust View Bounds so that the resulting ImageView is a perfect square with each side being the length of the height of the landscape layout, right aligned. However, the original image is larger than this.
I need to get the coordinates clicked on the ImageView in relation to itself (ie. the top left hand corner of the ImageView is 0,0 and the bottom right corner would be the displayed width and height coordinates) so that I can work out if the clicked point was past the mid-point of the ImageView.
I initially get the size of the ImageView using:
int myImageViewWidth = imageView.getWidth();
int myImageViewHeight = imageView.getHeight();
I've created an OnTouchListener and within this I'm using event.getX() and event.getY(), but this seems to return the coordinates relative to the image's oiginal size, not the displayed size. I've also tried the following:
Matrix m = imageView.getImageMatrix();
float[] values = new float[9];
m.getValues(values);
float relativeX = (event.getX() - values[2]) / values[0];
float relativeY = (event.getY() - values[5]) / values[4];
But again this seems to return inaccurate coordinates. Can anyone please help?

Categories