android: how to change layout on button click?-Collection of common programming errors
I have to following code for selecting layout on button click.
View.OnClickListener handler = new View.OnClickListener(){
public void onClick(View v) {
switch (v.getId()) {
case R.id.DownloadView:
// doStuff
setContentView(R.layout.main);
break;
case R.id.AppView:
// doStuff
setContentView(R.layout.app);
break;
}
}
};
findViewById(R.id.DownloadView).setOnClickListener(handler);
findViewById(R.id.AppView).setOnClickListener(handler);
When I click the “AppView” button, the layout changes, but when I click the “DownloadView “button, nothing happens.
This link says that I have to start a new activity.
But I don’t know how to use the code there of intent to start new activity, will a new file be added? I’m new to java and android dev, many thanks in advanced for any help! 🙂
EDIT: I have my code on the new activity:
package com.example.engagiasync;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class AppView extends Activity implements OnClickListener{
@Override
public void onCreate(Bundle savedInstanceState){
setContentView(R.layout.app);
TextView tv = (TextView) this.findViewById(R.id.thetext);
tv.setText("App View yo!?\n");
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
but it does not work, it force closes, the logcat says: