Event Handling in Android

Events In Android

It is a very simple Event handler program that is performed in Android.This program consists of two buttons and Text View in which two different methods are called. The first method is on click method that is provided by android studio another one is custom declared method behave same as the first method but I am here, I want to explain how to declare your own method in Android. The second method is text change method, this two method is added to setOnClickListener() and it is invoked by onclickListener(). Program is written in java which inbuilt feature of Android 

                         Event Handling in Android

This is my home view of event handler with the button

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

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mapsample.msclient009.buttonevent">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
Activity_Main.xml
<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.mapsample.msclient009.buttonevent.MainActivity">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView"
        android:layout_width="251dp"
        android:layout_height="23dp"
        android:text="Content_Change"
        android:width="300dp"
        tools:layout_editor_absoluteX="72dp"
        tools:layout_editor_absoluteY="125dp"
        android:textColor="#FF00EE" />

    <Button
        android:id="@+id/Click"
        android:layout_width="220dp"
        android:layout_height="wrap_content"
        android:text="PressButton"
        tools:layout_editor_absoluteX="83dp"
        tools:layout_editor_absoluteY="194dp" />
    <Button
        android:id="@+id/change"
        android:layout_width="220dp"
        android:layout_height="wrap_content"
        android:text="getButton"
        tools:layout_editor_absoluteX="83dp"
        tools:layout_editor_absoluteY="194dp"
        android:onClick="result"/>

</LinearLayout>
</android.support.constraint.ConstraintLayout>
Activity_Main.java
package com.mapsample.msclient009.buttonevent;
import android.content.Intent;
import android.graphics.Color;
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 {
 Button btn,btn2;
 TextView txt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = (Button)findViewById(R.id.Click);
        btn2=(Button)findViewById(R.id.change);
        btn.setOnClickListener(new Button.OnClickListener(){
               public void onClick(View v)
               {txt =(TextView)findViewById(R.id.textView);
                  txt.setText("Now the Text is...");
               }
        });
        btn2.setOnClickListener(new Button.OnClickListener(){
           public void onClick(View view)
           {
                 result(view);
           }
        });
    }
    public void result(View view) {
        txt.setText("This is new method......");
    }
}


Last updated:9/7/2019 12:17:53 AM

0 Comments

Leave Comment