How to Open a Second Activity on Click of Button in Android App

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>

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 get my button to open a second activity in Android Studio?

1.
First you need to make buttonAbout1 method public.

2.And then you need to pass View as a parameters.Like this

public void buttonAbout1(View v) {
Button buttonAbout1 = (Button) findViewById(R.id.buttonAbout1);
assert buttonAbout1 != null;
buttonAbout1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(GMOEd.this,About2.class));
}
});
}

Here is a quick tip
Avoid making onClick method by yourself.Instead try using android studio's intention action(ALT+ENTER) to generate for you.When you add android:onClick="buttonAbout1" in the xml ,then press ALT+ENTER(make sure your cursor located on onClick) and then choose Create 'buttonAbout1(View)' in GMOEd and that will create the method in your activity.

Hope this help!

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();
}
});

Open new activity by Clicking button in a Fragment

getSupportActionBar().hide(); is what's causing the error. You have added it before

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);

So it's trying to execute it before the activity is initialised.

Add it after the above 2 lines and it should work.

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.



Related Topics



Leave a reply



Submit