Go to another activity when all fields are not empty - android

I have 1 activity and 3 fragments in this activity.
In 3 fragments I have booleans returning true when checkbox is checked or edittext is not empty..
I want to unable button when editTexts are not empty and my checkbox is checked..
When I added something like sexMan.isChecked() to method EditTextCompleted()
my app crushing....
So how can I connects this conditions in one place?
Checkbox Listener
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
mSendInfoFromBmrFragment.sexChecked(true);
switch (buttonView.getId()){
case R.id.sexMan:
mSendInfoFromBmrFragment.sex(BmrCalculation.SEX_MAN);
sexMan.setChecked(true);
sexWoman.setChecked(false);
break;
case R.id.sexWoman:
sexMan.setChecked(false);
sexWoman.setChecked(true);
mSendInfoFromBmrFragment.sex(BmrCalculation.SEX_WOMAN);
break;
Edit Text listener
#Override
public void afterTextChanged(Editable s) {
if ( s == weightEditText.getEditableText()){
mSendInfoFromBmrFragment.getweight(Integer.parseInt(s.toString()));
mWeight = Integer.parseInt(s.toString());
} else if ( s == heightEditText.getEditableText()) {
mSendInfoFromBmrFragment.getheight(Integer.parseInt(s.toString()));
mHeight = Integer.parseInt(s.toString());
} else if ( s == ageEditText.getEditableText()){
mSendInfoFromBmrFragment.getage(Integer.parseInt(s.toString()));
mAge = Integer.parseInt(s.toString());
}
if ( (!(weightEditText.getEditableText().toString().equals("")) && !(heightEditText.getEditableText().toString().equals("")) && !(ageEditText.getEditableText().toString().equals("")))) {
mSendInfoFromBmrFragment.editTextComplete(true);
} else {
mSendInfoFromBmrFragment.editTextComplete(false);
}
}
Interface
public interface sendInfoFromBmrFragment {
public void getheight(int height);
public void getweight(int weight);
public void getage(int age);
public void editTextComplete(boolean editTextComplete);
public void sex(String sex);
}

For the Radio Buttons you can declare an array if you know their number and they are static, do this as follow:
public Boolean AllRadiosAreChecked;
private Boolean Checked[] =
{
false, // sexMan is checked ?
false // sexWoman is checked ?
};
Then, in the same activity ovverride the OnCheckedChanged
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
switch (buttonView.getId()){
case R.id.sexMan:
Checked[0]=true;
break;
case R.id.sexWoman:
Checked[1]=true;
break;
Then you check anywhere you want if all Radio Buttons are checked:
AllRadiosAreChecked=true;
for (int i = 0; i < 2; i++)
{
if (!Checked[i])
{
AllRadiosAreChecked= false;
}
}
if(!AllRadiosAreChecked){
// NOT all radio buttons are checked
}
else{
// All radio buttons are checked enter code here
}
This conditions in the array must be declared to group radios.
Good Luck

You can add callbacks to your checkboxes and then they all are true you need call method in your parent activity (for example send in activity fragment ID and boolean value). To call method from activity you need to use the next code
((ParentActivity)getActivity()).yourMethod();
And then activity received all booleans from all fragment you need to change Button visibility.

If you have edittext you can add textchangelistener to it, if you have checkbox you can add listener of it, in listener you can check validation. Based on validation you can enable or disable the button for next activity. You can have that button to be static to access from other class.
In every listener you can call one public static method in which you will check all validation every time, if it satisfies then method will return true and you can enable button.

Related

Changes apply on the second tap

I have a RecyclerView and I am trying to make it with multiple selections so I can perform an action to all items selected.
I have to mention that in the fragment that holds one item I have a radio button(radioButton), some text, and a button(joinBtn).
What I want to do is that when I click the item, the radio button will become checked, and the button will become visible and enabled.
My class Group has a private boolean clicked = false by default.
In the ViewHolder I have the method bind :
public void bind(Group group) {
this.group = group;
mTitleTextView.setText(group.getName());
if(group.isClicked())
radioButton.setChecked(true);
else
radioButton.setChecked(false);
}
And in the onClick method i have the actions I want to perform:
#Override
public void onClick(View view) {
if (joinBtn.isShown()) {
joinBtn.setVisibility(View.GONE);
joinBtn.setEnabled(false);
group.setClicked(false);
}
else{
joinBtn.setVisibility(View.VISIBLE);
joinBtn.setEnabled(true);
group.setClicked(true);
}
}
My problem is the behaviour I get:
1st tap: radio button gets checked.
2nd tap: joinBtn appears.
3rd tap: radio button gets unchecked.
4th tap: joinBtn disappears.
How can I make the actions from 1st and 2nd tap to happen in one tap? The same for 3rd and 4th?
Try calling notifyItemChanged(int position) on the adapter instance after your onClick() logic. Pass the index of the item as the param.
The UI may not update by itself, but this function should refresh it.
Maintain a flag which will change on group click
boolean isChecked = false ;
public void bind(Group group) {
this.group = group;
mTitleTextView.setText(group.getName());
if(isChecked)
isChecked = false;
radioButton.setChecked(false);
else
isChecked = true;
radioButton.setChecked(true);
}
and then
#Override
public void onClick(View view) {
if (isChecked) {
joinBtn.setVisibility(View.GONE);
joinBtn.setEnabled(false);
group.setClicked(false);
}
else{
joinBtn.setVisibility(View.VISIBLE);
joinBtn.setEnabled(true);
group.setClicked(true);
}
}
Apparently the problem was because of the radio button. The first thing I did was to use a ImageView instead of the radio button. In that ImageView I used (when needed) one of two pictures: selected or unselected.
In the bind method all I needed was: joinBtn.setVisibility(View.GONE); and radioBtnImage.setImageResource(R.drawable.radio_btn_unselected);
In the onClick method I used:
if (joinBtn.isShown()) {
joinBtn.setVisibility(View.GONE);
joinBtn.setClickable(false);
radioBtnImage.setImageResource(R.drawable.radio_btn_unselected);
}
else{
joinBtn.setVisibility(View.VISIBLE);
joinBtn.setClickable(true);
radioBtnImage.setImageResource(R.drawable.radio_btn_selected);
}
This did it for me!

Check state of togglebutton android

Im new to android and I don't get the code on the developers website about this really, I just want to check if the state of the toggle button is on or off. I have the following scenario
if (isCheckedToggleButton?)
{
// do something
}
else
{
// do something else
}
And a method as the guide suggests
public void onToggleClicked(View view)
{
boolean on = ((ToggleButton)view).isChecked();
if(on) {
return;
} else {
}
}
So I just want to see if the toggle button is on or off so I can decide whether to execute the code inside the if or the else. Unfortunately the method provided by the android guide is a void so it doesn't return a boolean. How can I still check the state?
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lift"
android:textOff="Uit"
android:textOn="Aan"
android:id="#+id/switchLift" android:layout_below="#id/btnEindpunt"
android:layout_alignLeft="#+id/btnEindpunt"/>
Use it like this
myToggleButton.setOnCheckedChangeListener( new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton toggleButton, boolean isChecked)
{
if(isChecked)
// Do something
else
// Do the other thing
}
});
Change
ToggleButton myToggleButton = ((ToggleButton) findViewById(R.id.switchLift));
to
Switch myToggleButton = ((Switch) findViewById(R.id.switchLift));
Also change isChecked to something else like mIsChecked or inside the listener use YourClassName.this.isChecked for changing its value. There is already a local variable with same name.

