How can i get the dynamic width of LinearLayout at runtime?
I mean, i want to get the width of LinearLayout at runtime , when LinearLayout visibility is "GONE" and LinearLayout width is "match_parent" in xml layout file.
I can get width using onGlobalLayoutChangeListener and onPreDrawListener but only if our view(LinearLayout) is already "VISIBLE" state, not at "GONE" state.
Is here any solution for this problem?
Thank you so much for your time.
Simply fetch a reference to your linearlayout lets call it "ll" and a call to "ll.getWidth()" gives you thewidth in pixels...
You can use getMeasuredWidth() method:
int width = yourView.getMeasuredWidth();
PS: Be careful, you need to call it after onMeasure() gets executed, otherwise the view size is not yet measured.
Related
I read often something like this:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
What exactly LayoutParams do?
I've read the Documentary but I wasn't smarter after reading!
Hope someone can explain me what LayoutParams do or pass!
Kind Regards!
LayoutParams are the Java Object representation of all the params you give to your View in the .xml layout file, like layout_width, layout_height and so on. Getting this object from a View allows you to look up those params on runtime, but also to change them in your Java code, when you need to move the View, change it's size etc.
LayoutParams are used by views to tell their parents how they want to be laid out.
The base LayoutParams class just describes how big the view wants to be for both width and height. For each dimension, it can specify one of:
FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher), which means that the view wants to be as big as its parent (minus padding)
WRAP_CONTENT, which means that the view wants to be just big enough to enclose its content.
That's all folks.
LayoutParams is use for the dynamically change the layout width and height. and also use the create custom view without the xml by using the directly by use of the LayoutParams for Relative or Linear type layout.
Basically when you set an xml with 'match_parent' or anything like layout_something
the android inflator will set the layout param for the child with the appropriate Layout params with the type matching the parent control, you could also do this in code and if you forget or set the wrong type you will get an exception in runtime.
the parent control needs this information to layout the child control correctly and to his liking.
Please see the following: Android Developer site - Layout Params
I think this picture says it all
I'm using the following code to change the height of a relative layout that contains a listview. It works fine the first time I call it, but if i call the same code again later to change the height to a different value it does nothing. Why is this and how do I make it work?
LayoutParams params = myrelativelayout.getLayoutParams();
params.height = newHeight;
Try to add myrelativelayout.setLayoutParams(params); every time you change it. This will make sure the view is invalidated each time and that you update the Actual params object for the view.
my purpose is to set the height of a listView when the activity is initialized.
the height of my listview is less than the screen, and after intialized I want to get the height of the listView and make the height plus an integer,then set the listvView's height with the new number.
But when I use listview.getMeasuredHeight() or listview.getLayoutParams().height in onCreate or onStart or onResume.The answer is always zero.
How can I get the right answer?
I am new to android, thanks for your help.
That seems weird. Anyway, you can place your ListView in a ViewGroup lik LinearLayour, RelativeLayout and set the listview height to fill_parent. And then set the height of the viewgroup outside.
I have created linearlayout with layout:height="fill_parent" I have set 3 buttons and one TextView. I am able to see all. But when I want to get get the height of linearlayout. I used the following code in onCreate method. I am getting linear layout height as zero. But My linearlayout height is fill parent. and also i am able to see elements that i have added. Why I am not getting the actual height of my linearlayout.
LinearLayout mainLayout = (LinearLayout) findViewById(R.id.main_linearlayout);
System.out.println("...Height..."+mainLayout.getMeasuredHeight());
Thanks
Deepak
Refer to this question
Can't get height of Widgets
When an Activity receives focus, it will be requested to draw its layout. The Android framework will handle the procedure for drawing, but the Activity must provide the root node of its layout hierarchy.
See this link view drawing and layout padding and margis
Till onCreate, OnResume, onStart view is not yet created.
So if you want to get height or width of view do following and get it
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener()
{
#Override
public void onGlobalLayout()
{
SearchResultActivity.this.sideIndexHeight = SearchResultActivity.this.sideIndex.getHeight();
ViewTreeObserver obs = sideIndex.getViewTreeObserver();
obs.removeGlobalOnLayoutListener(this);
}
});
Please get new TreeObserver to remove, otherwise you will get an exception, TreeObserver is not alive
overriding onWindowFocusChanged method, you get the height or width of view. But if your activity is started as child activity then onWindowFocusChanged method will not get called. So in that case use the above one.
Is there a way to declare a row of buttons in XML so that all the buttons have the same width, which is equal to the wrap_content width of the widest button? I'm familiar with the trick of setting all the widths to 0 and assign them all a weight of 1, but that won't work if the parent layout width is set to wrap_content. I don't want to set the parent width to fill_parent because I don't want the buttons stretched more than necessary.
The only way I can think of doing this is in code (either with onMeasure logic in each button that communicates with the other buttons or with a custom layout class).
I think you'd have to do this in code.
Creating a custom layout class would be the way to go. Override onMeasure() and make it look something like this:
Call setLayoutParams on all children to set their layout_widths to WRAP_CONTENT.
Call super.onMeasure()
Iterate child views to find the one with the biggest getMeasuredWidth().
Iterate all other child views calling setLayoutParams() with the widest pixel width.
Call super.onMeasure() again. :)
That should work but I won't stake my reputation on it... happy to help you further if it doesn't.
To get the buttons in a row in the XML you need to add the buttons with in a LinearLayout and change the orietnation to horizontal i.e.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
</LinearLayout>
As for getting the widest button and changing all the other buttons to match I am not sure as a guess you would have to have some sort of method within your activity to get the widest button and then programaticaly set all the other buttons to be the same.
Just find out what your widest button is, but it in a view with a horizontal width to match, and then use layout_width="match_parent" or "fill_parent" in < 2.3.
It'll make them all use the width assigned.
If you want to do it programatically, you need to iterate over all the sections, find the max, than iterate again and set it.