checkbox used to select the option either single or multiple
checkbox based onclick OnClickListener for Example : android:onclick attribute
We can make options like checked and not checked option
based on onclick to receive the event in android checkbox
MainActivity[MainActivity.class]
package com.androidfeeders.checkboxsample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
public static final String TAG=MainActivity.class.getSimpleName();
LinearLayout linearLayout;
CheckBox checkBox1,checkBox2,checkBox3,checkBox4;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout = findViewById(R.id.LinearLayoutId1);
checkBox1 = (CheckBox)findViewById(R.id.CheckBoxId1);
checkBox1.setText("Mr.");
checkBox2 = (CheckBox)findViewById(R.id.CheckBoxId2);
checkBox2.setText("Mrs.");
checkBox3 = (CheckBox)findViewById(R.id.CheckBoxId3);
checkBox3.setText("Ms.");
checkBox4 = (CheckBox)findViewById(R.id.CheckBoxId4);
checkBox4.setText("Miss");
button = (Button) findViewById(R.id.SubmitButtonId);
button.setOnClickListener(this);
}
public void onCheckboxClicked(View view) {
boolean checked = ((CheckBox) view).isChecked();
switch(view.getId()) {
case R.id.CheckBoxId1:
break;
case R.id.CheckBoxId2:
break;
case R.id.CheckBoxId3:
break;
case R.id.CheckBoxId4:
break;
}
}
@Override
public void onClick(View view) {
}
}
Layout Resource[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:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center"
android:orientation="horizontal"
android:id="@+id/LinearLayoutId1">
<CheckBox
android:id="@+id/CheckBoxId1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="product1"/>
<CheckBox
android:id="@+id/CheckBoxId2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="product1"/>
<CheckBox
android:id="@+id/CheckBoxId3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="product3"/>
<CheckBox
android:id="@+id/CheckBoxId4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="product4"/>
</LinearLayout>
<Button
android:id="@+id/SubmitButtonId"
android:layout_margin="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit"
/>
</LinearLayout>
Gradle Scripts~build.gradle[Module:app]
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.androidfeeders.checkboxsample"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0-alpha3'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
Gradle Scripts~build.gradle[Project:CheckBoxSample]
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
delete rootProject.buildDir
}
Resource[strings.xml]
<resources>
<string name="app_name">CheckBoxSample</string>
</resources>
manifests
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidfeeders.checkboxsample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I started to learn android and Kotlin and iOS and Hybrid application and i cant code IOS very well. But i love more interesting to code and finding new developer minds who are join to me You're welcome to keep on using my website.Instead, you can join our FB page for developer comments and post there: https://www.facebook.com/ramasamy.m.779 Best wishes :) Ramasamy(Software Developer)
See what's new in Android & IOS & Hybrid development … explore and learn in site
Ramasamy
5 min agoKotlin and React native example too
ReplyRamasamy
5 min agoKotlin and React native example too
ReplyRamasamy
5 min agoKotlin and React native example too
Reply