Rami Masarweh
Rami Masarweh

Reputation: 1

How to check if my button is clicked in another activity?

I want to get text depending on which button in another layout in clicked (I have button 1 and button 2) I can't do a if clause statement because it doesn't recognize the buttons.

Upvotes: 0

Views: 4050

Answers (6)

user2558461
user2558461

Reputation: 281

If you just need to get the button of which you clicked on activity1 to get you to activity2 I would suggest you to do as follows: for Activity1 as the current activity and Activity2 as the calling activity.

  1. declare in Activity2
    public static final String EXTRA_IS_BUTTON_ONE = "isButtonOne". the use of a constant value allows you to guarentee the same String will be used thus making your code more readable and make less room for bugs. besides, saving some cycles according to Android Performance Guidelines

  2. create a new Intent to transfer to activity2 in activity 1->
    Intent intent = new Intent(Activity1.this,Activity2.class);

  3. create boolean is button one to put in the intent declaring which button clicked (button1 clicked --> true, button2 clicked --> false) ->
    intent.putExtra(Activity2.EXTRA_IS_BUTTON_ONE,isButtonOne);

  4. in Activity2 receive the intent and get the boolean ->
    boolean isButtonOne = getIntent().getBooleanExtra(EXTRA_IS_BUTTON_ONE,false);
    (the false is the default case if you didn't pass the extra to the activity).

  5. in Activity2 you may use the boolean to decide which text to

Hope I helped, feel free to ask questions if something ain't clear.

Upvotes: 1

Devrim
Devrim

Reputation: 15533

  1. If you have an Activity A(which has the buttons), Activity B and you are starting B from A:
    You can pass your selection at A with bundle while starting B:

    Intent intent = new Intent(A.this, B.class);
    intent.putExtra("selection_of_button_at_A", selectionOfButtonAtA);
    startActivity(intent);
    

    and get that value at B's onCreate method:

    getIntent().getStringExtra("selection_of_button_at_A");// sample for string 
    
  2. But if your B is not started from A, then you may store your selection at SharedPreferences(when a selection is done at A):

    SharedPreferences prefs =A.this.getSharedPreferences("com.your_app", Context.MODE_PRIVATE);
    prefs.edit().putString("selection_of_button_at_A", selectionOfButtonAtA).commit();
    

    now you can reach the state of your button clicks anywhere on your application.

    SharedPreferences prefs = B.this.getSharedPreferences("com.your_app", Context.MODE_PRIVATE);
    String selectionOfButtonAtA = prefs.getString("selection_of_button_at_A");
    

Upvotes: 2

Bala Vishnu
Bala Vishnu

Reputation: 2628

You could set different set some variable to different values(eg. if you click on button1 make its value as 1 or else 2) pass that integer variable as an intent to the previous activity and use GetExtras() to retrieve the integer variable...now use the IF condition to determine which button was clicked

public void onClick(View view) {
int code;

switch(view.getId()){
case R.id.button1:
 code=1;
break;

case R.id.button2:
 code=2;
break;
}
    Intent i = new Intent(this, ResultActivity.class);
    i.putExtra("yourcode", code);
    startActivityForResult(i, REQUEST_CODE);
  }

Now back to your old activity use

getIntent().getExtras("yourcode").toString(); to retrieve it

Upvotes: 3

N Jay
N Jay

Reputation: 1824

You cannot do an if clause off-course, but what you can do is use Intent when an activity is clicked and put a certain boolean in the intent extra.

Another way to do this is to send a broadcast from the first activity when a button is clicked and handle it in from wherever you want by a receiver.

The main Idea is that intents is your way to go.

Upvotes: 0

dumbfingers
dumbfingers

Reputation: 7089

In simple words:

Use startActivityForResult method.

In explicit explanation:

A launches B for result.

B handles click event and set the result, the result will goes back to A.

A receives the result.

For more detailed explanation and even example code snippet, please refer to Android Developers which provides a quite good example.

Upvotes: 0

Mikel
Mikel

Reputation: 1621

Use this. It refers to fragments but it's the same for activities. Basically you create an interface in one activity and call it in the other. As simple as that.

Upvotes: 0

Related Questions