Get Height of LinearLayout or RelativeLayout in Android programmatically - android

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

Related

Dynamic height of ImageView in scrollView

I am trying to add 3-4 imageviews in a scroll view. Imageviews have dynamic heights. Height of first view have 70% of the screen and second and third have 30% height. I am using constraint layout and guidelines but the height is getting set according to the height of the scroll view but not according to the screen height. Is there a way to do this with constraint layout.
Have you tried doing it programmatically?
First, get the Screenheight for your device: How to determine the screen width in terms of dp or dip at runtime in Android?
Secondly, get your Imagview and set its height according to xx% of the determined screen height.
BR

First child of LinearLayout inside ScrollView with 1/3 space of device height

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

Can I set the height of a progressBar to half the height of it's parent in xml?

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);

Changing background of LinearLayout changes it's width & height

I tried to change background of a LinearLayout to some wood texture, but it changes LinearLayout width and height depending on dimensions of the wooden background!
I need LinearLayout to disregard background dimensions!
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/wood"
android:orientation="vertical" >
some content here which I need to wrap height to them!
</LinearLayout>
Your LinearLayout's height is "wrap_content". Which means it takes height of the content(your background). You can change it to "match_parent" or give it a fixed size in dp. So that it remains same even with background.
If You want it to be of fixed either use fixed height and width in dp Or dynamically calculate height and width of old background and then apply new background with setting LinearLayout height and width to be of size you calculated programatically.
I simply replaced it with another container such as ConstraintLayout.. I don't have time to figure it out.

Relative Layout match parent and maximum width

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.

Categories

Resources