Skip to content

Latest commit

 

History

History
52 lines (35 loc) · 1.11 KB

File metadata and controls

52 lines (35 loc) · 1.11 KB

Android click listener

Here we present two ways of handling clicks (taps) on buttons.

Button onclick

  • Using the XML layout to set the onClick

  • Adding the on setOnClickListener in the java file. See the code for the implementation:

Option one:

// Button One
        (findViewById(R.id.btn)).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // Your code here
                startActivity(new Intent(getBaseContext(), SecondActivity.class));
            }
        });

Option Two

  // Button two
    public void btnTwo(View view) {
        // Your code here
        startActivity(new Intent(getBaseContext(), SecondActivity.class));

    }

for this latter, you need set the button clickable android:onClick="btnTwo"

    <Button
        android:id="@+id/btn_two"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:onClick="btnTwo"
        android:text="Button two" />