getch(); command is used in c++ to pause the program, What should i use in Android Studio to pause the program?
for (int i = 0; i < 2; i++){
if ((b[0].getText().toString() == c && b[1].getText().toString() == c && b[2].getText().toString() == c) || (b[3].getText().toString() == c && b[4].getText().toString() == c && b[5].getText().toString() == c) || (b[6].getText().toString() == c && b[7].getText().toString() == c && b[8].getText().toString() == c) || (b[0].getText().toString() == c && b[3].getText().toString() == c && b[6].getText().toString() == c) || (b[1].getText().toString() == c && b[4].getText().toString() == c && b[7].getText().toString() == c) || (b[2].getText().toString() == c && b[5].getText().toString() == c && b[8].getText().toString() == c) || (b[0].getText().toString() == c && b[4].getText().toString() == c && b[8].getText().toString() == c) || (b[2].getText().toString() == c && b[4].getText().toString() == c && b[6].getText().toString() == c)){
if (c == "X"){
paper.setText("You Won the Game.");
}
else{
paper.setText("Computer Won the Game.");
}
}
I want to pause the program where i am displaying the result of the game using paper.setText(); command.
If you are inside of an Activity simply call this.finish();
Edit: I'm not sure exactly what you're trying to do, but it's typically not good practice to close the program without the user consenting to do so. After you have shown your text it may be better to show a dialog such as:
"Would you like to play again?"
[Yes] [Close Game]
You can accomplish that with the following code:
new AlertDialog.Builder(this)
.setTitle("Play again?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// start game over
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
MyActivity.this.finish();
//use the class name of you activity + this
//to access your Activity's finish rather than try and perform finish on an onClick
}
})
.setIcon(android.R.drawable.myicon)
.show();
Related
I made a code to show the amount of times a vowel is used. A, E, I, O, U. The code is counting but it is not conunting correctly.
#Override
public void onClick(View v) {
int letter1 = 0;
int letter2 = 0;
String enter1 = str1.getText().toString();
String enter2 = str2.getText().toString();
for (int i = 0; i < str1.length(); i++) {
if (enter1.charAt(i) == 'a' || enter1.charAt(i) == 'e' || enter1.charAt(i) == 'i' || enter1.charAt(i) == 'o' ||
enter1.charAt(i) == 'u')
{
letter1++;
}
for(int j = 0; j < str2.length(); j ++){
if(enter2.charAt(j) == 'a' || enter2.charAt(j) == 'e' || enter2.charAt(j) == 'i' || enter2.charAt(j) == 'o' ||
enter2.charAt(j) == 'u')
{
letter2++;
}
}
display3.setText("The amount of vowels are :"+ letter1 + " & " + letter2);
}
}
});
My output for Hello World is 2 and 5. 2 for Hello is correct but 5 for World is not. What am I missing?
If we reformat the code slightly, the problem is more apparent:
for (int i = 0; i < str1.length(); i++) {
if (enter1.charAt(i) == 'a' || enter1.charAt(i) == 'e' || enter1.charAt(i) == 'i' || enter1.charAt(i) == 'o' ||
enter1.charAt(i) == 'u') {
letter1++;
}
for (int j = 0; j < str2.length(); j++) {
if (enter2.charAt(j) == 'a' || enter2.charAt(j) == 'e' || enter2.charAt(j) == 'i' || enter2.charAt(j) == 'o' ||
enter2.charAt(j) == 'u') {
letter2++;
}
}
display3.setText("The amount of vowels are :" + letter1 + " & " + letter2);
}
As you can see, the second for loop is inside the first one. That means the second number will be N times larger than it should be, where N is the length of the first word. This matches what's happening: there is only 1 vowel in "world" but there are 5 letters in "hello".
The display3.setText(...) part will also be executed N times, but since it overwrites the previous value you won't be able to notice that, and you'll just see the final value.
To fix, just close the first for loop earlier:
for (int i = 0; i < str1.length(); i++) {
if (enter1.charAt(i) == 'a' || enter1.charAt(i) == 'e' || enter1.charAt(i) == 'i' || enter1.charAt(i) == 'o' ||
enter1.charAt(i) == 'u') {
letter1++;
}
}
for (int j = 0; j < str2.length(); j++) {
if (enter2.charAt(j) == 'a' || enter2.charAt(j) == 'e' || enter2.charAt(j) == 'i' || enter2.charAt(j) == 'o' ||
enter2.charAt(j) == 'u') {
letter2++;
}
}
display3.setText("The amount of vowels are :" + letter1 + " & " + letter2);
Something problem with the brackets
#Override
public void onClick(View v) {
for(Loop){
if(condition){
}
}//first loop
for(Loop){
if(condition){
}
}//second loop
//display here
});//end for onClick
How can I use if statement of a spinner when the selected value = 0?
if( wilayat.setSelection(0) || city.setSelection(0) ||
station.setSelection(0) || distnation.setSelection(0)) {
message = "Select the 4 fields, please..";
Toast.makeText(BusArrivalTime.this, message, Toast.LENGTH_SHORT).show();
}
Try this.
if (wilayat.getSelectedItemPosition() == 0 || city.getSelectedItemPosition() == 0 || station.getSelectedItemPosition() == 0 || distnation.getSelectedItemPosition() == 0) {
message = "Select the 4 fields, please..";
Toast.makeText(BusArrivalTime.this, message, Toast.LENGTH_SHORT).show();
}
Just use getSelectedItemPosition() method.
You make sure that wilayat、city and so on were Spinner.
Add judgement
if (wilayat != null || city != null || station != null || distnation != null) {
// do something
}
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 WANT TO DIRECT IT TO ANOTHER CLASS
if (rbSingle.isChecked() || rbMarried.isChecked()) {
if (sqlName.getText().toString().length() == 0 && sqlcycle.getText().toString().length() == 0
&& sqlperiod.getText().toString().length() == 0 && sqlAge.getText().toString().length() == 0 && tvper.getText().toString().length() == 0) {
sqlName.setError("Please Input Name");
sqlcycle.setError("Please Input your Cycle Length");
sqlperiod.setError("Please Input your Period");
sqlAge.setError("Put your Birthday");
tvper.setError("Put your first day of last period");
} else if (sqlName.getText().toString().length() == 0 && sqlcycle.getText().toString().length() == 0) {
sqlName.setError("Please Input your Cycle Length");
sqlcycle.setError("Please Input your Period");
} else if (sqlName.getText().toString().length() == 0 && sqlperiod.getText().toString().length() == 0) {
sqlName.setError("Please Input your Cycle Length");
sqlperiod.setError("Please Input your Period");
} else if (sqlcycle.getText().toString().length() == 0 && sqlperiod.getText().toString().length() == 0) {
sqlcycle.setError("Please Input your Cycle Length");
sqlperiod.setError("Please Input your Period");
} else if (sqlName.getText().toString().length() == 0) {
sqlName.setError("Please Input your Cycle Length");
} else if (sqlcycle.getText().toString().length() == 0) {
sqlcycle.setError("Please Input your Cycle Length");
} else if (sqlperiod.getText().toString().length() == 0) {
sqlperiod.setError("Please Input your Cycle Length");
}
} else if(sqlName.getText().toString().length() == 0
&& sqlcycle.getText().toString().length() == 0 && sqlperiod.getText().toString().length() == 0) {
Toast.makeText(getApplicationContext(), "Please select your Gender", Toast.LENGTH_SHORT).show();
sqlName.setError("Please Input Name");
sqlcycle.setError("Please Input your Cycle Length");
sqlperiod.setError("Please Input your Period");
}else{
Intent main = new Intent(this, CalendarMain.class);
startActivity(main);
finish();
}
problem: why it wont direct to another page? after the codition i want it to direct to CalendarMain class. can you specify what's the problem in my code?
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)