How to Start New Activity on Button Click

How to start new activity on button click

Easy.

Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
myIntent.putExtra("key", value); //Optional parameters
CurrentActivity.this.startActivity(myIntent);

Extras are retrieved on the other side via:

@Override
protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
String value = intent.getStringExtra("key"); //if it's a string you stored.
}

Don't forget to add your new activity in the AndroidManifest.xml:

<activity android:label="@string/app_name" android:name="NextActivity"/>

How to open a second activity on click of button in android app

You can move to desired activity on button click. just add this line.

android:onClick="sendMessage"

xml:

 <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sendMessage"
android:text="@string/button" />

In your main activity just add this method:

public void sendMessage(View view) {
Intent intent = new Intent(FromActivity.this, ToActivity.class);
startActivity(intent);
}

And the most important thing: don't forget to define your activity in manifest.xml

 <activity>
android:name=".ToActivity"
android:label="@string/app_name">
</activity>

Passing Data to a new Activity on a Button Click

To pass data trhoug activities you can use the same intent you use to open the new activity. You can set extras like this:

Intent i = new Intent(context, CartActivity.class);
i.putExtra("price", textView6.getText().toString());
i.putExtra("name", textView7.getText().toString());
startActivity(i);

And then in onCreate() of the just created activity you can retrieve this data getting the intent used to open this activity and getting its extras:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Intent data = getIntent();
String price = data.getStringExtra("price");
String name = data.getStringExtra("name");
}

Hope this help.

How do i get a button to open a new activity in android studio?

Method 1


Use the onclick attribute in XML (so that whenever you click the button, defined method will trigger)

Step 1. - go to the XML where you button is(activity_home) & add

<Button
............
android:onClick="gotoTutorial"/>

Step 2. - then go to the home.Java & add following

public void gotoTutorial(View v){
Intent tutorialPage = new Intent (this, tutorials.class);
startActivity(tutorialPage);
}


Method 2

Use the setOnClickListener

Step 1. -

 //create the link to the button in the interface
btn_tutorial = (Button)findViewById(R.id.tutorial_button);

btn_tutorial.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent tutorialPage = new Intent (this, tutorials.class);
startActivity(tutorialPage);
}
}

Start another activity by clicking a button

You need to call another activity like this inside the OnClickListener() of button

Button mondayEdit= (Button)findViewById(R.id.button1);
mondayEdit.setOnClickListener(new OnClickListener()
{ public void onClick(View v)
{
Intent intent = new Intent(main.this, secondActivity.class);
startActivity(intent);
finish();
}
});

Opening New Activity in Second Activity

You have to use intent to open a new Activity. Assuming you want to open an activity called SixthActivity from your FifthActivity.

You should use this:

 public class FifthActivity extends Activity {

Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fifth_layout);
Button button = (Button) findViewById(R.id.button10);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(FifthActivity.this,SixthActivity.java);
FifthActivity.this.startActivity(intent);
}
});

}
}

Hope this helps,
Regards.

How to start new activity on button click event in custom adapter?

Make intent like

  Intent i=new Intent(_context,secondActivity.class);
_context.startActivity(i);

Remember you must register secondActivity in manifest.xml

and also you should change

 if(txtListChild.getText().toString()=="Geometry")

to

 if(txtListChild.getText().toString().equals("Geometry"))

always used .equals() method for string comparison.

Open a new activity after clicking the Button

Even if you are new in android programming, I would not encourage you in using of onClick from XML.

You should get a reference for the button with findViewById function, then you should set a listener on it.

It will look like

Button btnLogIn = (Button) findViewById(R.id.logIn);
btnLogIn.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
Intent intent = new Intent(Mainactivity.this, DisplayLogInActivity.class);
startActivity(intent);
}
});

If you are not passing intent data, you can use the shortened form:

startActivity(new Intent(MainActivity.this, DisplayLogInActivity.class););


Related Topics



Leave a reply



Submit