0

Get CheckBox value in Android Java Code

//MainActivity.java
[code language=”css”]
package codeof.sandy;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;

public class MainActivity extends Activity {
CheckBox chkfair, chkgood, chkgreat, chkexcellent

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("CheckBoxActivity");
setContentView(R.layout.main);
find_and_modify_text_view();
}

private void find_and_modify_text_view() {
chkfair = (CheckBox) findViewById(R.id.chkfair);
chkgood = (CheckBox) findViewById(R.id.chkgood);
chkgreat = (CheckBox) findViewById(R.id.chkgreat);
chkexcellent = (CheckBox) findViewById(R.id.chkexcellent);
Button get_view_button = (Button) findViewById(R.id.get_view_button);
get_view_button.setOnClickListener(get_view_button_listener);
}

private Button.OnClickListener get_view_button_listener = new Button.OnClickListener() {
public void onClick(View v) {
String condition = "";
if (chkfair.isChecked()) {
condition = condition + "," + chkfair.getText();
}
if (chkgood.isChecked()) {
condition = condition + "," + chkgood.getText();
}
if (chkgreat.isChecked()) {
condition = condition + "," + chkgreat.getText();
}
if (chkexcellent.isChecked()) {
condition = condition + "," + chkexcellent.getText();
}
setTitle("Checked: " + r);
}
};
}

[/code]

//main.xml
[code language=”css”]

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<CheckBox
android:id="@+id/chkfair"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:paddingLeft="5dip"
android:layout_weight="1"
android:fontFamily="sans-serif-light"
android:text="Fair"
android:button="@drawable/custom_checkbox_design"
android:textColor="#ffffff"
android:textSize="11sp"/>
<CheckBox
android:id="@+id/chkgood"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_weight="1"
android:paddingLeft="5dip"
android:button="@drawable/custom_checkbox_design"
android:fontFamily="sans-serif-light"
android:text="Good"
android:textColor="#ffffff"
android:textSize="11sp"/>
<CheckBox
android:id="@+id/chkgreat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_weight="1"
android:paddingLeft="5dip"
android:button="@drawable/custom_checkbox_design"
android:fontFamily="sans-serif-light"
android:text="Great"
android:textColor="#ffffff"
android:textSize="11sp"/>
<CheckBox
android:id="@+id/chkexcellent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_weight="1"
android:paddingLeft="5dip"
android:button="@drawable/custom_checkbox_design"
android:fontFamily="sans-serif-light"
android:text="Excellent"
android:textColor="#ffffff"
android:textSize="11sp"/>

<Button android:id="@+id/get_view_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get CheckBox" />

</LinearLayout>
[/code]

Sandeep Kumar

Sandeep kumar is a web developer and a professinal blogger. He works for latest technologies and write articles what he learn. In ICodeHub he share web languages tutorials and other programming tips and tricks. if you need some help or have some suggestion then mail him at sandeep92pal@gmail.com without any hesitation. He believes in learning and sharing.