32

I have a small project where I want to reuse a certain UI component a few time so I created a widget by expanding a ViewGroup. In that ViewGroup I inflated a view that contained a TextView inside a LinearLayout and added that inflated view to the ViewGroup trough addView.

The outer LinearLayout expands itself perfectly but the inner TextView have getHeight() = 0 and getWith() = 0 when I view it through Hierarchy Viewer. The strange thing is that layout_height and layout_width is the values I gave them in my xml.

I don't have the code here but it looked something like this:

xml:

<LinearLayout  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent">  

    <TextView  
        android:text="random text.."  
        android:layout_with="200px"  
        android:layout_height="50px" />  
</LinearLayout>  

Java:

class MyWidget extends ViewGroup {
...

//In constructor  
myView = View.inflate(context, R.layout.xml, null);  
addView(myView);  

//In layout  
myView.layout(l, t, r, b);  

I have tried to give my text view fill_parent values for size but it didn't help.

David Snabel-Caunt
  • 57,804
  • 13
  • 114
  • 132
MrFroYo
  • 321
  • 1
  • 3
  • 4
  • have you tried in Hierarchy Viewer to force a layout request? if it makes the textview show up, then you're probably facing a framework bug (that should be solved in API > 8 AFAIK). – bigstones Apr 28 '11 at 14:42
  • No I didn't know I could do that. Will try when I get back later. I did however try a forceLayout() from within my widget a few times and that didn't help. – MrFroYo Apr 28 '11 at 15:23
  • Maybe you should create LinearLayout.LayoutParams(int width, int height) and set it to your TextView? – Anton Derevyanko Jul 11 '11 at 13:24
  • I guess, you have to call measure(...,...) – Damian Kołakowski Jul 12 '11 at 13:52
  • 2
    The TextView `layout_width` is missing a `d`. That probably doesn't help. – thegrinner Jul 12 '11 at 14:31
  • @thegrinner - That's probably just a typo in the posting; I think the project wouldn't build if that were in the actual source. – Ted Hopp Jul 13 '11 at 01:01
  • @Ted That's true, but I figured I'd at least point it out on the off chance that it had been suppressed and ignored somehow. – thegrinner Jul 13 '11 at 13:48

4 Answers4

116

Remember:getHeight() and getWidth()return 0 if components are not drawn yet.


To find the width And height of a View before it being drawn:

  1. First call measure

    view.measure(MeasureSpec.UNSPECIFIED,MeasureSpec.UNSPECIFIED)
    
  2. Now you can get width using getMeasuredWidth and height using getMeasuredHeight

    int width = view.getMeasuredWidth();
    int height = view.getMeasuredHeight();
    

I have posted some more ideas here: How to get width/height of a View

d0n.key
  • 1,318
  • 2
  • 18
  • 39
Sherif elKhatib
  • 45,786
  • 16
  • 89
  • 106
4

1) Here is some links to use Hierarchy Viewer on your dev phone.

http://developer.android.com/guide/developing/debugging/debugging-ui.html

and the class you'll need:

http://github.com/romainguy/ViewServer

2) You can also reuse layout like a component with the include tag:

<include android:id="@+id/your_id" layout="@layout/layout_name" />
plus-
  • 45,453
  • 15
  • 60
  • 73
1

So, I put a bounty on this one, and here is what I've found.

Inflating with a null reference is A Bad Idea(TM). Essentially, that View won't get the proper layout parameters it needs (its parent sets a whole bunch of them, with a whole bunch of magic/logic involved). So inflating into null means no parents, and no inherited layout parameters. One can manually set a number of these parameters, but due to the magic involved it might not solve your problem.

The "solution(s)" that I've come up with involve; using include (when you know how many you need) and pulling them into code, or inflating to a parent (when you need true dynamic, N things). And of course, the XML you inflate will have ID collisions, so I go about it by grabbing the last child (e.g. getChildAt(getChildCount()-1) ) of whatever I'm looking for, etc.

JohnO
  • 1,889
  • 1
  • 12
  • 15
0

Did you try passing yourself as the root:

View.inflate(context, R.layout.xml, this); 

Since you will be the parent of this View that complies with the javadoc spec.

Femi
  • 64,273
  • 8
  • 118
  • 148
  • Be sure to remove the `addView(view)` line or you'll get a runtime exception for attempting to assign a view to another parent. – Jason Robinson Jul 12 '11 at 17:14