Android: how to change layout on button click?

Button btnDownload = (Button)findViewById(R.id. DownloadView); Button btnApp = (Button)findViewById(R.id. AppView); btnDownload.

SetOnClickListener(handler); btnApp. SetOnClickListener(handler); View. OnClickListener handler = new View.OnClickListener(){ public void onClick(View v) { if(v==btnDownload){ // doStuff Intent intentMain = new Intent(CurrentActivity.

This , SecondActivity. Class); CurrentActivity.this. StartActivity(intentMain); Log.

I("Content "," Main layout "); } if(v==btnApp){ // doStuff Intent intentApp = new Intent(CurrentActivity. This , ThirdActivity. Class); CurrentActivity.this.

StartActivity(intentApp); Log. I("Content "," App layout "); } } } Note : and then you should declare all your activities in the manifest like this : activity android:name="SecondActivity" > App); TextView tv = (TextView) this. FindViewById(R.id.

Thetext); tv. SetText("App View yo!? \n"); }.

Button btnDownload = (Button)findViewById(R.id. DownloadView); Button btnApp = (Button)findViewById(R.id. AppView); btnDownload.

SetOnClickListener(handler); btnApp. SetOnClickListener(handler); View. OnClickListener handler = new View.OnClickListener(){ public void onClick(View v) { if(v==btnDownload){ // doStuff Intent intentMain = new Intent(CurrentActivity.

This , SecondActivity. Class); CurrentActivity.this. StartActivity(intentMain); Log.

I("Content "," Main layout "); } if(v==btnApp){ // doStuff Intent intentApp = new Intent(CurrentActivity. This , ThirdActivity. Class); CurrentActivity.this.

StartActivity(intentApp); Log. I("Content "," App layout "); } } }; Note : and then you should declare all your activities in the manifest like this : EDIT : update this part of Code :) : @Override public void onCreate(Bundle savedInstanceState){ super. OnCreate(savedInstanceState);// Add THIS LINE setContentView(R.layout.

App); TextView tv = (TextView) this. FindViewById(R.id. Thetext); tv.

SetText("App View yo!? \n"); }.

This code has the same effect... – Kris May 25 at 9:17 @Kris : see my edit , and I think you should have the same buttons in the second layout with the same identifients , because when you will change the layout, the buttons are only in the first layout ,! – Houcine May 25 at 9:20 Thanks for your effort @Houcine, but I think it really needs a new activity... – Kris May 25 at 9:47 1 hi Kris , see my Edit :) , I think you have forgot the super.onCreate() ; – Houcine May 25 at 10:39 1 you are welcome Kris , we are here to help ;) – Houcine May 25 at 10:55.

I think what you're trying to do should be done with multiple Activities. If you're learning Android, understanding Activities is something you're going to have to tackle. Trying to write a whole app with just one Activity will end up being a lot more difficult.

Read this article to get yourself started, then you should end up with something more like this: View. OnClickListener handler = new View.OnClickListener(){ public void onClick(View v) { switch (v.getId()) { case R.id. DownloadView: // doStuff startActivity(new Intent(ThisActivity.

This, DownloadActivity. Class)); break; case R.id. AppView: // doStuff startActivity(new Intent(ThisActivity.

This, AppActivity. Class)); break; } } }; findViewById(R.id. DownloadView).

SetOnClickListener(handler); findViewById(R.id. AppView). SetOnClickListener(handler).

First I would suggest putting a Log in each case of your switch to be sure that your code is being called. Then I would check that the layouts are actually different.

Thanks for your response, I put a log and the code was called, yes the layouts are different. – Kris May 25 at 8:59.

I know I'm coming to this late, but what the heck. I've got almost the exact same code as Kris, using just one Activity but with 2 different layouts/views, and I want to switch between the layouts at will. As a test, I added 2 menu options, each one switches the view: public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.

Item1: setContentView(R.layout. Main); return true; case R.id. Item2: setContentView(R.layout.

Alternate); return true; default: return super. OnOptionsItemSelected(item); } } Note, I've got one Activity class. This works perfectly.So I have no idea why people are suggesting using different Activities / Intents.

Maybe someone can explain why my code works and Kris's didn't.

I've got almost the exact same code as Kris, using just one Activity but with 2 different layouts/views, and I want to switch between the layouts at will. Note, I've got one Activity class. This works perfectly.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions