How do I get a button to open another activity?
android, java, xml
Solution
A. Make sure your other activity is declared in manifest:
<activity
android:name="MyOtherActivity"
android:label="@string/app_name">
</activity>
All activities must be declared in manifest, even if they do not have an intent filter assigned to them.
B. In your MainActivity do something like this:
Button btn = (Button)findViewById(R.id.open_activity_button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, MyOtherActivity.class));
}
});
Problem
I've added a button to my activity XML file and I can't get it to open my other activity. Can some please tell me step by step on how to do this?