Using a dialog preference with custom layout - java

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.

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

If statement for two checkboxes are checked problem

I am trying to write a code that states that if one checkbox is checked the second one can not be checked at the same time.
I have one checkbox called dayShift and another called nightShift.
If I am checking nightShift and THEN dayShift the code works as I expected. But if I check dayShift first and THEN nightShift, the code does not apply.
public void days(View view) {
CheckBox nightShift_check = (CheckBox)findViewById(R.id.nightShift);
CheckBox dayShift_check = (CheckBox)findViewById(R.id.dayShift);
if (nightShift_check.isChecked() && dayShift_check.isChecked())
{
nightShift_check.setChecked(false);
dayShift_check.setChecked(false);
Toast.makeText(mainApplication.this,"Error message",Toast.LENGTH_LONG).show();
}
}
If you need to perform different logic for that you could use RadioButton so there can't be two statements true at the same time.
Otherwise you could add something that is called setOnCheckedChangeListener on your CheckBox and uncheck them both if they both are checked for example:
dayShift_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked && nightShift_check.isChecked()) {
dayShift_check.setChecked(false);
nightShift_check.setChecked(false);
}
}
});
But I still strongly recommend you to use RadioButton if you need to choose from two, as #NileshRathod mentioned above in comment section.
If you need to choose one option from more you probably want to use RadioGroup.
You should use a listener on your CheckBox to get the state at each click :
nightShift_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
dayShift_check.setChecked(!isChecked);
}
});
dayShift_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
nightShift_check.setChecked(!isChecked);
}
});
When one have a state the other have the opposite state.
Hope it's help.

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.

Android - toggleButton delay with own OnClickListener

I have problem with unwanted delay after click on toggleButton using own OnClickListener.
I make my listener by this advice on stackoverflow, like below:
public class ToggleButtonOnClickListener implements OnClickListener{
private String _name;
public ToggleButtonOnClickListener(String name) {
_name = name;
}
#Override
public void onClick(View v) {
Log.i("toggle button clicked",_name);
}
}
and using this:
toggle.setOnClickListener(new ToggleButtonOnClickListener(device.GetName()));
But it not fire onClick method after first click, but the next one.
And because I have group of toggleButtons is this very unhappy, when I click on first, and onClick method fire after click again or even after click to second (or any) from the group.
The OnCheckChangeListener behaves the same.
Please refer developer's example.
You can implement something like below:
public void onToggleClicked(View view) {
// Is the toggle on?
boolean on = ((ToggleButton) view).isChecked();
if (on) {
// Enable vibrate
} else {
// Disable vibrate
}
}
After looking for errors and testing other options, I found that delay caused not Listener, but the log statement.
So, the code above working well except for
Log.i("toggle button clicked",_name);
and an alternative without needs have own class(parametrs) using OnCheckedChangeListener is:
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(buttonView.getContext(), "test", Toast.LENGTH_LONG).show();
}
});
I don't know why this Log do, but I used them only for debug, so problem solved!

Categories

Resources