Breaking News
Home / Android / Android Snippets / How to implement Button OnClickListener in Android?

How to implement Button OnClickListener in Android?

Snippet

The following snippet shows how to implement OnClickListner for a ImageButton in an Android Activity.

  1. package com.livrona.apps.radio.activity;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.ImageButton;
  8.  
  9. import com.livrona.andriod.commons.utils.Log;
  10.  
  11. public class AboutScreen extends Activity {
  12.  
  13.         @Override
  14.         public void onCreate(Bundle savedInstanceState) {
  15.  
  16.         Log.d("in : onCreate(..)");
  17.         super.onCreate(savedInstanceState);
  18.         setContentView(R.layout.about_screen);
  19.  
  20.         // find it             
  21.         ImageButton buttonWebsite=(ImageButton)findViewById(R.id.button_website);
  22.  
  23.         // Add the on click listener
  24.         buttonWebsite.setOnClickListener(new View.OnClickListener() {
  25.         public void onClick(View view) {
  26.         Log.d("in : Button Website onClick(..)");
  27.  
  28.          // write your action code here
  29.         }
  30.         }); // listener
  31.  
  32.       } // method
  33.  
  34. } // class
[pb_builder]

Check Also

How to check if an Android device can connect to Internet?

The following code snippet shows how to check if an Android device can connect to …

Advertisment ad adsense adlogger