Changes apply on the second tap - android

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!

Related

Go to another activity when all fields are not empty

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.

CheckBox OnClick Toggled Before OnClick Handler?

So I'm working on a list of check boxes. I'm using the OnClickListener to catch the touch event. I've also tried the OnTouch listener and the OnCheckChanged listener. The issue I've come across is the fact that the check box IsChecked value is set to true before it reaches any of these event listeners. So if I were to do something like this:
checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (checkBox.isChecked())
checkBox.setChecked(false);
}
});
It will always hit this if statement and immediately set the checkbox to unchecked because it's setting it as ischecked true before it reaches OnClickListener. The easiest way around this I've found is:
checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
checkBox.setChecked(!checkBox.isChecked());
if (checkBox.isChecked()) {
checkBox.setChecked(false);
}
}
});
But this is what I would call hackish. The other option I see being to create a new checkbox class and override the preformClick method.
#Override
public boolean performClick() {
toggle();
final boolean handled = super.performClick();
if (!handled) {
// View only makes a sound effect if the onClickListener was
// called, so we'll need to make one here instead.
playSoundEffect(SoundEffectConstants.CLICK);
}
return handled;
}
But that seems like a lot more work than should need to go in to accomplishing this task.
So, essentially my question is: Is there a method where I can override and intercept the setting of the checkbox before it is actually changed?
The OnClickListener of the CheckBox always gets called after it changed its checked state (as you can see in the performClick() method you copied, the toggle() method changes the checked state, before calling super's performClick(), which will call the OnClickListener).
If you don't want to always change the checked state of the CheckBox, then you can override performClick() and leave out the toggle() method (or only call it if some condition is true).
But if you want to change the state every time a click happens, then the easiest way is to use the OnClickListener, and just negate your conditions (e.g. if you want to do something when the checkbox was empty, then you check if the checkbox's new state is not empty).

Android single button multiple functions

I am beginner to Android development. I have 3 edit boxes and one "Edit" button. When I launch the activity all the edit boxes should be disabled. When I click on the Edit button all the 3 edit boxes should get enabled and button text should change to "Save". After updating the data in the edit boxes, when I click on the "Save" button, I should be able to send the updated data to the backend.
My problem is how can I make use of a single button for two function "Edit" and "Save".
Please help me.
You can do it this way:
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
String ButtonText = button.getText().toString();
if(ButtonText.equals("Save"){
//code for save
button.setText("Edit");
}
else{
//code for edit
button.setText("Save");
}
}
});
If I were you I would actually use two buttons one for edit, and one for save. Make them the same size and in the same position, when you want to switch between them make one invisible, and the other visible. Doing it that way would let you keep your onClickListeners separate which would make your code more understandable in my mind.
That being said you could technically achieve it with a single button as well. Just change the text on the button when you want to switch between them, and add an if statement into your click listener to check which "mode" your button is currently in to determine which action it should take.
I am not sure there is an easy way to do this or not. but you can sure use different behaviors of button clicks like
// When you press it for long time.
dummyButton.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
return true; // Can do lot more stuff here I am just returning boolean
}
});
// Normal click of button
dummyButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//do lot more stuff here.
}
});
Do it this way :
Make a public boolean variable
public boolean isClickedFirstTime = true;
make your 3 editTexts enabled false in xml and
onClick of your button
#Override
public void onClick(View v) {
if (v.getId() == R.id.edit_button_id) { //whatever your id of button
Button button = (Button) findViewById(R.id.edit_button_id);
if(isClickedFirstTime)
{
edit1.setEnabled(true);
edit2.setEnabled(true);
edit3.setEnabled(true);
butt.setText("Save");
isClickedFirstTime = false;
}
else
{
....//Get your values from editText and update your database
isClickedFirstTime = true;
}
}

Android: ToggleButton Listener

I have this code here
ToggleButton toggleAlarm = (ToggleButton) d.findViewById(R.id.toggle_alarm);
toggleAlarm.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
{
Log.d("alarmCheck","ALARM SET TO TRUE");
sched.setAlarm(true);
}
else
{
Log.d("alarmCheck","ALARM SET TO FALSE");
sched.setAlarm(false);
}
}
});
I have to keep track if its ON or OFF. But when I logged something to logcat where it is on or off, it won't do a thing. I don't know, what is wrong, because on my other code same, syntax but it works I just copy paste it and change only the ToggleButton variable.
EDIT
I have observed, with the help of cdr. Powell of course, that when you put this code block, the one that I have posted, inside another anonymous listener, say listener for a save button, the checkOnChangedListener is broken, it doesn't function well inside another anonymous listener, but the one thing that I don't understand is that, there is also a outer listener in my code, it is like a button to display a dialog box and inside that dialog box, there is an add button that opens another dialog box which has that toggle button and another button for save or add which closes that dialog and returns to the previous dialog which will then display the newly added record, so anyone of you have an idea why is it broken when i put it inside a listener for a save button but works fine in a outer listener.
try this, May be the problem is with import
toggleAlarm.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
{
Log.d("alarmCheck","ALARM SET TO TRUE");
sched.setAlarm(true);
}
else
{
Log.d("alarmCheck","ALARM SET TO FALSE");
sched.setAlarm(false);
}
}
});
Try toggleAlarm.isChecked() too see if the button is checked or not.
In case toggleAlarm.isChecked() does not work for you you could always.
boolean _isChecked = false;
((ToggleButton) d.findViewById(R.id.toggle_alarm)).setOnClickListener(new OnOnClickListener() {
public void onClick(View arg0) {
_isChecked = !isChecked;
if(_isChecked()) {
Log.d("alarmCheck","ALARM SET TO TRUE");
sched.setAlarm(true);
}
else {
Log.d("alarmCheck","ALARM SET TO FALSE");
sched.setAlarm(false);
}
}
});
So i have observed, with the help of cdr. powell of course, that when u put this code block, the one that i have posted, inside another anonymous listener, say listener for a save button, the checkOnChangedListener is broken, it doesn't function well inside another anonymous listener, but the one thing that i don't understand is that, there is also a outer listener in my code, its like a button to display a dialog box and inside that dialog box, there is an add button that opens another dialog box which has that toggle button and another button for save or add which closes that dialog and returns to the previous dialog which will then display the newly added record, so anyone of you have an idea why is it broken when i put it inside a listener for a save button but works fine in a outer listener.

