I have a RelativeLayout and i want it to fill the whole width of the view, but if it would become bigger than xx dip it should stay in this size.
I tried to use android:maxWidth="xxdp" but it is not working with match_parent.
Rather than setting width to match_parent, set the width to "0dp" and the weight of the layout to 1.0f.
Assuming there are no other views in the same parent, this should force the RelativeLayout to fill the width. maxWidth should now work like you expected.
Related
I have an ImageView and I want to set something like this:
min_with=match_parent and max_width=value
does someone know how to do that?
I am guessing your parent is not the root parent and your parent width is not match_parent either.
You can set your parent width as wrap_content and set child width as wrap_content but set child min width to some dp so your child won’t have less width than min width but it will hug the parent on all sides.
You can also take advantage of linear view and its weightSum, weight to childrens
We can go in details if you share more data and small drawing of what you want to do.
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);
I need to get the length of LinearLayout or RelativeLayout programmatically in Android like if I have a LinearLayout as follows
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
I need to increase height of the layout which is located in the mid of the screen by taking screen height and increase or decrease its height by taking calculations on the basis of screen height. So is there is a way of calculating the height of a Layout whose height is set as wrap_content. I tried to get Height from Layout Parameters but it gives me the value of -1 or -2 not height in pixels or density pixels.
Of course match_parent and wrap_content equals like -1 and -2.
In this case try to use viewTreeObserver.
Here a good link: http://www.sherif.mobi/2013/01/how-to-get-widthheight-of-view.html
Try the following:
myLayoutRef.post(new Runnable() {
int height = myLayoutRef.getHeight;
});
This runnable runs after your layout has been drawn and laid out on screen, so get height returns the real height and not 0
I've been trying to achieve this using nested LinearLayouts. Tried setting both width and height to 0dp . But it's not achieving 80% width and height. But it's working when i'm switching it to just single dimension ( ie, just width or height ). Couldn't get any info on it though i googled a lot .
So, now the question : does the Layout Weight work with both dimensions together?.
Note: I cannot use center gravity it , due to the lack of proper width in the children. They need to get it from linearlayout2 ( fill_parent ) .
1、The LinearLayout weight do not work both with width and height, It depends on the orientation of it. the default is Horizontal.