title: Button Click Event on Android
body: <p>This is going to be a real noob question, so please have mercy. I am trying to create a message box on a button click event in Android. I have read some examples on StackOverflow, but I can't seems to grasp the concept. In my main.xml file, I have defined the button xml as follows:</p>
  
  <pre><code>&lt;Button
  android:id="@+id/btnOK"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Display Message"
  android:onClick="onBtnClicked" /&gt;
  </code></pre>
  
  <p>I read on one of the posts that I need to register the onClick event in the XML layout. So that is what I thought I did in the XML code above. Then, in my java code file, I have written the following code:</p>
  
  <pre><code>package com.example.helloandroid;
  
  import android.app.Activity;
  import android.content.DialogInterface;
  import android.content.DialogInterface.OnClickListener;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  import android.widget.Toast;
  
  public class HelloAndroid extends Activity 
  {
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) 
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);        
      }   
  
      public void onBtnClicked(View v)
      { 
          if(v.getId() == R.id.btnOK)
          {
              MessageBox("Hello World");
          }       
      }
  
      public void MessageBox(String message)
      {
         Toast.makeText(this, message, Toast.LENGTH_SHORT);
      }   
  }
  </code></pre>
  
  <p>To me, this makes sense. But the message box does not display when I click on the button. From the code imports above, you can see that I have already tried a few solutions without success. Am I perhaps missing a listener? I thought that the definition in the XML code would create this for me?</p>
  
  <p>Thanks in advance :-)</p>