Basic Onclicklistener

Posted on

It’s a real basic illustration to implement onClickListener for Button:
– Button A, define onClickListener as anonymous class.
– Button B1 in addition to B2: define onClickListener class.
– Button C1 in addition to C2: assign callback piece of employment inward 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="https://rootandroid.org//"         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 inward 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 inward 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;  world 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             world void onClick(View v) {                 Toast.makeText(MainActivity.this,                         "Button Influenza A virus subtype H5N1 Clicked: implement anonymous onClickListener",                         Toast.LENGTH_LONG).show();                 textInfo.setText(v.toString());             }         });          btnB1.setOnClickListener(btnBOnClickListener);         btnB2.setOnClickListener(btnBOnClickListener);     }      //set past times calling setOnClickListener()     View.OnClickListener btnBOnClickListener = novel View.OnClickListener() {         @Override         world 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 inward XML     world void onClickCallback(View v){         if(v.getId() == R.id.buttonc1){             Toast.makeText(MainActivity.this,                     "Button C1 Clicked: callback assign inward XML",                     Toast.LENGTH_LONG).show();         }else if((v.getId() == R.id.buttonc2)){             Toast.makeText(MainActivity.this,                     "Button C2 Clicked: callback assign inward XML",                     Toast.LENGTH_LONG).show();         }          textInfo.setText(v.toString());     }      //define inward XML     world void onBackgroundClickedCallback(View v){         Toast.makeText(MainActivity.this,                 "Background Clicked",                 Toast.LENGTH_LONG).show();         textInfo.setText(v.toString());     } }