Wednesday, May 11, 2016

Basic onClickListener


It's a very basic example to implement onClickListener for Button:
- Button A, define onClickListener as anonymous class.
- Button B1 and B2: define onClickListener class.
- Button C1 and C2: assign callback function in XML.


activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/background"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:orientation="vertical"
    android:onClick="onBackgroundClickedCallback"
    tools:context="com.blogspot.android_er.androidclicklistener.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />

    <Button
        android:id="@+id/buttona"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button A: anonymous onClickListener"/>

    <Button
        android:id="@+id/buttonb1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button B1: onClickListener"/>

    <Button
        android:id="@+id/buttonb2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button B2: onClickListener"/>

    <Button
        android:id="@+id/buttonc1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClickCallback"
        android:text="Button C1: define callback in XML"/>

    <Button
        android:id="@+id/buttonc2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClickCallback"
        android:text="Button C2: define callback in XML"/>

    <TextView
        android:id="@+id/info"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="28dp"/>
</LinearLayout>


MainActivity.java
package com.blogspot.android_er.androidclicklistener;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button btnA, btnB1, btnB2, btnC1, btnC2;
    TextView textInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnA  = (Button)findViewById(R.id.buttona);
        btnB1 = (Button)findViewById(R.id.buttonb1);
        btnB2 = (Button)findViewById(R.id.buttonb2);
        btnC1 = (Button)findViewById(R.id.buttonc1);
        btnC2 = (Button)findViewById(R.id.buttonc2);
        textInfo = (TextView) findViewById(R.id.info);

        btnA.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,
                        "Button A Clicked: implement anonymous onClickListener",
                        Toast.LENGTH_LONG).show();
                textInfo.setText(v.toString());
            }
        });

        btnB1.setOnClickListener(btnBOnClickListener);
        btnB2.setOnClickListener(btnBOnClickListener);
    }

    //set by calling setOnClickListener()
    View.OnClickListener btnBOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(v.getId() == R.id.buttonb1){
                Toast.makeText(MainActivity.this,
                        "Button B1 Clicked: implement onClickListener",
                        Toast.LENGTH_LONG).show();
            }else if((v.getId() == R.id.buttonb2)){
                Toast.makeText(MainActivity.this,
                        "Button B2 Clicked: implement onClickListener",
                        Toast.LENGTH_LONG).show();
            }

            textInfo.setText(v.toString());
        }
    };

    //define in XML
    public void onClickCallback(View v){
        if(v.getId() == R.id.buttonc1){
            Toast.makeText(MainActivity.this,
                    "Button C1 Clicked: callback assign in XML",
                    Toast.LENGTH_LONG).show();
        }else if((v.getId() == R.id.buttonc2)){
            Toast.makeText(MainActivity.this,
                    "Button C2 Clicked: callback assign in XML",
                    Toast.LENGTH_LONG).show();
        }

        textInfo.setText(v.toString());
    }

    //define in XML
    public void onBackgroundClickedCallback(View v){
        Toast.makeText(MainActivity.this,
                "Background Clicked",
                Toast.LENGTH_LONG).show();
        textInfo.setText(v.toString());
    }
}


No comments: