-2

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.

FreshMeat
  • 93
  • 7
  • This was answered here: http://stackoverflow.com/questions/4186021/how-to-start-a-new-activity-when-click-on-button – Jimtronic Oct 12 '11 at 03:20

1 Answers1

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
  • 1
    replace `context` with `this` or just call findViewById() without it as it should already be in scope – Moog Oct 12 '11 at 14:23