1

I want to change the size of MyView by pressing a button.

The initial size of MyView is 400 * 400, every time I press button b1, w adds 100. I suppose the view size should change, but it remains 400 * 400. However, if I change the size of the button b2 in the ClickListener, (add line a and line b), both the size of b2 and MyView change size. I don't know how this happens. If I just want to change the size of MyView dynamically, what should I do?

Relevant code:

public class MyView extends RelativeLayout {

Button b1;
Button b2;
Context sContext;
public static int i = 0;
private int w = 400;
private int h = 400;
private int w2 = 100;
private int h2 = 100;

public MyView(Context context) {
    super(context);
    sContext = context;
    init();
}

public MyView(Context context, AttributeSet attrs) {
    super(context, attrs);
    sContext = context;
    init();
}

private void init() {
    b1 = new Button(sContext);
    b2 = new Button(sContext);
    addView(b1);
    addView(b2);
    b1.setBackgroundColor(Color.YELLOW);
    b2.setX(500);

    b1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (MyView.this.getLayoutParams() == null) {
                MyView.this.setLayoutParams(new FrameLayout.LayoutParams(w, h));
            } else {
                MyView.this.getLayoutParams().width = w;
                MyView.this.getLayoutParams().height = h;
            }

                MyView.this.setBackgroundColor(Color.GREEN);


            //b2.setWidth(w2);          line a
            //b2.setHeight(h2);         line b

            //MyView.this.invalidate();
            w += 100;
            w2 += 20;
        }

    });
}

@Override
protected void onDraw(Canvas canvas) {
    //super.onDraw(canvas);
    test();
}

private void test() {
    b2.setBackgroundColor(Color.BLUE);
    b2.setX(200.0f);
    Toast.makeText(sContext, ""+i, Toast.LENGTH_SHORT).show();
    ++i;
}

}

JoshDM
  • 4,939
  • 7
  • 43
  • 72
darklord
  • 5,077
  • 12
  • 40
  • 65

2 Answers2

0

A couple of odd things I noticed

1) //MyView.this.invalidate() is commented out. You need to call either invalidate() or revalidate() when you change your layout.

2) I'm not sure why you're using MyView.this - try using just this. The only time I've seen Classname.this is when you're in an inner class and need to get to the outer one (see this). MyView is not an inner class.

3) //super.onDraw(canvas) is commented out. With java drawing/painting overrides you almost always want to call super to make sure children and parent containers get the message to redraw as well.

Community
  • 1
  • 1
sevensevens
  • 1,703
  • 16
  • 27
0

Try the below code

private void init() {
    b1 = new Button(sContext);
    b2 = new Button(sContext);
    addView(b1);
    addView(b2);
    b1.setBackgroundColor(Color.YELLOW);
    b2.setX(500);
    LayoutParams lp =getLayoutParams();
    if(lp==null){
       lp=new LayoutParams(w,h);
       setLayoutParams(lp);
    }
    lp.height=h;
    lp.width=w;
    requestLayout();

b1.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
            MyView.this.setBackgroundColor(Color.GREEN);
             w += 100;
            getLayoutParams().width=w;
            requestLayout();
    }

});
}
Sreejith B Naick
  • 1,203
  • 7
  • 13