I have a activity which contains 3 different CheckBox. Now I want to validate that at least 2 CheckBox must be selected but I am not able to achieve this. Please help.
Code:
Boolean checkPC = chkProtectMoney.isChecked();
Boolean checkGM = chkGrow_my_money.isChecked();
Boolean checkMN = chkMedicalNeeds.isChecked();
if(!(checkPC && checkGM == true) || !(checkGM && checkMN == true) || !(checkPC && checkMN == true) || !(checkPC && checkGM && checkPC == true)){
Toast.makeText(getActivity(),R.string.two_expertise_caution,Toast.LENGTH_SHORT).show();
}
private boolean checkIfAnyTwoAreChecked() {
int sum = 0;
if(checkbox1.isChecked()){ sum++; }
if(checkbox2.isChecked()){ sum++; }
if(checkbox3.isChecked()){ sum++; }
return sum >= 2;
}
You can Use a int count .
private int getCheckedCount() {
int count = 0;
if (checkBox1.isChecked())
count++;
if (checkBox2.isChecked())
count++;
if (checkBox3.isChecked())
count++;
return count;
}
if(getCheckedCount()>=2){
// Do your stuff
}
OR
if(checkBox1.isChecked() && checkBox2.isChecked()
|| checkBox1.isChecked() && checkBox3.isChecked()
|| checkBox2.isChecked() && checkBox3.isChecked()){
// Do stuff
}
Add checkedChangeListener for all checkboxes and update count based on check as in
int checkedCount = 0;
chkProtectMoney.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
checkedCount++;
}else{
checkedCount--;
}
if(checkedCount >= 2){
// voila
}else{
//alert to check atleast two
}
}
});
//add for other two same logic then
Change your conditional statement to this
if((checkPC && checkGM) || (checkGM && checkMN) || (checkPC && checkMN)) {
// YOUR CODE
}
Related
I want to get the background color of a button inside a function. I want to check the equality of the color to perform some action. I use PaintDrawer but its not working.. app crashing... this is my function
void moves(Button bn)
{
if(flag==1)
{
// Toast.makeText(getApplicationContext(),"Exception",Toast.LENGTH_LONG).show();
if (bn.getText().equals("RED") && plyr==1 && mov==0|| mov==1 && bn.getText().equals("RED"))
{
rembr = bn;
Toast.makeText(getApplicationContext(),"Exception"+rembr.getText(),Toast.LENGTH_LONG).show();
mov = 1;
}
else if (bn.getText().equals("GREEN") && plyr == 2 && mov == 0 || mov==1 && bn.getText().equals("GREEN"))
{
Toast.makeText(getApplicationContext(),"Exception GREEN",Toast.LENGTH_LONG).show();
rembr = bn;
mov = 1;
}
else if (mov == 1 && bn.getText() != "RED" && bn.getText() != "GREEN")
{
/*check
adjsnt(lbl, rembr);
end check*/
falsemov=adjsnt(bn, rembr);
if (falsemov == 1)
{
falsemov = 0;
}
else
{
mov = 0;
PaintDrawable drawable = (PaintDrawable) rembr.getBackground();
int temp = drawable.getPaint().getColor();
bn.setBackgroundColor(temp);
bn.setText(rembr.getText());
rembr.setBackgroundColor(Color.LTGRAY);
if (plyr == 1)
{
plyr = 2;
t1.setBackgroundColor(Color.LTGRAY);
t2.setBackgroundColor(Color.GREEN);
}
else
{
plyr = 1;
t2.setBackgroundColor(Color.LTGRAY);
t1.setBackgroundColor(Color.RED);
}
}
}
}
}
Any help would be highly appretiated.
use Color Drawable class instead of paint
ColorDrawable buttonColor = (ColorDrawable) button.getBackground();
int colorId = buttonColor.getColor();
I have a list of numbers, in that list I want to check if any of the numbers match, and if so return 'true'. Essentially, what I want to check is if the numbers match, then do not save. I've gotten a seemingly very inefficient method to check. Any help would be appreciated, thanks!
if (mFav1Compare == mNumber1Compare || mFav1Compare == mNumber2Compare || mFav1Compare == mNumber3Compare || mFav1Compare == mNumber4Compare || mFav1Compare == mNumber5Compare) {
return true;
}
if (mFav2Compare == mNumber1Compare || mFav2Compare == mNumber2Compare || mFav2Compare == mNumber3Compare || mFav2Compare == mNumber4Compare || mFav2Compare == mNumber5Compare) {
return true;
}
if (mFav3Compare == mNumber1Compare || mFav3Compare == mNumber2Compare || mFav3Compare == mNumber3Compare || mFav3Compare == mNumber4Compare || mFav3Compare == mNumber5Compare) {
return true;
}
if (mFav4Compare == mNumber1Compare || mFav4Compare == mNumber2Compare || mFav4Compare == mNumber3Compare || mFav4Compare == mNumber4Compare || mFav4Compare == mNumber5Compare) {
return true;
}
if (mFav5Compare == mNumber1Compare || mFav5Compare == mNumber2Compare || mFav5Compare == mNumber3Compare || mFav5Compare == mNumber4Compare || mFav5Compare == mNumber5Compare) {
return true;
}
If you have two lists and you want to know if the second list contains at least one element of the first list you could try
public <T> boolean listMatches(List<T> list1, List<T> list2) {
for (T element : list1) {
if (list2.contains(element)) {
return true;
}
}
return false;
}
Do you thought this?
ArrayList<Integer>listNumbers = new ArrayList<>();
listNumbers.add(99);
listNumbers.add(99);
listNumbers.add(11);
listNumbers.add(10);
for(int i = 0; i < listNumbers.size(); i++){
if(!listCheck(listNumbers.get(i))){
//save something
}
}
and this method
private boolean listCheck(int number){
for(int i = 0; i < listNumbers.size(); i++){
if(number == listNumbers.get(i)){
return true;
}
}
return false;
}
I made a simpel app that notifies users when they have to check something.
I managed to get the notifications sent with the right delay, but I also want the user to be able to turn off the notification 'bunch' (via a Switch) somewhere between the delays..
I wanted to do this with Handler.removeCallBacks, but it didn't do the job.
final Switch mySwitch = (Switch) findViewById(R.id.theswitch);
mySwitch.setChecked(myPrefs.getBoolean("switch", false));
//attach a listener to check for changes in state
mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
final boolean isChecked) {
editor.putBoolean("switch", isChecked);
editor.commit();
if (isChecked) {
//switch has been switched ON
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+1:00"));
Date currentLocalTime = cal.getTime();
DateFormat date = new SimpleDateFormat("HH:mm");
// you can get seconds by adding "...:ss" to it
date.setTimeZone(TimeZone.getTimeZone("GMT+1:00"));
String localTime = date.format(currentLocalTime);
Log.w(localTime, "1");
if (localTime.equals("08:50") || localTime.equals("08:51") || localTime.equals("08:52") || localTime.equals("08:53") || localTime.equals("08:54") || localTime.equals("08:55") || localTime.equals("08:56")
|| localTime.equals("08:57") || localTime.equals("08:58") || localTime.equals("08:59") || localTime.equals("09:00") || localTime.equals("09:01") || localTime.equals("09:02") || localTime.equals("09:03")
|| localTime.equals("09:04") || localTime.equals("09:05") || localTime.equals("09:06") || localTime.equals("09:07") || localTime.equals("09:08") || localTime.equals("09:09") || localTime.equals("09:10")
|| localTime.equals("09:11") || localTime.equals("09:12") || localTime.equals("09:13") || localTime.equals("09:14") || localTime.equals("09:15") || localTime.equals("09:16") || localTime.equals("09:17")
|| localTime.equals("09:18") || localTime.equals("09:19") || localTime.equals("09:20") || localTime.equals("09:21") || localTime.equals("09:22") || localTime.equals("09:23") || localTime.equals("09:24")
|| localTime.equals("09:25") || localTime.equals("09:26") || localTime.equals("09:27") || localTime.equals("09:28") || localTime.equals("09:29") || localTime.equals("09:30") || localTime.equals("09:31")
|| localTime.equals("09:32") || localTime.equals("09:33") || localTime.equals("09:34") || localTime.equals("09:35") || localTime.equals("09:36") || localTime.equals("09:37") || localTime.equals("09:38")
|| localTime.equals("09:39") || localTime.equals("09:40") || localTime.equals("09:41") || localTime.equals("09:42") || localTime.equals("09:43") || localTime.equals("09:44") || localTime.equals("09:45")
|| localTime.equals("09:46") || localTime.equals("09:47") || localTime.equals("09:48") || localTime.equals("09:49") || localTime.equals("09:50") || localTime.equals("09:51") || localTime.equals("09:52")
|| localTime.equals("09:53") || localTime.equals("09:54") || localTime.equals("09:55") || localTime.equals("09:56") || localTime.equals("09:57") || localTime.equals("09:58") || localTime.equals("09:59")
|| localTime.equals("10:00") || localTime.equals("10:01") || localTime.equals("10:02") || localTime.equals("10:03") || localTime.equals("10:04") || localTime.equals("10:05") || localTime.equals("10:06")
|| localTime.equals("10:07") || localTime.equals("10:08") || localTime.equals("10:09") || localTime.equals("10:10") || localTime.equals("14:16")) {
Log.w("It is between", "those times");
boolean beenfired2 = myPrefs.getBoolean("beenfired", false);
if (beenfired2 != true) {
Log.w("yes", "it is not equal to true");
// day 1
mHandler.postDelayed(mClickRunnable, 36000);
mHandler.postDelayed(mClickRunnable, 72000);
beenfired = true;
editor.putBoolean("beenfired", beenfired);
editor.commit();
} else {
Log.w("else", "else");
}
} else {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Inlopen.this);
// set title
alertDialogBuilder.setTitle("Alarm kan niet worden ingeschakeld");
// set dialog message
alertDialogBuilder
.setMessage("U kunt het alarm alleen inschakelen tussen 9:00 en 10:00 's ochtends.")
.setCancelable(false)
.setNegativeButton("Oke", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, just close
// the dialog box and do nothing
mySwitch.setChecked(false);
editor.putBoolean("switch", false);
editor.commit();
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
} else {
//switch has been switched OFF
beenfired = false;
mHandler.removeCallbacks(mClickRunnable);
editor.putBoolean("beenfired", beenfired);
editor.commit();
editor.putBoolean("switch", false);
editor.commit();
}
}
});
}
private Handler mHandler = new Handler();
private Runnable mClickRunnable = new Runnable() {
#Override
public void run() {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(Inlopen.this)
.setSmallIcon(R.drawable.iconsmall)
.setContentTitle("DM Voet App")
.setContentText("Uw moet uw voeten controleren!");
mBuilder.setAutoCancel(true);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mBuilder.setSound(alarmSound);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, mBuilder.build());
}
};
I always combine those two methods when stopping a handler :
mHandler.removeCallbacks(mClickRunnable);
mHandler.removeMessages(0);
To be honest, i dont really understand what is the difference between them, but its work for me. (and it will not work if i remove either one of them)
I'm trying to get an indexable list in my list view. I referred this. But while using the code, I'm facing with an error while using Korean Characters in the StringMatcher class. Can anyone explain me the usage of this class? Is this class required for English Characters as well?
Thanks in advance.
There are some changes to be done to make it work. In order to compile the project and get rid of korean text update the StringMatcher class
package com.woozzu.android.util;
public class StringMatcher {
public static boolean match(String value, String keyword) {
if (value == null || keyword == null)
return false;
if (keyword.length() > value.length())
return false;
int i = 0, j = 0;
do {
int vi = value.charAt(i);
int kj = keyword.charAt(j);
if (isKorean(vi) && isInitialSound(kj)) {
} else {
if (vi == kj) {
i++;
j++;
} else if (j > 0)
break;
else
i++;
}
} while (i < value.length() && j < keyword.length());
return (j == keyword.length())? true : false;
}
private static boolean isKorean(int i) {
return false;
}
private static boolean isInitialSound(int i) {
return false;
}
}
Here is My Code:
public boolean onTouchEvent(MotionEvent cv){
int x=(int)cv.getX();
int y=(int)cv.getY();
// here I want to use Log to gain the position(x,y) that I touched.But how?
if( 0<=y && y<=py ){
if( (px-(py-y)*Math.tan(30))<=x && x<=(px+(py-y)*Math.tan(30)) ){
Log.i(VIEW_LOG_TAG, "yes");
}
}
if( px<=x && x<=(px+py*Math.tan(15)) ){
if( y<=(Math.tan(15)*(px-x)+py) ){
Log.i(VIEW_LOG_TAG, "no");
}
}
return super.onTouchEvent(cv);
}
Log as... android.util.Log ?, if so:
Just add:
Log.v(String TAG, String MSG);
Like:
int x=(int)cv.getX();
int y=(int)cv.getY();
// here I want to use Log to gain the position(x,y) that I touched.But how?
Log.v("X", "pos x = " + x);
Log.v("Y", "pos y = " + y);
if( 0<=y && y<=py ){
if( (px-(py-y)*Math.tan(30))<=x && x<=(px+(py-y)*Math.tan(30)) ){
Log.i(VIEW_LOG_TAG, "yes");
}
}
if( px<=x && x<=(px+py*Math.tan(15)) ){
if( y<=(Math.tan(15)*(px-x)+py) ){
Log.i(VIEW_LOG_TAG, "no");
}
}
return super.onTouchEvent(cv);
}