adfly

Sunday, 31 May 2015

Android Set Click Event


ANDROID XML file 

Test.xml

<?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:text="CLICK"
        android:id="@+id/Button1" android:layout_width="fill_parent"
        android:layout_height="wrap_content"></Button>
</LinearLayout>


Test.Java

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.Toast;



public class Main extends Activity implements View.OnClickListener {
Button btn1

  @Override

  public void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

      setContentView(R.layout.main);
       
     Button btn1 = (Button)findViewById(R.id.Button1);

      btn1.setOnClickListener(btn1Listener);


    }

//this is inner class for button

  private View.OnClickListener btn1Listener = new View.OnClickListener() {

      @Override

      public void onClick(View v) {

        showToastMessage("You clicked btn1 - uses an inner class named btn1Listener");

      }

  };

}

No comments:

Post a Comment