how to skip switch.setOnCheckedChangeListener when status is not updated by user

since i am trying the switch first time (new to android) i am not sure how to handle this issue. i have a switch on an activity and an attached setOnCheckedChangeListener() to it. when the activity's oncreate is called i make an async call to database and depending on the values received i set the status of the switch on/off. Now the problem is that however i am setting the switch state to just show whats its current status on db and no user has changed it yet, still the listner function is called. i know that the code is working correctly but with the state changed listner i need something else to confirm that the state has been changed by the user . i think onTouchEvent(MotionEvent event) can fill the purpose but do not know hot to use it in conjuction with switch.setOnCheckedChangeListener
does anyone know of any better solution to it or atleast can help me telling how to use ontouch even with listner...
sw_recording_switch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
}}
thanks !!!
Indeed when calling Switch.setChecked(boolean); the OnCheckedChangeListener will be triggerd as well.
The way I overcame this problem was to use a flag and set it to false before I call setChecked()
This way the listener will still be called when you programmatically use setChecked() but the code inside won't execute, unless a user presses on the switch.
//prevent the code from listener to run, flag set to false before calling setChecked(true);
should_run = false;
toggle_facebook.setChecked(true);
....
private OnCheckedChangeListener onSwitchSlided = new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
switch(buttonView.getId())
{
case R.id.settings_toggle_facebook:
{
if(true == should_run)
{
//do stuff
}
should_run = true;
break;
}
case R.id.settings_toggle_twitter:
{
if(true == should_run)
{
//do stuff
}
should_run = true;
break;
}
}
}
};
Two ways to handle initialization code so handlers do not fire.
Design your handler to recognize that it is initialization. Below example use isResumed() to determine if the code is initializing. This works because onCreate is called before onResume.
#Override
public void onCheckedChanged(RadioGroup rg, int checkId) {
switch (rg.getId()) {
case R.id.rgMileKilometer:
switch (checkId) {
// process the speed radio group
case R.id.rdoMiles:
// Speed Radio Group check if the mph button is checked
isMile = true;
break;
case R.id.rdoKilometer:
isMile = false;
// Speed Radio Group check if the mph button is checked
break;
}
if (isResumed()) {
//do something the code is ready...
}
}
}
Add the listeners after you have done the initialization
CheckBox cb = (CheckBox) view
.findViewById(R.id.cbApplicationCacheTabs);
cb.setChecked(isApplicationCacheTabs);
cb.setOnCheckedChangeListener(this);

