Search code examples
androidandroid-buttonbuttonclick

can we fire up one button from another


Is there a way that we can fire up a button from another button in Android. Let's say when I click button1 to call the onClick() function of button2?

            <Button
                android:id="@+id/button1"
                android:layout_width="150dp"
                android:layout_height="wrap_content" />
            <Button
                 android:id="@+id/button2" 
                android:layout_width="140dp"
                android:layout_height="50dp"/>

Solution

  • In your onClick() of your first button, you can do

    public void onClick(View v) {
        btn2.performClick();
    }
    

    the performClick() method will fire btn2's onClick()

    Docs for performClick()