Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 9th, 2012  |  syntax: None  |  size: 1.34 KB  |  hits: 32  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Button Click Event on Android
  2. <Button
  3. android:id="@+id/btnOK"
  4. android:layout_width="wrap_content"
  5. android:layout_height="wrap_content"
  6. android:text="Display Message"
  7. android:onClick="onBtnClicked" />
  8.        
  9. package com.example.helloandroid;
  10.  
  11. import android.app.Activity;
  12. import android.content.DialogInterface;
  13. import android.content.DialogInterface.OnClickListener;
  14. import android.os.Bundle;
  15. import android.view.View;
  16. import android.widget.Button;
  17. import android.widget.Toast;
  18.  
  19. public class HelloAndroid extends Activity
  20. {
  21.     /** Called when the activity is first created. */
  22.     @Override
  23.     public void onCreate(Bundle savedInstanceState)
  24.     {
  25.         super.onCreate(savedInstanceState);
  26.         setContentView(R.layout.main);        
  27.     }  
  28.  
  29.     public void onBtnClicked(View v)
  30.     {
  31.         if(v.getId() == R.id.btnOK)
  32.         {
  33.             MessageBox("Hello World");
  34.         }      
  35.     }
  36.  
  37.     public void MessageBox(String message)
  38.     {
  39.        Toast.makeText(this, message, Toast.LENGTH_SHORT);
  40.     }  
  41. }
  42.        
  43. Toast.makeText(this, message, Toast.LENGTH_SHORT);
  44.        
  45. Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
  46.        
  47. Button b = (Button) findViewById(R.id.btnOK);
  48. b.setOnClickListener(this);
  49.        
  50. public void onClick(View v) {
  51.     switch(v.getId()){
  52.     case R.id.btnOK:
  53.         /*  the instruccions of the button */
  54.         break;
  55.     }
  56. }