In this tutorial you will learn about the FindViewById & On Click Listener and its application with practical example.
In Kotlin we do not need to fetch our component using Id, It depends upon you If you want to fetch them so you can. You can directly Access your xml component without using “findViewById()”.In this article we get know about On-click Listener and set text into component.
#Use of component without using “findViewByID()” So Now say good by to findViewById() and just write your logic with less verbosity.
1 2 3 4 5 6 7 8 9 10 11 12 |
import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) this.tv_hello.setText("Kotlin Welcome") } } |
#On-Click Listener: The below code will show you that how can you use On-click Listener, In Kotlin and in Java.
Kotlin:
1 2 3 4 5 6 |
this.btn_fir.setOnClickListener{ //this.tv_hello.setText("Button Clicked") Toast.makeText(this,"Clicked", Toast.LENGTH_SHORT).show() } } |
Java:
1 2 3 4 5 6 7 |
Button btn=(Button)findViewById(R.id.btn); btn_fir.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(this,"Clicked on Button", Toast.LENGTH_SHORT).show() } }); |
Example:Let get more understand it by this simple example.
#Main Activity:the below code I have used in MainActivity.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package com.w3adda.myapplication import android.os.Bundle import android.support.v7.app.AppCompatActivity import android.widget.Toast import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) this.btn_fir.setOnClickListener{ this.tv_hello.setText("Welcome to w3adda.com") Toast.makeText(this,"Clicked", Toast.LENGTH_SHORT).show() } } } |
Xml: The below xml is associated with MainActivity.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayoutxmlns: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" tools:context="com.w3adda.myapplication.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv_hello" android:textColor="#000" android:layout_centerHorizontal="true" android:layout_centerVertical="true" /> <Button android:id="@+id/btn_fir" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click" android:layout_above="@+id/tv_hello" android:layout_centerHorizontal="true" android:layout_marginBottom="10dp"/> </RelativeLayout> |
Output:So output of this code will look like below images. First Image when you will run this code and second when you will click on Button.