How to Enable a button after one checkbox or more are checked?

I have a question say if I have a List of check-boxes but I don't know how many since they are generated by the user how can i Disable/Enable the delete button when at least one or more check-boxes are checked ?
I have this code so far
I am new to programming so be specific please
Thank you in advance.
#Override
public void onCheckedChanged(CompoundButton cb, boolean DeleteButton) {
if(cb.isChecked()){
DeleteButton.setEnabled(true);
}
else if(DeleteButton.isEnabled()){
DeleteButton.setEnabled(false);
If you only want to know if one or more CheckBoxes are checked then create an int member variable and add to it when a box is checked
public class MyActivity
{
int count = 0;
// oncreate...
#Override
public void onCheckedChanged(CompoundButton cb, boolean DeleteButton) {
count = (DeleteButton) ? count+1 : count - 1; // if is checked then add
// add 1 to count else decrement
// I'm not sure about what is below but now you have the count
if(cb.isChecked()){
DeleteButton.setEnabled(true);
}
else if(DeleteButton.isEnabled()){
DeleteButton.setEnabled(false);
}
}
};
Now the way to disable/enable the button if the count is greater than 0(you have at least one CheckBox checked would be like
#Override
public void onCheckedChanged(CompoundButton cb, boolean DeleteButton) {
count = (DeleteButton) ? count+1 : count - 1; // if is checked then add
// add 1 to count else decrement
DeleteButton.setEnabled(count > 0); // if count > 0 will be enabled
}
};
But I think you are confused on the second param of onCheckChanged(). It says whether or not the Button is checked, not what the Button is.
You can manage a list of checked items and whenever the checked state is changed, check whether the list is empty or has some items and then handle the enabling or disabling the button accordingly
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if (isChecked)
list.add(itemId);
else
list.remove(itemId);
button.setEnabled(list.size() > 0);
}
});
Let me know if I misunderstood your question, but if you mean that you want to disable the delete button when one or more check boxes are selected, you could simply track the quantity of boxes checked. The that value is greater than or equal to one, than disable the delete button. For example:
int i = 0;
final Button DeleteButton = (Button)findViewById(R.id.button1);
DeleteButton.setEnabled(false);
final CheckBox cb = (CheckBox)findViewById(R.id.checkBox);
OnCheckedChangeListener ch = new OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton cb, boolean DeleteButton) {
if(cb.isChecked()){i++;} else{i--;}
if(i >= 1){DeleteButton.setEnabled(true);} else{DeleteButton.setEnabled(false);}
}
};
cb.setOnCheckedChangeListener(ch);
DeleteButton.setOnClickListener(new OnClickListener(){
...

Checkbox default value & OnCheckedChangeListener

This is not so much of a problem but I am trying to find a correct way of doing this.
I have the following situation:
public class SettingsDialogFragment extends DialogFragment implements OnCheckedChangeListener {
...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.settings, container);
...
CheckBox lBox1 = (CheckBox)view.findViewById(R.id.checkBox1);
lBox1.setOnCheckedChangeListener(this);
lBox1.setChecked(true);
return view;
}
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
....
}
The "problem" I have is that by calling setChecked(true) the onCheckChanged will already fire. I guess that when I inflate the layout - the CheckBox is initialised with a false setting and me changing that to true indeed is a CheckedChanged event.
I could of course change the order and assign the listener after I set the initial value, but is there a way to inflate the layout whilst somehow passing the initial values for the various components? They are dynamic so I cannot fix the values to a particular value in the settings.xml
Cheers
The above suggestion is good, but this problem still exists in a checkable ListView. I solved it in this way: disable the listener, set check state, and then set listener again. Here is a helper function:
private void checkCheckBox(CheckBox checkBox, boolean checked) {
checkBox.setOnCheckedChangeListener(null);
checkBox.setChecked(checked);
checkBox.setOnCheckedChangeListener(this);
}
You've answered your own question, the setChecked(true) is causing the OnCheckedChangeListener to be called.
A simple fix would be to add android:checked="true" to your CheckBox XML declaration and omit the setChecked(true) call.
The code of the CheckBox looks something like this:
public void setChecked(boolean checked) {
if (mChecked != checked) {
mChecked = checked;
refreshDrawableState();
// Avoid infinite recursions if setChecked() is called from a listener
if (mBroadcasting) {
return;
}
mBroadcasting = true;
if (mOnCheckedChangeListener != null) {
mOnCheckedChangeListener.onCheckedChanged(this, mChecked);
}
if (mOnCheckedChangeWidgetListener != null) {
mOnCheckedChangeWidgetListener.onCheckedChanged(this, mChecked);
}
mBroadcasting = false;
}
}
So basically you cannot use the method without firing events, unless you remove or disable the event handler before (or set them afterwards only).
If you just want to set initial values, then your first suggestion is probably the best: just register the listeners after you have initialized everything.
The solution is the OnTouchListener. By default you don't set the OnCheckedChangeListener. What you have to do is the following:
**declared in the class object declaration**:
OnCheckedChangeListener onCheckedChangeListener = new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
**have fun**
}
};
**used to the point you would have set the OnCheckedChangeListener**
anyView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
anyUsableView.setOnCheckedChangeListener(onCheckedChangeListener);
return false;
}
});
The return false to the OnTouchListener is very important, otherwise the view will not work anymore.
Tip: this solution can be used with any kind of listener that is good for it (I am using it with a Switch widget)
I guess the solution could be a setChecked() function which detaches the listener before setting the checked value.
public void setCheckedNoEvent(boolean checked) {
if (onCheckedChangeListener == null) {
switcher.setChecked(checked);
} else {
switcher.setOnCheckedChangeListener(null);
switcher.setChecked(checked);
switcher.setOnCheckedChangeListener(OnCheckedChangeListener);
}
}

Categories

Resources