-3

I am trying to make it so that when the user presses a button on the default activity, it opens a new activity.

Here's what I've got.

The code for the button:

android:onClick="openmenu"

The code for the "openmenu" method:

public void openmenu(View view) {
    Intent intent = new Intent(this , MainMenuPassed.class);
    startActivity(intent);
} 

Cheers guys!

mopsled
  • 8,445
  • 1
  • 38
  • 40
user3166793
  • 27
  • 2
  • 3
  • 8

1 Answers1

1

You can do workaround like :

Define an Id for your button in xml layout

 <Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button Text"
  />

Now in your Activity Class

 public class MyActivity extends Activity implements View.OnClickListener{



   @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_layout);
    final Button button =(Button) findVieById(R.id.button);
    button.setOnClickListener(this);

   }

   @Override
   protected void onClick(View view) {

     switch(v.getId){
       case R.id.button :
          Intent intent = new Intent(this , MainMenuPassed.class);
          startActivity(intent);
       break;

     }
  }

 }
Piyush Agarwal
  • 25,608
  • 8
  • 98
  • 111