In android studio, I want to set some kind of 'timer' before the if/else statement runs parts of it's body. In this case the edit1.setText("") and edit1.setBackgroundColor(Color.parseColor("#FFFFFFF")); Is there a way to do this.
Thanks in advance, have a nice day!
EDIT: I'm not using any UI libraries.
public void check1(View view) {
uinput = edit1.getText().toString();
if (uinput.equals(answerList[0])) {
edit1.setBackgroundColor(Color.parseColor("#00FF00"));
score++;
score_view.setText(String.valueOf(score));
// I only want to delay the three below.
view1.setText(questionList[1]);
edit1.setText("");
edit1.setBackgroundColor(Color.parseColor("#FFFFFF"));
} else {
edit1.setBackgroundColor(Color.parseColor("#FF0000"));
wrong++;
wrong_view.setText(String.valueOf(wrong));
// I only want to delay the three below.
view1.setText(questionList[1]);
edit1.setText("");
edit1.setBackgroundColor(Color.parseColor("#FFFFFFF"));
}
}
Related
I am making an application that helps score a table tennis game. I am at the final stages but I am having trouble with switching the server around every two points. I have given it a lot of thought but I can only get it to switch once. I know it is probably an easy solution but it's just not coming to me.
Here's how I am switching it once. I am using a count each time the button is pressed and when it reaches a number divisible by 2 it switches to the right.. However, using this logic is making it difficult to switch back! Thanks in advance.
public void serveSwitch() {
TextView leftServe = findViewById(R.id.leftServe);
TextView rightServe = findViewById(R.id.rightServe);
serverCount++;
if (server.serve=="left") {
if (serverCount % 2 == 0) {
rightServe.setVisibility(View.VISIBLE);
leftServe.setVisibility(View.GONE);
}
}
The part I'm struggling with is the logic on how to switch visibility every two points
If I get your point right, you want to toggle the visibility from off to on every two points and vice versa
You can do something like:
...
if (server.serve=="left") {
if (serverCount % 2 == 0) {
switch (rightServe.getVisibility()) {
case View.GONE:
rightServe.setVisibility(View.VISIBLE);
break;
case View.VISIBLE:
rightServe.setVisibility(View.GONE);
break;
}
switch (leftServe.getVisibility()) {
case View.GONE:
leftServe.setVisibility(View.VISIBLE);
break;
case View.VISIBLE:
leftServe.setVisibility(View.GONE);
break;
}
}
}
Note: I left the equality as-is as you say there is no problem with it. but in general you should use .equals() when it comes to compare strings in java.
I'm creating an application which updates users on the score of a football match either in real time or as a final result. At least one score must be inputted in order for the TextView to be updated and the relevant score to be displayed. I'm checking that at least 1 of a pair of EditText fields is not empty using the following code:
if(!(et_current.getText().toString().isEmpty())||(!(et_final.getText().toString().isEmpty()))
&& (!(et_current2.getText().toString().isEmpty())||(!(et_final2.getText().toString().isEmpty()))){
if(!(et_final.getText().toString().isEmpty()))
tv_final.setText(et_final.getText().toString());
else
tv_current.setText(et_current.getText().toString());
if(!(et_final2.getText().toString().isEmpty()))
tv_final2.setText(et_final2.getText().toString());
else
tv_current2.setText(et_current2.getText().toString());
}
I want to be able to set the correct TextView so I have another if statement inside the original if statement to see ensure the correct score is being updated.
When I run the code, I do not seem to be getting past the first if statement. Am I using the correct format or is there an better way to complete these checks?
Thanks!
For readabilities sake, get some variables going
boolean currentEmpty = et_current.getText().toString().isEmpty();
boolean current2Empty = et_current2.getText().toString().isEmpty();
boolean finalEmpty = et_final.getText().toString().isEmpty();
boolean final2Empty = et_final2.getText().toString().isEmpty();
And then your code can be much cleaner. Something like
if( (!currentEmpty || !finalEmpty) || (!current2Empty || !final2Empty)) {
if(finalEmpty) {
tv_current.setText(et_current.getText());
}
else {
tv_final.setText(et_final.getText());
}
if(final2Empty) {
tv_current2.setText(et_current2.getText());
}
else {
tv_final2.setText(et_final2.getText());
}
}
I'm not sure if that is completely correct as the requirement is not entirely clear to me, but it should atleast be a good start to follow what's going on.
I am creating a mobile application that updates users with the current score and final score in a football match. I want the textview displaying the score to show only the current or final score.
I am having trouble with the if statement. I need one of the fields to contains something in order for a record to be created so I have:
if (!(et_currentgoals.getText().toString().isEmpty()) || !(et_finalgoals.getText().toString().isEmpty()){
}
Inside this if statement I was another that updates the textview with the correct values. So if the final number of goals was entered, the current goals are discarded. Would the best way be something like this:
if(!(et_finalgoals.getText().toString.isEmpty()){
tv_goals.setText(et_finalgoals.getText().toString();
}else{
tv_goals.setText(et_currentgoals.getText().toString();
}
Does this cover both scenarios or am I missing something?
Having that second if block inside the first will work, but there's a simpler way.
This single if will work.
if (!et_finalgoals.getText().toString.isEmpty()) {
tv_goals.setText(et_finalgoals.getText().toString();
} else if (!et_currentgoals.getText().toString.isEmpty()) {
tv_goals.setText(et_currentgoals.getText().toString();
}
In other words, these two blocks are equivalent
if (a || b) {
if (a) {
// a
} else {
// b
}
}
if (a) {
// a
} else if (b) {
// b
}
If I'm understanding correctly, you are executing additional code inside of that first if statement, after setting the tv_goals text. To do that now, you can just check the text of tv_goals.
if (!tv_goals.getText().toString.isEmpty()) {
// Do additional code
}
If this is the case, it ends up being the same amount of code as your original solution. You should just pick whichever way is more clear to you.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Syntax Question IF ELSE (Java)
I am trying to make a calculator which shows a message if no value is entered in editbox. But it FC's!! I am making apps after long time so I am quite confused.
private OnClickListener startListener = new OnClickListener() {
public void onClick(View v) {
double a=0;
double b=0;
double c=0;
EditText edit;
EditText edit2;
TextView edit3;
String lname="";
edit=(EditText)findViewById(R.id.edit);
edit2=(EditText)findViewById(R.id.edit2);
edit3=(TextView)findViewById(R.id.edit3); // everything defined above
String editstr= edit.getText().toString(); // real work starts
if(editstr.contentEquals(lname))
edit3.setText("Enter value");
else
a=Double.parseDouble(edit.getText().toString()); // else add the stuff
b=Double.parseDouble(edit2.getText().toString());
c=a+b;
edit3.setText(Double.toString(c));
} };
put brackets around your if-else, currently in case of else it only executes the first line, other lines are executed no matter if your if passes or fails.
if(editstr.contentEquals(lname)) {
edit3.setText("Enter value");
} else {
a=Double.parseDouble(edit.getText().toString()); // else add the stuff
b=Double.parseDouble(edit2.getText().toString());
c=a+b;
edit3.setText(Double.toString(c));
}
As written, only the a=Double.parseDouble(edit.getText().toString()); is affected by the else. If you want the rest of it there, surround the block in {}
It does work, it just never fires.
You should do
if(editStr.isEmpty())
editStr = "Enter value";
else
{
//editStr.equals("someValue"); //test against some value
//rest
}
I am relatively new to making applications for an Android phone and there is a problem that I have been trying to solve for the past week. What I am trying to do is take in 4 variables, 3 of which is from spinners and using while loops and case statements to search through the database to send a string into a TextView box. The main problem I have is the while loops after the button press errors out the program. I have tried implementing different ways like using a runnable and thread to work through it but have not had any success. I would greatly appreciate any help. The way the array is built is a 54x7.
public void onClick(View v) {
// TODO Auto-generated method stub
while(AutoDatabase[i][0] != YearSelect){
i++;
}
while(AutoDatabase[i][1] != MakeSelect){
i++;
}
while(AutoDatabase[i][2] != ModelSelect){
i++;
if (LightsOut == "FDTS"){
Part = AutoDatabase[i][3];
} else if (LightsOut == "FPTS"){
Part = AutoDatabase[i][4];
} else if (LightsOut == "RDTS"){
Part = AutoDatabase[i][5];
} else if (LightsOut == "RPTS"){
Part = AutoDatabase[i][6];
}
PartDisplay.setText(Part);
}