Home > Software design >  Is it possible to check if a button is clicked in a different page in Java?
Is it possible to check if a button is clicked in a different page in Java?

Time:03-12

I want to check if a button is clicked in the previous activity in Java. If 1st button is clicked, then it will proceed to 2nd page following function 'a'. If 2nd button is clicked, then it will proceed to 2nd page, following function 'b' and so on.

Basically, there will only be 2 main activities, 1st page and 2nd page. If any button is clicked in the first page it will go to the second page, but each button will execute different lines of codes(or functions) in the second page. Is it possible or what is the code for this?

CodePudding user response:

You can do it something like this:

First page:

btn1.setOnClickListener(v -> startActivity(new Intent(this,YoursecondactivityName.class).putExtra("click",1)));
btn2.setOnClickListener(v -> startActivity(new Intent(this,YoursecondactivityName.class).putExtra("click",2)));
btn3.setOnClickListener(v -> startActivity(new Intent(this,YoursecondactivityName.class).putExtra("click",3)));
btn4.setOnClickListener(v -> startActivity(new Intent(this,YoursecondactivityName.class).putExtra("click",4)));
btn5.setOnClickListener(v -> startActivity(new Intent(this,YoursecondactivityName.class).putExtra("click",5)));

Second page:

Now you have to get that what was clicked. In the on create add this:

switch(getIntent().getIntExtra("click",1)){
            case 1:
                Toast.makeText(this,"button 1 click",Toast.LENGTH_SHORT).show();
                break;
            case 2:
                Toast.makeText(this,"button 2 click",Toast.LENGTH_SHORT).show();
                break;
            case 3:
                Toast.makeText(this,"button 3 click",Toast.LENGTH_SHORT).show();
                break;
            case 4:
                Toast.makeText(this,"button 4 click",Toast.LENGTH_SHORT).show();
                break;
            case 5:
                Toast.makeText(this,"button 5 click",Toast.LENGTH_SHORT).show();
                break;
            default:
                Toast.makeText(this, "something went wrong", Toast.LENGTH_SHORT).show();
        }

Here from the first activity you pass which button was clicked using intent. Now in the second activity, you get it and then write the code which you want.

If you want my entire activity code, it is below

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn1 = findViewById(R.id.b1),btn2 = findViewById(R.id.b2),btn3 = findViewById(R.id.b3),btn4 = findViewById(R.id.b4),btn5 = findViewById(R.id.b5);
        btn1.setOnClickListener(v -> startActivity(new Intent(this,SecondActivity.class).putExtra("click",1)));
        btn2.setOnClickListener(v -> startActivity(new Intent(this,SecondActivity.class).putExtra("click",2)));
        btn3.setOnClickListener(v -> startActivity(new Intent(this,SecondActivity.class).putExtra("click",3)));
        btn4.setOnClickListener(v -> startActivity(new Intent(this,SecondActivity.class).putExtra("click",4)));
        btn5.setOnClickListener(v -> startActivity(new Intent(this,SecondActivity.class).putExtra("click",5)));
    }
}

And now my second activity

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.Toast;

public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        switch(getIntent().getIntExtra("click",1)){
            case 1:
                Toast.makeText(this,"button 1 click",Toast.LENGTH_SHORT).show();
                break;
            case 2:
                Toast.makeText(this,"button 2 click",Toast.LENGTH_SHORT).show();
                break;
            case 3:
                Toast.makeText(this,"button 3 click",Toast.LENGTH_SHORT).show();
                break;
            case 4:
                Toast.makeText(this,"button 4 click",Toast.LENGTH_SHORT).show();
                break;
            case 5:
                Toast.makeText(this,"button 5 click",Toast.LENGTH_SHORT).show();
                break;
            default:
                Toast.makeText(this, "something went wrong", Toast.LENGTH_SHORT).show();
        }
    }
}
  • Related