Full tutorial is given below. Dear Viewer we are working hard to maintain this website. We doesn't want any think from you. Its just a humble request If you thinks that this posts helps you please share this post with your friends.
Create a button and handle on click listener in Android Studio
activity_main.xml
<?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:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" tools:context=".MainActivity"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Cick Button" /> </LinearLayout>
MainActivity.java
package com.usmtip.myapplication; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //initialize Button mButton = (Button)findViewById(R.id.button); //handle onClick mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //display toast you can do anything you want onClick here Toast.makeText(MainActivity.this, "Button is Clicked", Toast.LENGTH_SHORT).show(); } }); } }
0 Comments: