Saturday, June 14, 2014

Button Click Event in android

Here i share how to handle button click event in android.
In android to handle click event java class must implement interface "OnClickListener"
1 Create XML File "activity_main.xml" activity_main.xml
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">
         android:orientation="horizontal"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content">
               android:id="@+id/name"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_weight="1.0"
         android:lines="1" />
      
         android:id="@+id/web_view"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_weight="1.0" />


2 Create java class file "MainActivity.java" MainActivity.java
package com.bhansalisoft.testapp;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity implements OnClickListener {

 private EditText urlText;
   private Button goButton;

   @Override
   public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);

     // Get a handle to all user interface elements
     urlText = (EditText) findViewById(R.id.name);
     goButton = (Button) findViewById(R.id.go_button);
     goButton.setOnClickListener(this);
     
   }

  @Override
  public void onClick(View view) {
   //go button is clicked..
   if(view.getId()==R.id.go_button){
    Toast.makeText(getApplicationContext(),"Go Button clicked ...your name is :"+urlText.getText().toString(),Toast.LENGTH_LONG).show();
   }

  }
   
   
}


If you are searching life partner. your searching end with kpmarriage.com. now kpmarriage.com offer free matrimonial website which offer free message, free chat, free view contact information. so register here : kpmarriage.com- Free matrimonial website