1. Creating New Project in Kotlin
- Open Android Studio.
- Go to File => New => New Project.Give Name To your application. Then, check Include Kotlin Support and click next button.
- Select minimum SDK you need. However, we have selected 21 as minimum SDK. Then, click next button.
- Then, select Empty Activity => click next => click finish.
2.Here we take 2 Check box in our Layout android Provide Label Any fruit name. As in below xml layout.
main_activity.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" tools:context="com.dharmendra.checkbox.MainActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:gravity="center" android:textColor="@color/colorPrimary" android:layout_marginTop="47dp" android:textSize="25dp" android:text="Select Your Favourite Fruit" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" android:id="@+id/textView" /> <CheckBox android:id="@+id/apple" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_below="@+id/textView" android:layout_marginStart="84dp" android:layout_marginTop="30dp" android:text="Apple" /> <CheckBox android:id="@+id/mango" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignStart="@+id/apple" android:layout_below="@+id/apple" android:layout_marginTop="32dp" android:text="Mango" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/mango" android:layout_centerHorizontal="true" android:layout_marginTop="40dp" android:text="Click" /> </RelativeLayout>
3.MainActivity.kt
package com.dharmendra.checkbox import android.os.Bundle import android.support.v7.app.AppCompatActivity import android.widget.Button import android.widget.CheckBox import android.widget.Toast class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) //Find View by id for checkBox and Button val apple = findViewById<CheckBox>(R.id.apple) as CheckBox val mango = findViewById<CheckBox>(R.id.mango) as CheckBox val btn = findViewById<Button>(R.id.button) as Button //Set onclick Of button btn.setOnClickListener { //Check Condition for all check box Checked or Not. if (mango.isChecked == true && apple.isChecked == true) { Toast.makeText(this@MainActivity, "Mango and Apple are Selected", Toast.LENGTH_LONG).show() } else if (apple.isChecked == true) { Toast.makeText(this@MainActivity, "Apple Selected", Toast.LENGTH_LONG).show() } else if (mango.isChecked == true) { Toast.makeText(this@MainActivity, "Mango Selected", Toast.LENGTH_LONG).show() } else { Toast.makeText(this@MainActivity, "Nothing is Selected", Toast.LENGTH_LONG).show() } } } }
No comments:
Post a Comment