In Android, you can use “android.widget.CheckBox” class to render a checkbox.
In this tutorial, we show you how to create 3 checkboxes in XML file, and demonstrates the use of listener to check the checkbox state – checked or unchecked.
P.S This project is developed in Eclipse 3.7, and tested with Android 2.3.3.
File : res/values/strings.xml
File : res/layout/main.xml
1. Result :
2. If “IPhone” is checked :
3. Checked “IPhone” and “Windows Mobile”, later, click on the “display” button :
In this tutorial, we show you how to create 3 checkboxes in XML file, and demonstrates the use of listener to check the checkbox state – checked or unchecked.
P.S This project is developed in Eclipse 3.7, and tested with Android 2.3.3.
1. Custom String
Open “res/values/strings.xml” file, add some user-defined string.File : res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, MyAndroidAppActivity!</string> <string name="app_name">MyAndroidApp</string> <string name="chk_ios">IPhone</string> <string name="chk_android">Android</string> <string name="chk_windows">Windows Mobile</string> <string name="btn_display">Display</string> </resources>
2. CheckBox
Open “res/layout/main.xml” file, add 3 “CheckBox” and a button, inside theLinearLayout
.File : res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <CheckBox android:id="@+id/chkIos" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/chk_ios" /> <CheckBox android:id="@+id/chkAndroid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/chk_android" android:checked="true" /> <CheckBox android:id="@+id/chkWindows" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/chk_windows" /> <Button android:id="@+id/btnDisplay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btn_display" /> </LinearLayout>
Make CheckBox is checked by default
Put
Put
android:checked="true"
inside checkbox element to make it checked bu default. In this case, “Android” option is checked by default.
3. Code Code
Attach listeners inside your activity “onCreate()
” method, to monitor following events :- If checkbox id : “chkIos” is checked, display a floating box with message “Bro, try Android”.
- If button is is clicked, display a floating box and display the checkbox states.
package com.mkyong.android; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.Toast; public class MyAndroidAppActivity extends Activity { private CheckBox chkIos, chkAndroid, chkWindows; private Button btnDisplay; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); addListenerOnChkIos(); addListenerOnButton(); } public void addListenerOnChkIos() { chkIos = (CheckBox) findViewById(R.id.chkIos); chkIos.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //is chkIos checked? if (((CheckBox) v).isChecked()) { Toast.makeText(MyAndroidAppActivity.this, "Bro, try Android :)", Toast.LENGTH_LONG).show(); } } }); } public void addListenerOnButton() { chkIos = (CheckBox) findViewById(R.id.chkIos); chkAndroid = (CheckBox) findViewById(R.id.chkAndroid); chkWindows = (CheckBox) findViewById(R.id.chkWindows); btnDisplay = (Button) findViewById(R.id.btnDisplay); btnDisplay.setOnClickListener(new OnClickListener() { //Run when button is clicked @Override public void onClick(View v) { StringBuffer result = new StringBuffer(); result.append("IPhone check : ").append(chkIos.isChecked()); result.append("\nAndroid check : ").append(chkAndroid.isChecked()); result.append("\nWindows Mobile check :").append(chkWindows.isChecked()); Toast.makeText(MyAndroidAppActivity.this, result.toString(), Toast.LENGTH_LONG).show(); } }); } }
4. Demo
Run the application.1. Result :
Download Source Code
Download it – Android-Checkbox-Example.zip (15 KB)
Android Tutorial Basic to Advance: Click Here
ReplyDeleteAndroid User Interface Tutorial: Click Here
This point has dependably been one of my most loved subjects to peruse about. I have observed your post to be exceptionally energizing and brimming with great data. I will check your different articles in the blink of an eye. live blogs
ReplyDeleteThanks for sharing this information and keep updating us. This is informatics and really useful to me.
ReplyDeleteBest Industrial Training in Noida
Best Industrial Training in Noida
Everythings Works Perfect!
ReplyDeleteBest BCA Colleges in Noida
hello sir,
ReplyDeletethanks for giving that type of information.Digital PVC Door suppliers in Karnataka
Best IOS training Company in Noida
ReplyDeleteBest 6 months IOS training Company in Noida
Best 6 weeks IOS training Company in Noida
Best Android training Company in Noida
Best 6 months Android training Company in Noida
Best 6 weeks Android training Company in Noida
Best React Native training Company in Noida
Best 6 weeks React Native training Company in Noida
Best flutter training Company in Noida
This comment has been removed by the author.
ReplyDeleteHack7
ReplyDeletemichael kors handbags sale
ReplyDeletefitflop
longchamp handbags
louboutin shoes uk
vapormax
kd 12
lebron 15
off white shoes
off white x jordan 1
yeezy
off white hoodie
ReplyDeleteyeezy boost 350 v2
palm angels hoodie
kobe shoes
golden goose sneakers
goyard bag
off white
air jordan
goyard bag
off white t shirt
karabük evden eve nakliyat
ReplyDeletebartın evden eve nakliyat
maraş evden eve nakliyat
mersin evden eve nakliyat
aksaray evden eve nakliyat
GYNYGJ
9ABBA
ReplyDeleteAnkara Şehirler Arası Nakliyat
Kayseri Parça Eşya Taşıma
Gate io Güvenilir mi
Çerkezköy Televizyon Tamircisi
Erzurum Şehir İçi Nakliyat
Isparta Lojistik
Tokat Şehirler Arası Nakliyat
Eskişehir Şehirler Arası Nakliyat
Amasya Lojistik
4B9BF
ReplyDeleteUrfa Parça Eşya Taşıma
Karaman Evden Eve Nakliyat
Afyon Şehir İçi Nakliyat
Bingöl Şehirler Arası Nakliyat
Şırnak Parça Eşya Taşıma
Ankara Boya Ustası
Adana Evden Eve Nakliyat
Coin Nedir
Yalova Şehir İçi Nakliyat
77B05
ReplyDeleteAdıyaman Şehirler Arası Nakliyat
Tekirdağ Fayans Ustası
Çerkezköy Mutfak Dolabı
Silivri Parke Ustası
Silivri Cam Balkon
Urfa Lojistik
Etlik Fayans Ustası
Bilecik Şehir İçi Nakliyat
AAX Güvenilir mi
4C1EA
ReplyDeleteAltındağ Fayans Ustası
Niğde Evden Eve Nakliyat
Referans Kimliği Nedir
Denizli Evden Eve Nakliyat
Çerkezköy Bulaşık Makinesi Tamircisi
Bitlis Evden Eve Nakliyat
Çerkezköy Oto Elektrik
Silivri Parke Ustası
Sincan Parke Ustası
7CE9E
ReplyDeleteÇerkezköy Boya Ustası
Binance Referans Kodu
Kripto Para Borsaları
AAX Güvenilir mi
Ordu Evden Eve Nakliyat
Maraş Evden Eve Nakliyat
Etlik Boya Ustası
Tekirdağ Çatı Ustası
Afyon Evden Eve Nakliyat
6121F
ReplyDeleteanapolon oxymetholone for sale
Karaman Parça Eşya Taşıma
Cointiger Güvenilir mi
Çerkezköy Kurtarıcı
Mardin Parça Eşya Taşıma
Ağrı Parça Eşya Taşıma
Çerkezköy Çekici
Bartın Şehir İçi Nakliyat
Bitcoin Nasıl Alınır
9F0E9
ReplyDeletehttps://resimlimag.net
97D7B
ReplyDeletereferans kimliği nedir
binance referans kodu
referans kimliği nedir
binance referans kodu
binance referans kodu
resimli magnet
binance referans kodu
resimli magnet
resimli magnet
066B2
ReplyDeletereferans kimliği nedir
binance referans kodu
binance referans kodu
resimli magnet
binance referans kodu
resimli magnet
binance referans kodu
referans kimliği nedir
resimli magnet
DD909
ReplyDeletepapaya meyvesi
https://kapinagelsin.com.tr/
probit
kraken
btcturk
mexc
kucoin
en iyi kripto para uygulaması
telegram en iyi kripto grupları
43E2B
ReplyDeleteJanuary 2024 Calendar
bybit
kripto telegram
telegram kripto para grupları
August 2024 Calendar
btcturk
2024 Calendar
canlı sohbet uygulamaları
kripto para nereden alınır