Android various definition of OnClickListeners.

  • Download Gist
ActivityImplementsOnClickListener.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
public class AndroidOnClickDefinitionInXMLActivity extends Activity implements OnClickListener {
private Button buttonWithActivityAsListener;
 
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonWithActivityAsListener = (Button) findViewById(R.id.button_with_activity_as_listener);
buttonWithActivityAsListener.setOnClickListener(this);
}
/** You implement this method after adding OnClickListener interface to your Activity. **/
public void onClick(View v) {
Toast.makeText(this, "Button with Activity as Listener has been clicked.", 2500).show();
}
//
 
}
AndroidInlineClassAsOnCLickListener.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
public class AndroidOnClickDefinitionInXMLActivity extends Activity {
private Button buttonWithInlineAsListener;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
 
buttonWithInlineAsListener = (Button) findViewById(R.id.button_with_inline_class_as_listener);
buttonWithInlineAsListener.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(AndroidOnClickDefinitionInXMLActivity.this, "Button with Inline Class as Listener has been clicked.", 2500).show();
}
});
}
}
AndroidInnerClassAsOnClickListener.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
public class AndroidOnClickDefinitionInXMLActivity extends Activity {
private Button buttonWithInnerClassAsListener;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonWithInnerClassAsListener = (Button) findViewById(R.id.button_with_inner_class_as_listener);
buttonWithInnerClassAsListener.setOnClickListener(new InnerOnClickButtonListener());
}
 
/** Inner Class to respond to OnClick events. **/
class InnerOnClickButtonListener implements OnClickListener {
public void onClick(View v) {
Toast.makeText(AndroidOnClickDefinitionInXMLActivity.this, "Button with Inner Class as Listener has been clicked.", 2500).show();
}
}
}
AndroidOnClickAsAction.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
public class AndroidOnClickDefinitionInXMLActivity extends Activity {
 
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
/** Name of the method and it's triggering comes from main.xml file.
* Button's widget attribute android:onClick allows you to specify name of the method
* which you have to implement. **/
public void onButtonClick(View v) {
Toast.makeText(this, "Button Without a Listener has been clicked.", 2500).show();
}
//
 
}
AndroidOnClickListenersTogether.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
package com.smartcloudcompany.onclickinxml;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
 
public class AndroidOnClickDefinitionInXMLActivity extends Activity implements OnClickListener {
private Button buttonWithActivityAsListener;
private Button buttonWithInnerClassAsListener;
private Button buttonWithInlineAsListener;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonWithActivityAsListener = (Button) findViewById(R.id.button_with_activity_as_listener);
buttonWithActivityAsListener.setOnClickListener(this);
buttonWithInnerClassAsListener = (Button) findViewById(R.id.button_with_inner_class_as_listener);
buttonWithInnerClassAsListener.setOnClickListener(new InnerOnClickButtonListener());
buttonWithInlineAsListener = (Button) findViewById(R.id.button_with_inline_class_as_listener);
buttonWithInlineAsListener.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(AndroidOnClickDefinitionInXMLActivity.this, "Button with Inline Class as Listener has been clicked.", 2500).show();
}
});
}
/** You implement this method after adding OnClickListener interface to your Activity. **/
public void onClick(View v) {
Toast.makeText(this, "Button with Activity as Listener has been clicked.", 2500).show();
}
//
 
/** Name of the method and it's triggering comes from main.xml file.
* Button's widget attribute android:onClick allows you to specify name of the method
* which you have to implement. **/
public void onButtonClick(View v) {
Toast.makeText(this, "Button Without a Listener has been clicked.", 2500).show();
}
//
/** Inner Class to respond to OnClick events. **/
class InnerOnClickButtonListener implements OnClickListener {
public void onClick(View v) {
Toast.makeText(AndroidOnClickDefinitionInXMLActivity.this, "Button with Inner Class as Listener has been clicked.", 2500).show();
}
}
}
on_click_activity_layout.xml
Liquid
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
 
<Button android:id="@+id/button_with_activity_as_listener"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button with Activity As Listener"
android:layout_gravity="center_horizontal"
android:layout_marginTop="100dp"
/>
 
<Button android:id="@+id/button_without_listener"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button Without listener"
android:layout_gravity="center_horizontal"
android:onClick="onButtonClick"
/>
 
<Button android:id="@+id/button_with_inner_class_as_listener"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button With Inner Class As Listener"
android:layout_gravity="center_horizontal"
/>
 
<Button android:id="@+id/button_with_inline_class_as_listener"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button With Inline Class As Listener"
android:layout_gravity="center_horizontal"
/>
 
</LinearLayout>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.