-4

how can I order a button in an activity to open another activity not by writing codes and only by using properties menu or right-clicking on the button(pointed in the picture)image. also I create a new activity(.java file) in src file. Is it possible or I must write codes?

Kasi
  • 11
  • 3

4 Answers4

0

For calling other activity you need to write code i.e. Intent like this you can call another activity

Intent intent = new Intent(this,anotherActivity.class);
startActivity(intent);
Nikhil Sharma
  • 593
  • 7
  • 23
0

You must. But its 1 line of code only. Add this to your button's onClick

startActivity(new Intent(this, ActivityToOpen.class));
Hakan Saglam
  • 211
  • 2
  • 7
0

In your layout xml file inside button write attribute android:onClick

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

and inside Activity

public void YourMethodName(View v) {
// ButtonClickAction
 }

and this will call the method when button is clicked

how ever i would recommend to use Intent

Manohar
  • 22,116
  • 9
  • 108
  • 144
0

You have to write code, there is no escaping that. For opening another activity after clicking a button add the following on the OnClickListener of the button:

button.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View view) {
    Intent intent = new Intent(ActivityA.this, ActivityB.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
    finish();
   }
});

Remember to add the next activity within your AndroidManifest.xml file as follows or the activity won't run:

<activity android:name=".ActivityB"
 android:screenOrientation="portrait"
 android:theme="@style/AppTheme.NoActionBar.BlackActionBar" />
Tania Sekhon
  • 13
  • 1
  • 2