How to Handle OnClick Listener using When keyword in Android?
In this article, we are going to see how can we apply the OnClick event to a button in android with the help of When Keyword, we can also apply the onClick event to a button in various other ways such as by setOnClickListener and findViewById, by ViewBinding but all these methods are taken a long time to implement and decrease our development speeds, so to overcome these problems we are going to handle OnClick Listener by the Help of When keyword and with the help of View. which makes it super easier and increases our development speed.
When Keyword: It is very similar to Switch Case in other languages, it executes a block of code when an specified condition is Satisfied .
To implement this first we have to Implement View.OnClickListener in your Activity or Fragment, you have to override the OnClick method on our class.
Step-By-Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. The Code is given in Kotlin language so make sure that you select Kotlin language for your project.
Step 2: Change the StatusBar Color
Navigate to app > res > values > themes > themes.xml and add the below code under the style section in the themes.xml file.
<item name="android:statusBarColor" tools:targetApi="l">#308d46</item>
Step 3: Working with activity_main.xml
Navigate to the app > res > layout > activity_main.xml and add the below code to the activity_main.xml file. Below is the code for the activity_main.xml file. The activity_main.xml represents the UI part of our application. It Includes two Buttons on which we are going to apply OnClick Listener and a TextView that displays which Button is clicked by the user.
XML
<? xml version = "1.0" encoding = "utf-8" ?>
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:orientation = "vertical"
tools:context = ".MainActivity" >
< TextView
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
tools:text = "Text"
android:id = "@+id/tv_display"
android:gravity = "center"
android:layout_gravity = "center"
android:layout_marginTop = "100dp"
android:textStyle = "bold"
android:textColor = "@color/black"
android:textSize = "20sp" />
< Button
android:layout_width = "180dp"
android:layout_height = "40dp"
android:text = "Button 1"
android:id = "@+id/btn1"
android:layout_gravity = "center"
android:layout_marginTop = "190dp" />
< Button
android:layout_width = "180dp"
android:layout_height = "40dp"
android:text = "Button 2"
android:id = "@+id/btn2"
android:layout_gravity = "center"
android:layout_marginTop = "50dp" />
</ LinearLayout >
|
Step 4: Working with MainActivity File
In this step, we are going to apply the OnClick listener to our two buttons with the help of the when keyword. At first, we have to implement View.OnClickListener to our MainActivity class.
class MainActivity : AppCompatActivity() , View.OnClickListener
Then we have to override the onClick method in our MainActivity class. Then in the onClick method, we are going to use our when keyword. Go to the MainActivity File (Navigate to app > java > YourPackageName > MainActivity) and follow the below code. Comments are added inside the code for a better understanding of the Code.
Kotlin
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
class MainActivity : AppCompatActivity() , View.OnClickListener {
var tv:TextView?= null
var btn1:Button?= null
var btn2:Button?= null
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
tv=findViewById(R.id.tv_display)
btn1=findViewById(R.id.btn1)
btn2=findViewById(R.id.btn2)
btn2?.setOnClickListener( this )
btn1?.setOnClickListener( this )
}
override fun onClick(view: View?) {
when(view!!.id){
R.id.btn1->{
Toast.makeText( this , "Button 1 is clicked" ,Toast.LENGTH_SHORT).show()
tv?.setText( "Button 1 is clicked" )
}
R.id.btn2->{
Toast.makeText( this , "Button 2 is clicked" ,Toast.LENGTH_SHORT).show()
tv?.setText( "Button 2 is Clicked" )
}
}
}
}
|
Java
import androidx.appcompat.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 implements View.OnClickListener {
private TextView tv;
private Button btn1;
private Button btn2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = findViewById(R.id.tv_display);
btn1 = findViewById(R.id.btn1);
btn2 = findViewById(R.id.btn2);
btn2.setOnClickListener( this );
btn1.setOnClickListener( this );
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn1:
Toast.makeText( this , "Button 1 is clicked" ,Toast.LENGTH_SHORT).show();
tv.setText( "Button 1 is clicked" );
break ;
case R.id.btn2:
Toast.makeText( this , "Button 2 is clicked" ,Toast.LENGTH_SHORT).show();
tv.setText( "Button 2 is Clicked" );
break ;
}
}
}
|
Output:
Last Updated :
27 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...