How can I launch another layout when button is pressed? I followed some of the web's solution, but it doesn't seem to help me out.
Asked
Active
Viewed 251 times
1 Answers
1
Well, if you simply want to change the view for the entire Activity, you can do setContentView(R.layout.newLayout)
but that is a really bad way to do it.
What you should do instead is startActivity(new Intent(context, NewActivity.class));
You would do that like this:
Button button = (Button)context.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Put the necessary methods here.
}
}
Though in the future, you really should try to do more research before asking a question like this. I had the same question when I was new to Android, but Google and StackOverflow were able to resolve it by searching for the answer. The Developer's Guide also helps if you peruse it enough.

Reed
- 14,703
- 8
- 66
- 110
-
thankyou so much for this info, was searching for it but unable to get the right tags. – FreshMeat Oct 12 '11 at 05:45
-
right now, using the way you gave, i am facing one error. The method findViewById(int) is undefined for the type Context – FreshMeat Oct 12 '11 at 05:49
-
1replace `context` with `this` or just call findViewById() without it as it should already be in scope – Moog Oct 12 '11 at 14:23