0% found this document useful (0 votes)
15 views3 pages

EventHandler

The document contains an Android layout file and Java code for a simple application with multiple buttons and an EditText. The layout includes buttons for various actions, and the Java code implements click listeners for these buttons, displaying Toast messages upon interaction. The application demonstrates event handling in both the same and separate classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views3 pages

EventHandler

The document contains an Android layout file and Java code for a simple application with multiple buttons and an EditText. The layout includes buttons for various actions, and the Java code implements click listeners for these buttons, displaying Toast messages upon interaction. The application demonstrates event handling in both the same and separate classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

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

>
<LinearLayout
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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<Button
android:id="@+id/btn3"
android:layout_width="391dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:onClick="display"
android:text="FIRST PROGRAM"
android:textSize="50dp" />

<Button
android:id="@+id/btnShow"
android:layout_width="391dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="Annonymous Event"
android:textSize="50dp" />

<Button
android:id="@+id/btn1"
android:layout_width="391dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="Listener on Same Class"
android:textSize="50dp" />

<Button
android:id="@+id/btn2"
android:layout_width="391dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="Listener on Separate Class"
android:textSize="50dp" />

<EditText
android:id="@+id/et1"
android:layout_width="391dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:hint="Enter text"
android:gravity="center"
android:textSize="50dp" />

<TextView
android:id="@+id/tv1"
android:layout_width="391dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:hint="Enter text"
android:gravity="center"
android:textSize="50dp" />

</LinearLayout>

package com.example.androidapp1;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity implements View.OnClickListener


{

TextView tv1;
EditText et1;
Button btnShow;
Button btn1, btn2;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);

btnShow=(Button)findViewById(R.id.btnShow);

btn1=(Button)findViewById(R.id.btn1);

btn1.setOnClickListener(this);

tv1=(TextView)findViewById(R.id.tv1);
et1=(EditText) findViewById(R.id.et1);

btnShow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Clicked on
BUTTON",Toast.LENGTH_SHORT).show();
}
});
btn2=(Button)findViewById(R.id.btn2);
EventHandler eh=new EventHandler();
btn2.setOnClickListener(eh);
//btn2.setOnClickListener(new EventHandler());
}

public void display(View v)


{
Toast.makeText(this, "Clicked on TEXT VIEW",Toast.LENGTH_SHORT).show();
}

@Override
public void onClick(View view) {
Toast.makeText(this, "Clicked on SAME CLASS
BUTTON",Toast.LENGTH_SHORT).show();

String val=et1.getText().toString();

tv1.setText(val);
}
}

package com.example.androidapp1;

import android.view.View;
import android.widget.Toast;

public class EventHandler implements View.OnClickListener{


@Override
public void onClick(View view) {
Toast.makeText(view.getContext(), "Clicked on SEPARATE CLASS
BUTTON",Toast.LENGTH_SHORT).show();
//Toast.makeText(view.getContext(), "Clicked on SEPARATE CLASS
BUTTON",Toast.LENGTH_SHORT).show();
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy