2

I am creating a module having a class (roundbutton) extending linear layout. It contains an image view and a textview.

When the roundbutton view is declared in the XML file, a layout_width and layout_height values are assigned. I want the imageview dimensions to be proportional to the layout dimensions assigned. Hence how to pass the layout dimensions to the view class so that I can assign the proportional dimensions to the image view?

P.S.: This question does not deal with passing custom attributes. But deals with passing the android:layout_width and android:layout_height to the custom UI Class.

suku
  • 10,507
  • 16
  • 75
  • 120
  • Possible duplicate of [Android - custom UI with custom attributes](http://stackoverflow.com/questions/7608464/android-custom-ui-with-custom-attributes) – R. Zagórski Sep 06 '16 at 06:57
  • 1
    This question is not a duplicate of that. I am not talking about custom attributes – suku Sep 06 '16 at 07:06

1 Answers1

0

I solved this by passing the instance of the view to the method that requires the values. And then use getViewTreeObserver().OnGlobalLayoutListener().

public void setIcon(final RoundedButton rb, final int drawableResourceId, final int position){
    mIconPosition = position;

    rb.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            buttonWidth = rb.getWidth();
            buttonHeight = rb.getHeight();

            //whatever needs to be done with the dimensions
        }
    });
}

You cannot do this.getViewTreeObserver().addOnGlobalLayoutListener() even if the class extends a type of view.

suku
  • 10,507
  • 16
  • 75
  • 120