Skip to content Skip to sidebar Skip to footer

Android Button Onclicklistener

I am trying to open new Activity by clicking on a button in my OnClickListener method. How does OnClickListener method work and what should be done in it to start a new Activity?

Solution 1:

This task can be accomplished using one of the android's main building block named as Intents and One of the methods public void startActivity (Intent intent) which belongs to your Activity class.

An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service.

An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.

Refer the official docs -- http://developer.android.com/reference/android/content/Intent.html

public void startActivity (Intent intent) -- Used to launch a new activity.

So suppose you have two Activity class --

  1. PresentActivity -- This is your current activity from which you want to go the second activity.

  2. NextActivity -- This is your next Activity on which you want to move.

So the Intent would be like this

Intent(PresentActivity.this, NextActivity.class)

Finally this will be the complete code

publicclassPresentActivityextendsActivity {
  protectedvoidonCreate(Bundle icicle) {
    super.onCreate(icicle);

    setContentView(R.layout.content_layout_id);

    finalButtonbutton= (Button) findViewById(R.id.button_id);
    button.setOnClickListener(newView.OnClickListener() {
            publicvoidonClick(View v) {
              // Perform action on click   IntentactivityChangeIntent=newIntent(PresentActivity.this, NextActivity.class);

              // currentContext.startActivity(activityChangeIntent);

              PresentActivity.this.startActivity(activityChangeIntent);
            }
          });
  }
}

Solution 2:

//create a variable that contain your buttonButtonbutton= (Button) findViewById(R.id.button);

    button.setOnClickListener(newOnClickListener(){
        @Override//On click functionpublicvoidonClick(View view) {
            //Create the intent to start another activityIntentintent=newIntent(view.getContext(), AnotherActivity.class);
            startActivity(intent);
        }
    });

Solution 3:

Use OnClicklistener or you can use android:onClick="myMethod" in your button's xml code from which you going to open a new layout. So when that button is clicked your myMethod function will be called automatically. Your myMethod function in class look like this.

publicvoidmyMethod(View v){
Intent intent=newIntent(context,SecondActivty.class);
startActivity(intent);
}

And in that SecondActivity.class set new layout in contentview.

Solution 4:

easy:

launching activity (onclick handler)

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

on the new activity:

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
Intentintent= getIntent();
Stringvalue= intent.getStringExtra("key"); //if it's a string you stored.

and add your new activity in the AndroidManifest.xml:

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

Post a Comment for "Android Button Onclicklistener"