getting a view WIDTH with match_parent property? - android

I have an imageView with layout_width="match_parent".
On my adapter's getView(), I'd like to get the actual width of this imageView but don't know how.
I tried using imageView.getWidth(), but this returns 0.

The view is not attached to its parent group in getView(), so it hasn't been measured, hence the zero width. The only way I can think of is to do a parent.getWidth() which returns the width of the ViewGroup. This will only work when your view is not going to be placed next to another view in parent. This is usually the case for ListView, so may work for your case.

Related

When get the real width?

I have set programmaticaly the width of a button to 0dp and a weight of 1 so that it has the same width of the rest of the buttons of the gridview.
The problem is that i cannot succeed in setting the height equals to the width as the width has been set to 0.
So i would like to know when the button takes its real width. For now, i'm using viewTreeObserver but i only get the good width when I click on the button.
Do you have any solution for this ?
Make a custom view subclass and override onMeasure. In your onMeasure, look at the measurespec to find out how big you can be, and set your appropriate width and height.
onMeasure is the function your parent view calls to figure out the size of its subviews. Make sure to call setMeasuredDimension inside onMeasure, or you'll throw an exception somewhere.

Modifying View width without calling requestLayout()

I have a FrameLayout with a 100 views (TextViews).
At some point I need to modify the width of 10 views.
Calling requestLayout() on 10 children as you know triggers a layout pass of all 100 views and it is very slow.
Is there a way to change only the width of these 10 views ?
I don't know if I explained well the problem. Basically what I need is to remeasure the View (TextView) so it will redraw itself using the new width value.

How do I fit my custom view to parent size if MeasureSpec is UNSPECIFIED in android?

I'm writing my custom view and I would like to make it the same width as parent is. And parent is RecyclerView which scrolls horizontally. So I receive widthMeasureSpec in my onMeasure method with size = 0 and mode = UNSPECIFIED.
So how do I know parent's width ?
Thanks.
Technically, width is infinite because it scrolls horizontally. If you want it to have the same width w/ RecyclerView, use recyclerView.getWidth(). You can use the RecyclerView instance passed to onCreateView.

get size of parent layout/fragment while drawing it

I have a custom view that I'm using canvas to be able to draw on it what I want, this view inside a scrollview that take match_parent for each width and height and also my custom view take match_parent for each width and height
I want to know the size of the scrollview to be able to draw only inside the available space that I have
I have tried onLayout, onMeasure but nothing works with me
Can any one please help?
I have solved this problem using
onGlobalLayout Method, and I also inherit Viewgroup not View and implement onLayoutMethod, to make element shown in my fragement
Thanks all

Unable to make child view wider than parent in Android Layout

I'm moving a view after an animation completes(to slide out a menu from the left). However, I don't seem to be able to achieve the effect I'm looking for. What I'd like is for the view to extend to the right past the parent's bounds, like this:
but what's happening is actually this:
The view resets itself to stay within the bounds of the parent. Even if I set an absolute pixel value (by looking at the display's width, or even a randomly large value).
The reason I need to do this is detailed in this SO question about a view's actual position after an animation has completed:
TranslateAnimated ImageView not clickable after animation [Android]
any thoughts? Thanks!
So, here is a related question. It explains how to move a view out of the screen.
I had same problem and I found the solution. I hope it can be helpful.
You need to set fixed width if you want to achieve this effect. To find real width of the view you need to measure it.
So full solution is:
view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
int height = view.getMeasuredHeight();
int width = view.getMeasuredWidth();
view.setLayoutParams(new LinearLayout.LayoutParams(width, height));
(Use LayoutParams of the type corresponding the type of the parent layout)

Categories

Resources