There are two xml files in my project,”main.xml” and “newn.xml”. Button with id button1 in main.xml and button2 in newn.xml.
I would like to click the button1 and go to newn layout file and come back to main layout when I click button2.
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
setContentView(R.layout.newn);
}
});
Button b1 = (Button) findViewById(R.id.button2);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
setContentView(R.layout.main);
}
});
but there was a run time error.
You cannot simply switch layouts in this manner.
If you want a new layout to show you need to use Fragments, dialogs, or start a new Activity.
Follow this tutorial to learn how to start a new activity.
http://developer.android.com/training/basics/firstapp/starting-activity.html