---
title: "Event Handling in Android"  
description: "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"  
author: "Nitish Kesarwani"  
published: 2017-12-15  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/12724/event-handling-in-android  
category: "android apps"  
tags: ["java", "handler", "onclicklistener"]  
reading_time: 3 minutes  

---

# Event Handling in Android

##### Events In Android

It is a very simple [Event handler](https://www.mindstick.com/forum/1371/c-sharp-dot-net-event-handler-delegate-question) [program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program) that is performed in Android.This program consists of two buttons and [Text View](https://www.mindstick.com/forum/33384/detail-text-view-of-cell-not-showing) in which two different [methods](https://www.mindstick.com/articles/13060/runny-nose-remedy-methods-that-work-best) are called. The first method is on click method that is provided by [android studio](https://www.mindstick.com/blog/10942/android-studio-vs-eclipse) another one is custom declared method behave same as the first method but I am here, I want to [explain](https://www.mindstick.com/forum/159699/what-is-a-compilation-error-in-dot-net-core-explain-with-an-example) how to [declare your](https://answers.mindstick.com/qa/32729/where-will-you-declare-your-activity-so-the-system-can-access-it) own method in Android. The second method is text change method, this two method is added to setOnClickListener() and it is invoked by [onclickListener](https://www.mindstick.com/forum/33726/inside-onclicklistener-i-cannot-access-a-lot-of-things-how-to-approach)(). Program is written in java which inbuilt [feature](https://www.mindstick.com/forum/541/what-is-new-feature-in-sql-server-2012) of Android

![Event Handling in Android](https://www.mindstick.com/mindstickarticle/2e94c799-6e02-4a24-af78-c2f192051ae6/images/7ef46aab-f302-47c8-a98d-b8cd4ee13fa3.png)

###### 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......");    }}
```

\

---

Original Source: https://www.mindstick.com/articles/12724/event-handling-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
