I added a switch button to my app and created a setOnCheckChangeListener the will pop up toast message..
But it works only after the second click.
Any ideas?
MainActivity:
public void checkSwitch (View view)
{
addBeeps.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
{
beeps = true;
Toast.makeText(MainActivity.this,"beeps is True",Toast.LENGTH_LONG).show();
}else
{
beeps = false;
Toast.makeText(MainActivity.this,"beeps is False",Toast.LENGTH_LONG).show();
}
}
});
}
I think in xml file which contains addBeeps switch you are using this code.
<Switch
android:id="#+id/addBeeps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="checkSwitch"/>
Root cause: For the first time when you check the switch, a listener will be set on it and do nothing, the second time when you check the switch again the listener is triggered and the Toast showed.
Solution: Here is a solution for you.
Remove onClick from the switch in xml file
<Switch
android:id="#+id/addBeeps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Delete or comment-out checkSwitch method from MainActivity and move set listener block code to onCreate method.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addBeeps = findViewById(R.id.addBeeps);
// TODO: Put set listener block code here
addBeeps.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
beeps = true;
Toast.makeText(MainActivity.this, "beeps is True", Toast.LENGTH_LONG).show();
} else {
beeps = false;
Toast.makeText(MainActivity.this, "beeps is False", Toast.LENGTH_LONG).show();
}
}
});
}
// TODO: Delete or comment-out this method
// public void checkSwitch(View view) {
// addBeeps.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
// #Override
// public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// if (isChecked) {
// beeps = true;
// Toast.makeText(MainActivity.this, "beeps is True", Toast.LENGTH_LONG).show();
// } else {
// beeps = false;
// Toast.makeText(MainActivity.this, "beeps is False", Toast.LENGTH_LONG).show();
// }
// }
// });
// }
Related
I have a program which creates a list of to-dos that allows the user to set a date/time for the app to notify them about it. There's a checkbox that says 'Notify me' which is supposed to schedule the notification. But, in the main RecyclerView of the list, there is also a toggle switch that allows the user to turn off/on the notification after they've saved it. Problem is, the toggle switch doesn't seem to be changing the notification state.
holder.notifSwitch.setChecked(journalModel.isNotify());
if(journalModel.getJournalDateNotify().getTime() > System.currentTimeMillis())
{
holder.notifSwitch.setVisibility(View.VISIBLE);
} else {
holder.notifSwitch.setVisibility(View.INVISIBLE);
}
holder.notifSwitch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!journalModel.isNotify())
{
createJournalFunction.updateJournal(realm, journalModel.getRealmJournalNo(),true);
} else
{
createJournalFunction.updateJournal(realm, journalModel.getRealmJournalNo(), false);
}
}
});
And here is the code for updating the Realm object:
public boolean updateJournal (Realm realm, final int realmJournalNo, final boolean isNotify){
success = false;
try{
realm.executeTransaction(new Realm.Transaction() {
#Override
public void execute(Realm realm) {
final TblJournal tblJournal = realm.where(TblJournal.class).equalTo("realmJournalNo", realmJournalNo).findFirst();
tblJournal.setNotify(isNotify);
success = true;
}
});
}catch (RealmException e){
Log.i("INFO","update Retail Exception: "+e.toString());
success = false;
}finally {
return success;
}
}
As per your code your are not updating journalModel.isNotify= true or false in on click. journalModel.setIsNotify(true):/journalModel.setIsNotify(false):
holder.notifSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// status="true"; //edit here
switch_btn.setChecked(true);
} else {
// status="false"; //edit here
switch_btn.setChecked(false);
}
}
});
Everything looks fine, you just need to modify your onClick method.
holder.notifSwitch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CheckBox checkbox = (CheckBox)v;
createJournalFunction.updateJournal(realm, journalModel.getRealmJournalNo(),checkbox.isChecked());
}
});
this is my code
ck1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.isChecked()) {
// perform logic
new LikeTask().execute();
ck1.setTag(R.drawable.fill_like);
ck1.setButtonDrawable(R.drawable.fill_like);
} else {
// perform logic
new UnLikeTask().execute();
ck1.setTag(R.drawable.like);
ck1.setButtonDrawable(R.drawable.like);
}
}
});
I have this code
Switch serviceSwitch;
serviceSwitch =(Switch) this.findViewById(R.id.switchservice);
serviceSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if(isChecked)
{
arrayChecking();
serviceSwitch.setChecked(false);
}
}
}
public void arrayChecking()
{
if(tbPreferenceArray.length>0)
{
Toast.makeText(getApplicationContext(), "Please Set the Time and Day", Toast.LENGTH_LONG).show();
}
else
{
Intent i = new Intent(MainActivity.this, TimerService.class);
MainActivity.this.startService(i);
Toast.makeText(getApplicationContext(), "Service started", Toast.LENGTH_LONG).show();
}
}
From the code, when the user turned on the switch the method call for arrayChecking() will execute and also there is the code to reset the switch to (false), but this code is not working, serviceSwitch.setChecked (false); is not working. Can anybody help me to solve this simple issue?? Sorry for the language
Modify your arrayChecking method a little bit:
public void arrayChecking(){
if(tbPreferenceArray.length>0){
Toast.makeText(getApplicationContext(), "Please Set the Time and Day", Toast.LENGTH_LONG).show();
//Add this to set the Switch checked to false
serviceSwitch.setChecked(false);
}
else{
Intent i = new Intent(MainActivity.this, TimerService.class);
MainActivity.this.startService(i);
Toast.makeText(getApplicationContext(), "Service started", Toast.LENGTH_LONG).show();
}
}
And remove the serviceSwitch.setChecked(false); from your onCheckedChanged method:
serviceSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if(isChecked)
{
arrayChecking();
}
}
}
I want to inspect something before CheckBox's state changed. If user has logged in, change the state and post data to server. Otherwise, keep the state and show a log-in activity.
OnCheckedChangeListener does not fit. Is there anything like PreferenceChangeListener (return false to keep stateļ¼ in CheckBox.
Or other methods.
Thank you!
Update:
OnClickListener work for me.
#Override
public void onClick(View view) {
boolean isChecked = checkBox.isChecked();
checkBox.setChecked(!isChecked);
if (loggedIn) {
checkBox.setChecked(isChecked);
// push data to server
} else {
// show log-in activity
}
}
If I'm not misunderstood your question, you can still achieve it by using OnCheckedChangeListener:
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isLogin) {
buttonView.setChecked(true);
Toast.makeText(this, "Sending data", Toast.LENGTH_SHORT).show();
} else {
buttonView.setChecked(false);
Toast.makeText(this, "Show login activity", Toast.LENGTH_SHORT)
.show();
}
}
You can do something like this:
CheckBox repeatChkBx = ( CheckBox ) findViewById( R.id.repeat_checkbox );
repeatChkBx.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
// perform logic
}
}
});
or if you just want to see if the checkbox has been checked or not, then you can use this
CheckBox checkBox = (CheckBox)v.findViewById(R.id.Checkbox);
checkBox.setChecked(!checkBox.isChecked());
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// awesome code you may use
// if (isChecked)
// (buttonView.getId() == R.id.check1 ? check2 : check1)
// .setChecked(false);
//Or use the code below
if (isChecked) {
switch (buttonView.getId()) {
case R.id.check1:
check2.setChecked(false);
Toast.makeText(getApplicationContext(), "Check1 is selected!!! =)",
Toast.LENGTH_LONG).show();
break;
case R.id.check2:
check1.setChecked(false);
Toast.makeText(getApplicationContext(), "Check2 is selected!!! =)",
Toast.LENGTH_LONG).show();
break;
default:
break;
}
}
}
I have checkboxes for week days in my android application , and I want to put a listener to check if any one of these checkboxes is checked but my way seems hard , isn't there a way to gather all these listeners into one
sun.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if ( isChecked )
{
count=count+1;
// perform logic
}
else
{
count=count-1;
}
}});
mon.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if ( isChecked )
{
count=count+1;
// perform logic
}
else
{
count=count-1;
}
}});
tue.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if ( isChecked )
{
count=count+1;
// perform logic
}
else
{
count=count-1;
}
}});
wed.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if ( isChecked )
{
count=count+1;
// perform logic
}
else
{
count=count-1;
}
}});
thu.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if ( isChecked )
{
count=count+1;
// perform logic
}
else
{
count=count-1;
}
}});
fri.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if ( isChecked )
{
count=count+1;
// perform logic
}
else
{
count=count-1;
}
}});
sat.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if ( isChecked )
{
count=count+1;
// perform logic
}
else
{
count=count-1;
}
}});
You can check the id of the buttonView that is being triggered and do the proper validation, all you have to do is assign the same onCheckListener to the checkboxes and do something as show below:
private OnCheckedChangeListener checkedListener = new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch(buttonView.getId()){
case R.id.checkId1:
//TODO: Code for checked one...
break;
case R.id.checkId2:
//TODO: Code for checked two...
break;
}
}
};
Hope it Helps!
Regards!
If you have similar logic that you haven't provided here, you can create a list of checkboxes and create listeners for them in a cycle, for example.
Let your activity implement the OnCheckedChangeListener and then you have the onCheckChanged...
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()) {
case R.id.sun:
break;
case R.id.mon:
break;
default:
break;
}
}
there is; implements View.OnClickListener with his method in your class
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.checbox1:
if(checkbo1.isCheck){
//do logic then dont forget to set to the opposite state
checkbox.setChecked(false);
}
else{
//do logic
checkbox.setChecked(true);
}
break;
//
case R.id.checbox2:
//do logic etc...
break;
}
}
then use a switch case deal between different click event from user
hope it help
Of course there is a simpler way. Just make your Activity where you are doing this implement OnCheckedChangeListener
public Class MyActivity extends Activity implements OnCheckedChangeListener{
//your activity logic in here
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//your Activity will now receive onCheckedChangeEvents here from
//the checkboxes to which you assign it as a listener
}
}
, in onCreate, after you get references to all of your day checkboxes, set the Activity as the listener like this
#Override
public void onCreate(Bundle savedInstanceState){
//first get references to all your checkboxes and assign them to mon, tue, wed etc.
//then set the listeners
mon.setOnCheckedChangeListener(this);
tue.setOnCheckedChangeListener(this);
wed.setOnCheckedChangeListener(this);
thu.setOnCheckedChangeListener(this);
fri.setOnCheckedChangeListener(this);
sat.setOnCheckedChangeListener(this);
sun.setOnCheckedChangeListener(this);
}
, make sure all of your checkboxes have IDs assigned to them in the layout xml, like this
<CheckBox
android:id="#+id/checkMonday"
....
, then you will have one onCheckedChange method where you can handle the different days like this
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int id = buttonView.getId();
if(isChecked){
counter++; //a day has been checked
}else{
counter--; //a day has been unchecked
}
switch(id){
case R.id.checkMonday:
//logic for Monday
break;
case R.id.checkTuesday:
//logic for Tuesday
break;
...
}
}
That should do the trick!