Android checkbox example - android

I am trying to create an android application program..Is this possible to place the checkbox within the textview..if possible means tell some example..Thanks in advance

Just inflate android.R.layout.select_dialog_multichoice and you'll get a textview with a checkbox!
It's actually a CheckedTextView, so do it like
CheckedTextView whatever = (CheckedTextView) getLayoutInflater().inflate(android.R.layout.select_dialog_multichoice, null);

CheckBox cb = new CheckBox(context);
cb.setText("message");
cb.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
// perform logic
}
}
});

Related

i want to change the image when i click on a check box

I want that when I click first time on this check box it should change the image from my Image View and when I click again on same checkbox it should change the image in Image View with another Image.
I Tried:-
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
myImage.setBackgroundResource(R.drawable.saveimage);
}else{
myImage.setBackgroundResource(R.drawable.deleteimage);
}
}
});
It's done:-
Take globally (boolean switchStatus = false);
if(switchStatus == true){
dialog.dismiss();
image1.setImageResource(R.drawable.icon_delete);
switchStatus = false;
}else{
dialog.dismiss();
image1.setImageResource(R.drawable.icon_checkmark);
switchStatus = true;
}
There is nothing wrong with your code apart from the lack of using setchecked method. When you check your chechbox , call checkbox.setchecked(true) and when you unckeck it, call checkbox.setchecked(false);
Because your image is in Drawable I think you should try this:
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
myImage.setImageDrawable(getResources().getDrawable(R.drawable.saveimage,getApplicationContext().getTheme()));
}else{
myImage.setImageDrawable(getResources().getDrawable((R.drawable.deleteimage,getApplicationContext().getTheme()));
}
}
});

Uncheck one box unchecks all boxes

I have an activity that extends PreferenceActivity.
I have in settings.xml 3 checkboxpreferences. If I uncheck the first checkbox, the other 2 to uncheck.
What is the code for that?
I think you want to use checkbox.toggle()
First make a onClickListener:
Checkbox chk = (CheckBox)findViewById(R.id.checkBox1);
chk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
}
}
);
Then change the other checkboxes:
CheckBox chk1, chk2;
chk1 = (CheckBox)findViewById(R.id.checkBox2);
chk2 = (CheckBox)findViewById(R.id.checkBox3);
and:
if(chk1.isChecked()){
chk1.toggle();
}
if(chk2.isChecked()){
chk2.toggle();
}

Checkbox to textview android?

I want that when I click on the checkbox "a" for example, then the text view1 displays "a", I have 3 options per text view. Thanks for the help.
a.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if(isChecked) {
text_view1.setText("a");
}
}
};

How to implement a CheckBox's onTouchEvent

How do you implement this onTouchEvent? It should fire when the user checks or unchecks the CheckBox widget.
CheckBox checkBox = new CheckBox(activity);
checkBox.setText("Don't present me information again.");
checkBox.onTouchEvent(.....);
The CheckBox widget (and any other widget that extends CompoundButton) has a method setOnCheckedChangeListener, which is the bit you're lacking (you probably don't want to use onTouchEvent in this case).
This example should replace the final line of code in your snippet:
checkBox.setOnCheckedChangeListener( new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if ( isChecked ) {
// do some stuff
}
}
});
what you need in your case is this
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
Toast.makeText(yourActivity.this,"CheckBox is Checked ..",3000).show();
}
else{
Toast.makeText(yourActivity.this,"CheckBox is Unchecked ..",3000).show();
}
}
});

Pop_up on checkbox selection

How can I display a pop-up immediately a checkbox is checked in Android?
CheckBox yourCheckBox = (CheckBox) findViewById( R.id.yourCheckBox );
yourCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if (isChecked)
{
//call popup
}
}
});
You just need to declare a call back for the CheckedChanged Event on the text box and then fire your method that displays the popup

Categories

Resources