Hiding three checkboxes out of four, if another one is checked - java

There are 4 checkboxs in a dialog layout. I want to hide or disable to be checked three other ones if the first checkbox is checked. But if all or one of the other three checkboxs is checked, the first checkbox must be hidden or disabled in the layout.

Get the references(ids) using root.findViewById() hold them in the list.
Now in the onCheckChangeListener() loop through the list and uncheck all except the selected one. or do whatever is your requirement with them.

try this:
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if(isChecked)
{
// hide what you want example chkbox2.setVisibility(View.GONE);
}
else
{
// visualize what you have hidden in the if example chkbox2.setVisibility(View.VISIBLE);
}
}
});

Related

setVisibility of TableRow using CheckBox in android studio

I am currently working on android studio, but I cannot understand why my code does not work.
if (paidCheck.isChecked()) {
paidRow.setVisibility(View.VISIBLE);
} else {
paidRow.setVisibility(View.GONE);
}
As you can see, variables end with Checks are CheckBoxes, and end with Rows are TableRows.
My problem is even if I tap the CheckBoxes, TableRows do not appear in the app. Is there anything I did wrong?
The logic you have implemented gets called once whenever your activity or Fragment loads up, it does not gets called whenever you check or uncheck again.
What you need is a CheckedChangeListener which will be as follows
paidCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
// do somtheing when is checked
paidRow.setVisibility(View.VISIBLE);
}else{
paidRow.setVisibility(View.GONE);
}
}
});

Changing the width of an EditText when you select it

I want to make a search page with 3 text fields. Well, those text fields must expand when i select them and go back to the initial size after I am done writing. So, when I select one of the EditText, I must change the width and after I deselect it to go back to initial size.
Can someone help me with that?
Here is what I tried:
final EditText searchByName = (EditText) getActivity().findViewById(R.id.search_by_name);
searchByName.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
searchByName.setWidth(250);
}
});
The problem with this solution is that the text field won't come back to initial size after I deselect it.
Try with onFocusChangeListener without onClick listener.. this allows you to handle the event of selection and deselection both
You can use the onFocusChange event to verify if the user is in the EditText or not:
final EditText searchByName = (EditText) getActivity().findViewById(R.id.search_by_name);
searchByName.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
/* When focus is lost check that the text field
* has valid values.
*/
if (hasFocus) {
searchByName.setWidth(250);
} else {
searchByName.setWidth(normalSize); //Set back to normal.
}
}
});

OnclickListener in a checkbox (Android Studio)

here is part of my code which includes the CheckBox(ChckBoxNo):
final CheckBox ChckBoxNo = (CheckBox)promptsView.findViewById(R.id.ChkBoxNo);
ChckBoxNo.setChecked(true);
ChckBoxNo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (ChckBoxNo.isChecked()) {
ChckBoxNo.setChecked(false);
}
else if (!ChckBoxNo.isChecked())
{
ChckBoxNo.setChecked(true);
}
}
});
At the begining I set true for the isChecked() method on my checkbox , then I implement the onclicklistener on the checkbox.
When I run the app the checkbox is Checked as I defined eralier , but when I click on the checkbox ,It's unchecked and then immidately checked again(I didnt clciked again on the checkbox! )
What should I do in order to fix that,what wrong in my code ?
Thanks!
try this:
checkBox = (CheckBox) findViewById(R.id.checkBox);
checkBox.setChecked(checkPasswordExist());
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (checkBox.isChecked()) {
// your code to checked checkbox
}
} else {
// your code to no checked checkbox
}
}
});
A Checkbox will handle the "checking" process automatically - you do not have to manage this yourself for the standard usage.
Check out this example from the docs. Here, when a click event is caught, they are doing operations based on the isChecked() state.
You're battling the CheckBox. It's standart behavior that checkbox change states, you dont need to do that by yourself.
Remove setChecked true and false and paste something usefull there instead )
You dont need to set the checked state on the same that you have klicked.
You shold handle some other Operations on specific state.

Using a dialog preference with custom layout

I have a custom layout that I am using with a dialog preference, and the layout appears fine in the dialog box but the problem is that I have check boxes in the layout, and checking them doesnt make them behave as expected. Below is the code of my DialogPreferences.java class:
#Override
protected void onDialogClosed(boolean positiveResult) {
LayoutInflater inflater = ((Activity)getContext()).getLayoutInflater();
View view = inflater.inflate(R.layout.imagechoice, null);
button1 = (CheckBox)view.findViewById(R.id.background1);
button2 = (CheckBox)view.findViewById(R.id.background2);
button3 = (CheckBox)view.findViewById(R.id.background3);
button4 = (CheckBox)view.findViewById(R.id.background4);
button1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
selected=1;
button2.setChecked(false);
button3.setChecked(false);
button4.setChecked(false);
}
});
button2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
selected=2;
button1.setChecked(false);
button3.setChecked(false);
button4.setChecked(false);
}
});
if(positiveResult){
Log.d("BACKGROUND onDialogClosed", Integer.toString(selected));
persistInt(selected);
}
super.onDialogClosed(positiveResult); //To change body of overridden methods use File | Settings | File Templates.
}
When the app is running and I have the log open, I tried selecting the first or second check and I always see the value 0 printed by the log statement, indicating that the variable selected is never being updated as expected. I have tried to put the check box code in the
public DialogPreferences(Context oContext, AttributeSet attrs)
{...}
method and that led to the same results. In which method of this file is the proper place to put the code needed to do something when the check boxes are checked?
You should override onCreateDialogView and put your onCheckChangeListeners in there. The problem is currently they are only listening when onDialogClosed is called which is after you are trying check them.

Android Changing CheckBox Checked Color

I'm coding an application that deals with 2 different CheckBoxes. When one CheckBox gets clicked, the color of the tick should be blue (instead of green), whereas the color of the other CheckBox remains green.
This is my code...
CheckBox green = (CheckBox) findViewById(R.id.greenButton);
CheckBox blue = (CheckBox) findViewById(R.id.blueButton);
blue.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
if(arg1){
blue.setHighlightColor(Color.BLUE);
Toast.makeText(getBaseContext(), "Question Marked As Partial", 4000).show();
}
}
});
green.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
if(arg1){
blue.setHighlightColor(Color.GREEN);
Toast.makeText(getBaseContext(), "Question Marked As Fully Understood!", 4000).show();
}
}
});
However, both the CheckBoxes tick colors remains green, and the Toast message doesn't get displayed, so I am guessing that the OnCheckedChangeListener is never being called.
Could someone offer any advice?
If the Toast isn't appearing, it is possible that your listeners aren't actually being set on the CheckBoxes. In other words, maybe this whole piece of code isn't even being called.
If your code is in a method, make sure you're calling the method to set the listeners on the CheckBoxes, or ensure this code is in one of your main methods such as onCreate().
If you don't know already, it would be a great time to learn how to debug your code - it makes it really quick and easy to determine whether your code is being called or not.

Categories

Resources