multiple checkboxes in android example
Checkboxes allow the user to select one or more options from a set. Typically, you should present each checkbox option in a vertical list.
Follow the function using the example check();
public void check(){
if(chandroid.isChecked()==true){
Toast.makeText(MainActivity.this,"You are android chcked",Toast.LENGTH_SHORT).show();
}else if(chajava.isChecked()==true){
Toast.makeText(MainActivity.this,"You are Java chcked",Toast.LENGTH_SHORT).show();
}else if(chknet.isChecked()==true){
Toast.makeText(MainActivity.this,"You are C#.net chcked",Toast.LENGTH_SHORT).show();
}
}
1.MainActivity.java:
package com.akash.checkbox;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
CheckBox chandroid,chajava,chknet;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chandroid=(CheckBox)findViewById(R.id.chandroid);
chajava=(CheckBox)findViewById(R.id.chajava);
chknet=(CheckBox)findViewById(R.id.chknet);
check();
}
public void check(){
if(chandroid.isChecked()==true){
Toast.makeText(MainActivity.this,"You are android chcked",Toast.LENGTH_SHORT).show();
}else if(chajava.isChecked()==true){
Toast.makeText(MainActivity.this,"You are Java chcked",Toast.LENGTH_SHORT).show();
}else if(chknet.isChecked()==true){
Toast.makeText(MainActivity.this,"You are C#.net chcked",Toast.LENGTH_SHORT).show();
}
}
}
2.activity_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/chandroid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android" />
<CheckBox
android:id="@+id/chajava"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Java"
android:checked="true"/>
<CheckBox
android:id="@+id/chknet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C#.net" />
</LinearLayout>
3.strings.xml:
<resources>
<string name="app_name">CheckBox</string>
</resources>
4.AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.akash.checkbox">
<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>
No comments:
Post a Comment