1

I have a relative layout that has an ImageView and and a textview aligned to right of the ImageView.

I have a class that inflates this relative layout and adds in itself (as if it is that layout).

Initially textview's visiblity is set to GONE, hence the size of the view is just that of the ImageView. Now, when the view receives focus, I set the visibility of textview to VISIBLE and set an animation to incrementally resize the view from imageView's width to (approximately)imageview+textview width (I am actually giving hard coded value in the program).

My question is, how can i determine the total width of the layout after i have enabled/diabled the visibility of the textview(since textview's width can be variable), so that i can specify that value to the animation as the final endpoint?

I had like to find this out right after i set the visiblity because if there is a delay in enabling the visiblity and starting the animation, the output is like the view with complete width (imageview+textview) is displayed first, then goes back to imageview size(start of animation), and then goes on to imageview+textview size(end of animation). If these is no way to find it out at that time, then can anyone please suggesst me to manually calculate what the final size could be?

----EDIT----

For the effect I wanted to achieve, there didn't seem to be any way. Final dimension of a view can be computed only after layout for that view is done by Android. The final dimension cannot be computed right after you enable the visiblity of a child view

Hence for my solution, I kept the parent layout (Relative layout) set to fill_parent for the width, had another child layout (background) whose size I could control through program, and then on top of this I have my imageView (Parent Left aligned) + textView (right of imageView, fill_parent).

So when the view is created for the first time, parent layout is always fill_parent, background layout has size of the imageview only and set textview's visiblity to invisible. And when a text is added to the text view, few milliseconds later (Using countdowntimer), I fetched the final dimension, set the animation to scale the size from imageView's width to imageView+textView width, and after end of animation set the visiblity of text view to visible.

Am not sure if my solution is the best but it seems to work properly without any glitches.

Puneet
  • 565
  • 4
  • 9
  • 21

2 Answers2

0

set your RelativeLayout width value as "wrap_content" so whatever the content have the size that much of size will be set to the RelativeLayout.

<RelativeLayout
    android:layout_width="wrap_layout" android:layout_height="wrap_content">
    <ImageView
    android:layout_width="wrap_layout" android:layout_height="wrap_content" />
    <TextView
    android:layout_width="wrap_layout" android:layout_height="wrap_content" />
</RelativeLayout>

no need to set with hard code value

Pratik
  • 30,639
  • 18
  • 84
  • 159
  • I have the xml file exactly like you have suggessted, and it does return me the width. However, i need the width after i enable textview's visibility and before view's layout is done. I have added a comment to reply by gnclmorais. Maybe you can get some idea and can better help me. – Puneet Dec 16 '11 at 12:23
0

If I understood your question right, using getWidth() and getHeight() in the view you want will return the dimensions you seek.

gnclmorais
  • 4,897
  • 5
  • 30
  • 41
  • It does give me the dimensions, however i want the updated width. For ex. if image view width is 130px and textview width is 270px, initially it will return me 130px (since textview visibility is GONE), and after setting text view visiblity to visible, i expect getWidth to return 400, however it still returns me 130px. getWidth reflects 400 only after view's layout is done. I want a way to find out the total width before view's layout is done so that animation can work properly without any flickers. – Puneet Dec 16 '11 at 12:21
  • Have you tried this link? Seems like it is what you want (sorry I don't have time to try it out for you): http://stackoverflow.com/questions/4074937/android-how-to-get-a-custom-views-height-and-width – gnclmorais Dec 16 '11 at 12:37