Android - setOnClickListener Method, how does it work?

In this tutorial, we will learn how to work with setOnClickListener method in Android? By Manu Jemini Last updated : June 05, 2023

Android setOnClickListener() Method

One of the most usable methods in android is setOnClickListener method which helps us to link a listener with certain attributes.

setOnClickListener is a method in Android basically used with buttons, image buttons etc. You can initiate this method easily like,

Syntax

public void setOnClickListener(View.OnClickListner)

While invoking this method a callback function will run. One can also create a class for more than one listener, so this can lead you to code reusability.

After making the class you can implement android.view.View.OnClickListener{} method which gives you an override method inherited from super class called onClick(View v){} in which you can easily implement your code.

setOnClickListener() Method Example

Java file

package com.example.hp.demo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    EditText name;
    Button click;
    TextView result;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        name=(EditText)findViewById(R.id.name);
        click=(Button)findViewById(R.id.click);
        result=(TextView)findViewById(R.id.result);
        click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                result.setText("Hello "+name.getText());
            }
        });
    }
}

Xml File

<?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: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.hp.demo.MainActivity">

    <EditText
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:id="@+id/name"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:hint="Name" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click"
        android:id="@+id/click"
        android:layout_below="@+id/name"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:text="Result"
        android:id="@+id/result"
        android:layout_below="@+id/click"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

Manifest File

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hp.demo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        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>

Output

setOnClickListener method 1
setOnClickListener method 2


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.