For an app i have an activity with various views and I need them all to be square, have the exact same height and width and occupy all together a specified portion of the screen. Therefore, I need to set the width and height programmatically, which seems to work with
View view = findViewById(R.id.nutrition_bar_filled);
LayoutParams layoutParams = view.getLayoutParams();
layoutParams.width = newWidth;
view.setLayoutParams(layoutParams);
However, with many views this is pretty tedious, is there a way to set all heights and widths at once as they are all uniform?
Thanks in advance!
Related
I'm working with an ImageView and I want to resize it programmatically passing (for example) from a full-screen ImageView to a 50x50. Is there a way to do that?
Different from the one you suggested me to see because I don't need to fit the image in the ImageView but to resize the dimension of the ImageView.
My suggestion use a relative layout and put image inside that has with weight and able to auto resize which depends on the weight. The best way to auto resize even your screen rotates.
I didn't remember which one layout has weight format
You can use LayoutParams to set height width programmatically -
ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
layoutParams.width = 30;
layoutParams.height = 30;
imageView.setLayoutParams(layoutParams);
ConstraintLayout provides a mechanism for fixing the aspect ratio of a child view. Select the child view that we want to control, and then set the ratio value, we can manually put the ratio that we want to set.
Using constraintLayout makes UI shrink or expand according to screen size without distorting the UI. This is the most important advantage of using ConstraintLayout in UI.
I have a ScrollView with a LinearLayout with 3 elements inside. I would like that the first element has a height of 1/3 the height of the device height and the other 2 with wrap_content, is this possible to do in xml or how would you do this? Using weight alone it does not seem possible because the LinearLayout inside the ScrollView could be longer than the device's height.
According to Android docs, android:layout_weight:
Indicates how much of the extra space in the LinearLayout is allocated to the view associated with these LayoutParams. Specify 0 if the view should not be stretched. Otherwise the extra pixels will be pro-rated among all views whose weight is greater than 0.
This implies that the weight is not dependent on the screen height, but rather only on its parent view, so you can't achieve that from xml. To achieve this I would compute the height of the screen and then resize the view:
Display screen = getWindowManager().getDefaultDisplay();
Point size = new Point();
screen.getSize(size);
int screenHeight = size.y;
myView.setLayoutParams(new LayoutParams(myView.getWidth(), screenHeight / 3));
You can give the first linear layout weight=0.333 with height=0 and the other two layout wrap_content
Not Just the height of the progressBar but the actual progress track too, they seem to have different properties. I'm already using the layout weight of for it's horizontal length, but it has no effect on its height.
I think ProgressBar has a proportionate height and width attributes, meaning you can't have a bigger height value than an width otherwise it'll look skewed. (I could be wrong, but from playing around with the View that's what it seems like.)
If you're using a layout_weight value for the width, then it will have no effect on the height, you can set your height to be match_parent and the size you get will be the maximum size based on the width.
To set your ProgressBar at 50% use progressBar.setProgress(progressBar.getMax()/2) or if you know the max value ahead of time, you can hard-code the 50%.
This seems to set the scale of the progress track to exactly half of the parent:
LinearLayout progView = (LinearLayout) findViewById(R.id.progView);
ProgressBar xpBar = (ProgressBar) findViewById(R.id.xpBar);
xpBar.setScaleY(progView.getScrollBarSize()/2);
Im having problems specifiying the height of a relative Layout. From what i understand, these two blocks of code should be equivalent (myLayout is a RelativeLayout that i defined in XML, with an initial height of "0dp", and it's parent is also a RelativeLayout):
BLOCK 1:
RelativeLayout.LayoutParams p = (RelativeLayout.LayoutParams)myLayout.getLayoutParams();
p.height = (int)(35*scale);
myLayout.setLayoutParams(p);
myLayout.invalidate();
BLOCK 2:
RelativeLayout.LayoutParams p = (RelativeLayout.LayoutParams)myLayout.getLayoutParams();
RelativeLayout.LayoutParams newP = new RelativeLayout.LayoutParams(p.width, (int)(35*scale));
myLayout.setLayoutParams(newP);
myLayout.invalidate();
Scale, in my case, is 2.
So i expect myLayout to have a height of 70 after the execution of either of these blocks, and i expect it to return that height when i call myLayout.getHeight(). Also, i expect myLayout to occupy a rect with the height of 70 and its former width (happens to be match_parent).
But when i run the code, the first block does not change the height of myLayout on screen, nor does it change the return value of myLayout.getHeight(). It does, however, change myLayout.getLayoutParams().height.
Now the second block does work, although i have to run it twice(!?) for the change to take effect. Im seriously at a loss here and i cant find anything even closely related to this in the docs.
I thought this would be an easy task when i set out yesterday, but by now im questioning my sanity, among other things. Any Ideas?
If size is determined dynamically, you may need to use ViewTreeObserver to get the size seen on the screen.
Set your width and height in defining your params. If you want the width to be match_parent, change your code like below and it is going to work (if you don't want match_parent, just set the width that you desire in the code below):
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, (int)(35*scale));
myLayout.setLayoutParams(params);
I'm changing the width of a grid view based on its column width and number of columns (which works):
gridView.setLayoutParams(new LinearLayout.LayoutParams(gridManipulation.getColumnWidth() * (int)Math.sqrt(str.length) + 10, LayoutParams.FILL_PARENT));
gridView.setGravity(Gravity.CENTER);
I've tried re-centering it in the parent view but its not working, does anyone know why, its still centred as if it still has the width parameters before my dynamic change.
Set the Gravity as LayoutParams property then set that LayoutParams to your GridView as follows...
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(gridManipulation.getColumnWidth() * (int)Math.sqrt(str.length) + 10, LayoutParams.FILL_PARENT);
params.gravity = Gravity.CENTER;
gridView.setLayoutParams(params);
you cant center it if its bigger than its parent, which is never bigger than the screen, unless you set it manually, which will cause the system to render data outside the screen and slow you way down.....
is it expanding verticaly or horizontally or both? my recomendation would be to put it in a scrollview.