android: how to change layout on button click?

前端 未结 8 1348
醉话见心
醉话见心 2020-12-05 00:35

I have to following code for selecting layout on button click.

View.OnClickListener handler = new View.OnClickListener(){
    public void onClick(View v) {

         


        
相关标签:
8条回答
  • 2020-12-05 01:10

    I would add an android:onClick to the layout and then change the layout in the activity.

    So in the layout

    <ImageView
    (Other things like source etc.)
    android:onClick="changelayout"
    />
    

    Then in the activity add the following:

    public void changelayout(View view){
        setContentView(R.layout.second_layout);
    }
    
    0 讨论(0)
  • 2020-12-05 01:18

    You wanted to change the layout at runtime on button click. But that is not possible and as it has been rightly stated above, you need to restart the activity. You will come across a similar problem when u plan on changing the theme based on user's selection but it will not reflect in runtime. You will have to restart the activity.

    0 讨论(0)
提交回复
热议问题