Sunday, February 26, 2017

Event Handling in Android @ Button

Dear All

Event - Its an Action happened in App.

Event Handling means how we deal with Action Happened like Button Click is an Event.

Event can be Managed by following three important steps.
1. Adding Event Listener - It's an Interface
2. Event Listener Registration with widget - Its a procedure like setOnClickListener()
3. Event Handler - It's response method like onClick(View view)

Just See below example with all these three steps.

package com.example.ravigodara.buttonevent;
import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

import android.widget.Toast;
public class MainActivity extends AppCompatActivity 
                               implements View.OnClickListener 
                                          // Adding Event Listener
{
   Button btn1 , btn2;
    TextView txt;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txt = (TextView) findViewById(R.id.text);
        btn1 = (Button) findViewById(R.id.butsum);
        btn2 = (Button) findViewById(R.id.butmul);
        btn1.setOnClickListener(this);    
                     // Listener Registration with Event Widget
        btn2.setOnClickListener(this);

    }
    @Override    public void onClick(View v) { 
                     // Event Handler Method which will be called  

        int id = v.getId();
        switch (id)
        {
            case R.id.butsum:
                Toast.makeText(this, "Button 1 Pressed", Toast.LENGTH_SHORT).show();
                txt.setText("Button 1 Pressed");
                break;
            case R.id.butmul:
                Toast.makeText(this, "Button 2 Pressed", Toast.LENGTH_SHORT).show();
                txt.setText("Button 2 Pressed");
                break;

        }

    }


}

B. Layout File

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/activity_main"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context="com.example.ravigodara.buttonevent.MainActivity">

    <TextView

        android:id="@+id/text"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Hello World!"

        android:layout_alignParentEnd="true"

        android:layout_alignParentStart="true" />

    <Button

        android:text="Sum"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_below="@+id/textView"

        android:layout_toEndOf="@+id/textView"

        android:layout_marginStart="43dp"

        android:layout_marginTop="111dp"

        android:id="@+id/butsum" />

    <Button        android:text="Mul"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerVertical="true"

        android:layout_alignEnd="@+id/button"

        android:id="@+id/butmul" />
</RelativeLayout>

C. Output will be like 

1. 


2.


3.