Search code examples
androiduiswitchandroid-viewgroup

Android Switch dosent show in ViewGroup


I want to create a Widget like this :

image 1

But, what is shown is this:

image 2

Why this Switch Button doesn't show in ViewGroup:

In this case just Switch's text "hello" is showing.

public class TestView extends ViewGroup {
   ...
   private void init() {
       imageView = new ImageView(getContext());
       imageView.setImageResource(R.drawable.clock_icon);

       aSwitch = new Switch(getContext());
       aSwitch.setText("hello");
       aSwitch.setChecked(true);

       addView(imageView);
       addView(aSwitch);

   }

   @Override
   protected void onSizeChanged(int w, int h, int oldw, int oldh) {
       super.onSizeChanged(w, h, oldw, oldh);
       imageView.layout(0, 50,100, 70);
       aSwitch.layout(50,50,100,70);
   }
...

Solution

  • tnx @Gugalo I solved that with your descriptions.

     @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
            aSwitch.measure(w,h);
            aSwitch.layout(0, 0,  aSwitch.getMeasuredWidth(), aSwitch.getMeasuredHeight());
        }