how to get focus when checked chekbox? - android

I have set setFocusable(false) in my edit text. But when i checked checkbox i want to get focus in my edittext. below is example.
cbIsGap.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
edittext1.setFocusable(true);
edittext1.setClickable(true);
}else {
edittext1.setFocusable(false);
edittext1.setClickable(false);
}
}
});
I am trying above example but i am not getting focus when i checked checkbox

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()));
}
}
});

Android: How to uncheck checkbox without perform click on it?

I have 3 checkbox in my Layout:
(case #1) mCheckBoxAll: when is checked or unchecked, all other boxes are set in the same state.
(case #2) mCheckBoxChoice1 and mCheckBoxChoice2: if one of these is unchecked by the user then the mCheckBoxAll must be unchecked.
mCheckBoxAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mCheckBoxChoice1.setChecked(isChecked);
mCheckBoxChoice2.setChecked(isChecked);
}
});
mCheckBoxChoice1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (!isChecked)
mCheckBoxAll.setChecked(!isChecked);
}
});
mCheckBoxChoice2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (!isChecked)
mCheckBoxAll.setChecked(!isChecked);
}
});
But this code doesn't work in case #2. All cases are unchecked in the same time...
I would put all your checkboxes that should change at once in a RadioGroup, and when mCheckBoxAll is checked iterate over RadioGroup and select / deselect them all
case 1
final RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radioGroup);
for (int i = 0; i < radioGroup.getChildCount(); i++){
((RadioButton) radioGroup.getChildAt(i)).setChecked(isChecked);
}
case 2 - you could call this method, to determine if one of the buttons is unchecked (call inside your 2 buttons onCheckedChanged). If it returns false, deselecte the main RadioButton.
private boolean isButtonUnchecked() {
for (int i = 0; i < radioGroup.getChildCount(); i++){
RadioButton radioButton = (RadioButton) radioGroup.getChildAt(i);
if (!radioButton.isChecked()) {
return false;
}
}
return true;
}
When you checkbox2 is unchecked then you set the state of checkboxall to be unchecked, and there you call checkbox1 to be uncheck as well, here is your problem.
So if checkbox 1 and 2 are checked and then you unchecked checkbox2 it will affect checkbox1.
Edit:
You should identify the source of the unchecked in checkboxall, if it comes from one of the two checkbox or if it comes from the uncheck all.
The easiest solution is to declare a global variable in your activity/fragment which tells you if you comes from checkboxes 1 and 2.
And if your checkboxall listener uncheck 1 and 2 only if the value is true.
Don't forget to change its value when you are done to false.
Edited code:
modify your code to
mListener = new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mCheckBoxChoice1.setChecked(isChecked);
}
};
mCheckBoxAll.setOnCheckedChangeListener(mListener);
mCheckBoxChoice1.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (!isChecked){
mCheckBoxAll.setOnCheckedChangeListener(null);
mCheckBoxAll.setChecked(!isChecked);
mCheckBoxAll.setOnCheckedChangeListener(mListener)
}
}
});
mCheckBoxChoice2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (!isChecked){
mCheckBoxAll.setOnCheckedChangeListener(null);
mCheckBoxAll.setChecked(!isChecked);
mCheckBoxAll.setOnCheckedChangeListener(mListener)
}
}
});
use below code and it will work your scenario
int mCheckBoxAll_clicktimes=0;
int mCheckBoxChoice2_clicktimes=0;
int mCheckBoxChoice1_clicktimes=0;
mCheckBoxAll.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCheckBoxChoice1.setChecked(false);
mCheckBoxAll.setChecked(false);
mCheckBoxChoice2.setChecked(false);
mCheckBoxAll_clicktimes++;
mCheckBoxChoice2_clicktimes=0;
mCheckBoxChoice1_clicktimes=0;
if(mCheckBoxAll_clicktimes%2 !=0 || mCheckBoxAll_clicktimes==0){
//-----when mcheckbox all checked
mCheckBoxAll.setChecked(true);
mCheckBoxChoice1.setChecked(true);
mCheckBoxChoice2.setChecked(true);
}else{
//===when mcheckboxall unchecked
mCheckBoxChoice1.setChecked(false);
mCheckBoxAll.setChecked(false);
mCheckBoxChoice2.setChecked(false);
}
}
});
mCheckBoxChoice1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCheckBoxChoice1.setChecked(false);
mCheckBoxAll.setChecked(false);
mCheckBoxChoice2.setChecked(false);
mCheckBoxAll_clicktimes=0;
mCheckBoxChoice2_clicktimes=0;
mCheckBoxChoice1_clicktimes++;
if(mCheckBoxChoice1_clicktimes%2 !=0 || mCheckBoxChoice1_clicktimes==0){
//when mCheckBoxChoice1 checked
mCheckBoxChoice1.setChecked(true);
}else{
//====when mCheckBoxChoice1 unchecked
mCheckBoxChoice1.setChecked(false);
mCheckBoxAll.setChecked(false);
}
}
});
mCheckBoxChoice2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCheckBoxChoice1.setChecked(false);
mCheckBoxAll.setChecked(false);
mCheckBoxChoice2.setChecked(false);
mCheckBoxAll_clicktimes=0;
mCheckBoxChoice2_clicktimes++;
mCheckBoxChoice1_clicktimes=0;
if(mCheckBoxChoice2_clicktimes%2 !=0 || mCheckBoxChoice2_clicktimes==0){
//when mCheckBoxChoice2 checked
mCheckBoxChoice2.setChecked(true);
}else{
//when mCheckBoxChoice2 unchecked
mCheckBoxChoice2.setChecked(false);
mCheckBoxAll.setChecked(false);
}
}
});

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");
}
}
};

Store Checkbox value to array list

How do I store the value of a checked checkbox to an array list and display it in a list./..say Checkbox1 = "Apple"......so far I have this
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if (checkBox1.isChecked()) {
//my problem is below here: I can't seem to add the value of it not the id and store it in list
pubsList.add(pub3);
pub3=Integer.toString(checkBox1.getSelectedItem());
}
}
For only one check box e.g.selectAll you can have this---
selectAll.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
Log.e(TAG, "Select all Checkbox enabled");
String ad=(String) selectAll.getText();//get text of selectAll checkbox
SelectionList.add(ad);//add above text to list of strings
}
if(! isChecked)
{
//do something if required
}
}
});
Hope this will help you.

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();
}
}
});

Categories

Resources