Why height of linearlayout is zero - android

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.

Related

Android: FLAG_TRANSLUCENT_STATUS is not providing correct screen height

In my application, I am displaying list of cards. If first card is visible then status bar translucent and non-translucent for other list items. To achieve this I have used
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
It is shifting screen and I am not getting correct screen height. I have set list item height as match parent but getHeight is returning height value as screen height - status bar height.
Setting TreeObserver on Window view has worked for me. After adding FLAG_TRANSLUCENT_STATUS, added tree observer and inside it I set the parent view padding which had invoked onLayout method of the parent view and painted layout properly
getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
}
});

Want to get width of View(LinearLayout) at Runtime in Android

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.

Android equivalent to View.setTranslationX, but to adjust width?

I have been spending hours unsuccessfully trying to adjust the width, height, and offset of a simple view in Android as the result of a button press. I have discovered that setTranslationX and setTranslationY always work; the legacy method of setLayoutParams never works once the view is laid out initially. Calls to requestLayout() and invalidate() similarly produce no results.
I have tried to setLayoutParams within the context of posting a runnable, but this does nothing.
Because setTranslationX always works, I would just use that, but unfortunately there is no equivalent method like setWidth or setHeight.
As you can see in the AOSP, setTranslationX makes a call to invalidateViewProperty, which is a private method of View.
Is there an equivalent method to setTranslationX to adjust a view width or view margin, that presumably triggers invalidateViewProperty, and, by extension, works reliably?
EDIT
While in some situations, setLayoutParams may be expected to work after the initial layout, I am in a situation where setLayoutParams has no effect after the initial layout, but setTranslationX does. My setup is as follows:
Running Android KitKat 4.4
The view in question is MATCH_PARENT for both width, height
The view in question is a child of a RelativeLayout
The view in question is a View class with a simple solid-color background drawable
Here is the view:
<View
android:id="#+id/border"
android:background="#drawable/match_background_border_transparent"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
And here is the (non-working) code meant to dynamically alter its margins, but has no effect. Again, if I call setTranslationX, that always works.
holder.toggleButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
imageBorder.post(new Runnable() {
#Override
public void run() {
RelativeLayout.LayoutParams p = (LayoutParams) imageBorder.getLayoutParams();
p.leftMargin = 20;
p.rightMargin = 20;
p.topMargin = 20;
p.bottomMargin = 20;
imageBorder.setLayoutParams(p);
// imageBorder.setTranslationX does have an effect if I included it here
}
});
}
});
I have determined why setTranslationX was working, but setLayoutParams was not. My views were ultimately descendents of an AdapterView. I was able to programmatically manipulate LayoutParams of the AdapterView and his siblings, but none of the AdapterView's descendents.
Additional research showed that this was a common Android question:
Margin on ListView items in android
Why LinearLayout's margin is being ignored if used as ListView row view
What was confusing was that this view was several levels deep; i.e., it went:
AdapterView -> FrameLayout -> RelativeLayout -> View
Anyhow, I was able to accomplish my programmatic layout goals by wrapping view in another view, and using setPadding.

how to set the height of a listview when the activity is initialized

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.

Runtime adjustment of layout parameters

In a fragment's view, I'd like to adjust the layout params of one of the child views based on the width of the containing view, whose layout parameters are set to match_parent. What I need to do is to call getWidth/getHeight for the parent and then set the child view's ViewGroup.LayoutParameters, etc.
The problem is that getWidth will only return a valid value after the layout has been drawn the first time and I'm not sure if there are any fragment overrides that I can use for doing this. For example, the view's actual width is not determined when onCreateView or onStart are called.
So is there a straightforward way to implement this, or am I going to have to subclass a view and override onLayout or onMeasure?
You could set a global layout listener on the view tree and do your diddling in there.
final ViewTreeObserver vto = myView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
// do layout tweaking based on measurements
// remove the listener... or we'll be doing this a lot.
vto.removeGlobalOnLayoutListener(this);
}
}

Categories

Resources