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

publicclassMainActivityextendsActivity {
     Button button;
     Draw draw;
     Draw2 draw2;
     RelativeLayout linearLayout;
     ViewGroup.LayoutParams layoutParams;

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

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

    draw = newDraw(this);

    layoutParams = newViewGroup.LayoutParams(200, 200);
    draw.setLayoutParams(layoutParams);

    linearLayout.addView(draw);

    button.setOnClickListener(newView.OnClickListener() {


        publicvoidonClick(View v) {
            draw2 = newDraw2(getApplicationContext());
            draw2.setLayoutParams(layoutParams);
            linearLayout.addView(draw2);
        }
    });

}

}

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