Skip to content Skip to sidebar Skip to footer

Changing The Contents Of The Same Java Activity By Pressing The Button

When I run the following code, first I see a red rectangle. But I want to see a green rectangle first, and then when I press the button, next see a red rectangle in the same activi

Solution 1:

Draw2 is just a view not an activity,so Change MainActivity like this

public class MainActivity extends Activity {
     Button button;
     Draw draw;
     Draw2 draw2;
     RelativeLayout linearLayout;
     ViewGroup.LayoutParams layoutParams;

public void onCreate(Bundle s) {
    super.onCreate(s);
    setContentView(R.layout.activity_main);

    linearLayout = (RelativeLayout) findViewById(R.id.container);
    button = (Button) findViewById(R.id.button1);

    draw = new Draw(this);

    layoutParams = new ViewGroup.LayoutParams(200, 200);
    draw.setLayoutParams(layoutParams);

    linearLayout.addView(draw);

    button.setOnClickListener(new View.OnClickListener() {


        public void onClick(View v) {
            draw2 = new Draw2(getApplicationContext());
            draw2.setLayoutParams(layoutParams);
            linearLayout.addView(draw2);
        }
    });

}

}


Post a Comment for "Changing The Contents Of The Same Java Activity By Pressing The Button"