Android: long click on a button -> perform actions

I want to use the same button to perform 2 different methods.
One method when user single clicks it and a second method (different) when the user LONG clicks it.
I use this for the single short click (which works great):
Button downSelected = (Button) findViewById(R.id.downSelected);
downSelected.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
method();
}
}
});
I've tried to add a longClickListener but it didn't work.
Appreciate any ideas on how to solve this.
Thanks!
I've done it before, I just used:
down.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return true;
}
});
Per documentation:
public void setOnLongClickListener
(View.OnLongClickListener l)
Since: API Level 1 Register a callback
to be invoked when this view is
clicked and held. If this view is not
long clickable, it becomes long
clickable.
Notice that it requires to return a boolean, this should work.
To get both functions working for a clickable image that will respond to both short and long clicks, I tried the following that seems to work perfectly:
image = (ImageView) findViewById(R.id.imageViewCompass);
image.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
shortclick();
}
});
image.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
longclick();
return true;
}
});
//Then the functions that are called:
public void shortclick()
{
Toast.makeText(this, "Why did you do that? That hurts!!!", Toast.LENGTH_LONG).show();
}
public void longclick()
{
Toast.makeText(this, "Why did you do that? That REALLY hurts!!!", Toast.LENGTH_LONG).show();
}
It seems that the easy way of declaring the item in XML as clickable and then defining a function to call on the click only applies to short clicks - you must have a listener to differentiate between short and long clicks.
Initially when i implemented a longClick and a click to perform two separate events the problem i face was that when i had a longclick , the application also performed the action to be performed for a simple click . The solution i realized was to change the return type of the longClick to true which is normally false by default . Change it and it works perfectly .
Change return false; to return true; in longClickListener
You long click the button, if it returns true then it does the work. If it returns false then it does it's work and also calls the short click and then the onClick also works.
Try using an ontouch listener instead of a clicklistener.
http://developer.android.com/reference/android/view/View.OnTouchListener.html
The simplest and updated method is using a long click listener like
someView.setOnLongClickListener {
//do your work
true
}

Categories

Resources