I'm currently working on a school project in Android Studio and so far I've written a code which generates random equations.
Now I display two equations on the screen and the user then has to decide wether the second equation is bigger or smaller than the first one. If the second is bigger, the user presses the button 'bigger', if the second one is smaller, the user presses the button with 'smaller'.
Now I'm trying to write the code that if the user pressed correct, it generates a new equation, if he was wrong, the process stops.
I was thinking of if statements like so:
final ArrayList<String> arrayListCheck = new ArrayList<String>();
if(doubleAnswer1 > doubleAnswer2){
arrayListCheck.add("smaller");
} else {
arrayListCheck.add("bigger");
}
final Button buttonBigger = (Button)findViewById(R.id.button_bigger);
final Button buttonSmaller = (Button)findViewById(R.id.button_smaller);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
if(v.equals(buttonBigger)){
arrayListCheck.add("bigger");
} else {
arrayListCheck.add("smaller");
}
}
};
buttonBigger.setOnClickListener(listener);
buttonSmaller.setOnClickListener(listener);
In the arraylist arrayListCheck it will store either 'bigger' or 'smaller'. Now I want to check if the elements in the arraylist are both the same (either both 'bigger' or 'smaller'), if so a new equation will be generated. If the elements in the arraylist are different (not both the same), the process will be stopped.
I don't know if that really works, so it would be nice if someone could help me with this.
If there is anything unclear in my question, feel free to ask and I will try to clarify the problem :)
Thank you already in advance for your help!
I wouldn't use an ArrayList for that.
I would do the following:
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
if(v.equals(buttonBigger) && doubleAnswer1 < doubleAnswer2) {
Log.v("TAG", "you are right");
} else if(v.equals(buttonSmaller) && doubleAnswer1 > doubleAnswer2) {
Log.v("TAG", "you are right");
} else {
Log.v("TAG", "you are wrong");
}
}
};
Arrays are not really neccessary for this kind of simple comparison.
Related
I am creating an android application wherein I have mentioned the phone number, email, website of the business similar to the screenshot of google maps as attached.
Just like in the g maps where
Single click makes an intent to the respective app (opens dialer with phone number copied) etc.
Long Click/Press & hold will copy the number to clipboard displaying a toast that "Phone number is copied to clicpboard".
Now I have set up the intents properly and they are working fine --> step-1 achieved.
When I try to display the toast using "OnLongClickListener" method it is taking quite some time to display the toast message. Is there any way we can speed this up or am I missing something or should I use some other alternative like onTouch() method?
(Stuck with this, yet to learn and try out copying to clipboard thing. Inputs in this regards are also welcome!)
In the g maps app, the toast message appears almost instantly on press & hold. So the same is to be achieved.
The code extract from MainActivity.java (The website, phone number, email id are text views with drawableLeft. And I found out that we cannot use setOnItemLongClick on TextView as it is only for ListView)
// This method is called when the website text view is clicked and opens the compnay website
public void openWebsite(View v) {
TextView textView = (TextView) findViewById(R.id.webURL_text_view);
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent openWebsiteIntent = new Intent(Intent.ACTION_VIEW);
openWebsiteIntent.setData(Uri.parse("https://www.company.com"));
if (openWebsiteIntent.resolveActivity(getPackageManager()) != null) {
startActivity(openWebsiteIntent);
}
}
});
textView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
showMessage();
return true;
}
});
}
public void showMessage(){
Toast.makeText(this,"Information copied to Clipboard", Toast.LENGTH_LONG).show();
}
I am new to android development and just trying out something, so please be as elaborative as possible. Thank You.
I am creating a quiz game which is working fine,but the problem am encountering is that whenever the user selects the correct answer, its shows it as wrong answer,meanwhile it's the correct answer, it's turns out the correct answer is not at the correct location.
Here is my code so far, any ideas how i can fix this.
public class RiddleGuessActivity extends AppCompatActivity {
TextView riddleQuestionTextView;
Button btn1, btn2, btn3, btn4, btnContinue, btnOk;
ArrayList<String> riddles = new ArrayList<>();
ArrayList<String> answers = new ArrayList<>();
int locationOfCorrectAnswers = 0,incorrectAnswers = 0;
ArrayList<String> options = new ArrayList<>();
LinearLayout gameOverLayout;
LinearLayout buttonLayout;
RelativeLayout resultLayout;
TextView timerTextView;
TextView answerTextView;
TextView levelTextView, scoreTextView;
TextView gameOverTextView;
int levelCounter = 1, scoreCounter, riddleCounter = 0;
public void chosenRiddle (View view) {
if (view.getTag().toString().equals(Integer.toString(locationOfCorrectAnswers))) {
//Toast.makeText(RiddleGuessActivity.this, "Correct", Toast.LENGTH_SHORT).show();
levelCounter++;
scoreCounter+=60;
riddleCounter++;
levelTextView.setText(String.valueOf(levelCounter));
scoreTextView.setText(String.valueOf(scoreCounter));
result();
}else {
Toast.makeText(RiddleGuessActivity.this, "Wrong, It was " +
answers.get(riddleCounter), Toast.LENGTH_SHORT).show();
gameOver();
}
}
public void generateRiddles () {
riddles.add("I kiss my mother before i die. What am i ?");
riddles.add("It is greater than God and more evil than the devil. The poor have it, the rich need it and if you eat it you'll die. What am i ?");
riddles.add("It walks on four legs in the morning, two legs at noon and three legs in the evening. What am i ?");
riddles.add("I am the beginning of the end, and the end of time and space. I am essential to creation and i surround everyplace. What am i ?");
riddles.add("What always runs but never walks, often murmurs, never talks, has a bed but never sleeps, has a mouth but never eats. What am i ?");
riddles.add("At night they come without being fetched, By day they are lost without being stolen. What am i ?");
riddles.add("The one who makes it, sells it. The one who buys it, never uses it. The one that uses it never knows that he's using it. What am i ?");
riddles.add("The more you have of it, the less you see. What am i ?");
riddles.add("I am always hungry, i must be fed, the finger i touch will soon turn red. What am i ?");
riddles.add("If you break me, i do not stop working, if you touch me, i may snared, if you lose me, nothing will matter. What am i ?");
answers.add("Matches");
answers.add("Nothing");
answers.add("Man");
answers.add("Letter E");
answers.add("River");
answers.add("The Stars");
answers.add("A Coffin");
answers.add("Darkness");
answers.add("Fire");
answers.add("Heart");
answers.add("The Moon");
answers.add("Food");
answers.add("Love");
answers.add("Mouth");
answers.add("Money");
Random random = new Random();
riddleQuestionTextView.setText(riddles.get(riddleCounter));
locationOfCorrectAnswers = random.nextInt(4);
options.clear();
for (int i = 0; i < 4; i++) {
if (i == locationOfCorrectAnswers &&
!options.contains(answers.get(riddleCounter))) {
options.add(answers.get(riddleCounter));
}else {
incorrectAnswers = random.nextInt(answers.size());
while (incorrectAnswers == riddleCounter) {
incorrectAnswers = random.nextInt(answers.size());
}
if(!options.contains(answers.get(incorrectAnswers)))
options.add(answers.get(incorrectAnswers));
else --i;
}
}
btn1.setText(options.get(0));
btn2.setText(options.get(1));
btn3.setText(options.get(2));
btn4.setText(options.get(3));
}
Are you missing an onCreate method??
Anyways, I suggest you add the correctAnswer followed by the incorrect options. Basically, forget random numbers and while loops.
Then Collections.shuffle() the list.
Then scan the list for the location of the correct answer.
Working in Android Studio and I've run into a few small issues so I figured I would ask for help / advice.
I am currently working on a small testing application that would allow users to open the application and select practice test or timed test (as displayed in picture 1). If users select practice test they have unlimited time to select an answer and the systems tells them if they have picked the correct answer. If the user selects timed than the system allots a predetermined amount of time and after each question the score is added and calculated, here lies my issues.
1.) Right now my code loops endlessly, ideally (at least for the timed aspect) I want my code to go through the 5 questions scoring the correct or incorrect based on the users selection. Then I want to display the # correct and in correct.
So my question is two fold, first how do I go about preventing my code from looping endless so I can begin to record iterations as ++correctAnswer ++incorrect answer. And, when it comes time to display the results, should I create a new activity to display the results.
Android is a bit confusing to me, I hope I have provided enough information and I appreciate and helping advice.
Thank You
** Code **
private int currentQuestion;
private String[] questions;
private String[] answers;
private Button answerButton;
private Button questionButton;
private TextView questionView;
private TextView answerView;
private EditText answerText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
public void init()
{
questions = new String[]{"What is the acronym for Computer Science and Information Management?",
"What is the CSIT corse number of the class you are currently in?",
"What Android program do we use to create android applications?"};
answers = new String[]{"CSIT","2250", "Android Studio"};
currentQuestion = -1;
answerButton = (Button)findViewById(R.id.AnswerButton);
questionButton = (Button)findViewById(R.id.QuestionButton);
questionView = (TextView)
findViewById(R.id.QuestionTextView);
answerView = (TextView) findViewById(R.id.AnswerTextView);
answerText = (EditText) findViewById(R.id.AnswerText);
answerButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
checkAnswer();
}});
questionButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
showQuestion();
}});
}
// Display current quesiton
public void showQuestion()
{
currentQuestion++;
if(currentQuestion == questions.length)
currentQuestion =0;
questionView.setText(questions[currentQuestion]);
answerView.setText("");
answerText.setText("");
}
// Return true if answer is correct
public boolean isCorrect(String answer)
{
return (answer.equalsIgnoreCase(answers[currentQuestion]));
}
// Display correct or incorrec results
public void checkAnswer()
{
String answer = answerText.getText().toString();
if(isCorrect(answer))
answerView.setText("You are correct!");
else
answerView.setText("Sorry, the correct answer is "+answers[currentQuestion]);
}}
Home Screen
Quiz Screen
Just have a condition check if currentQuestion == 5 at the end of checkAnswer(). If so proceed to the results screen
I am developing an App using Eclipse. I have a page where it has different check boxes. I want the user if checking lest say options A and B and D then Activity 7 will open and if the user checks options A and C then Activity 5 will open.
Thank you
You can do so like this:
Get the IDs of the checkboxes. Then add an OnClickListener to the checkboxes like so:
OnClickListener checkBoxListener;
checkBoxListener = new OnClickListener()
{
#Override
public void onClick(View arg0) {
if (checkboxA.isChecked() && checkboxC.isChecked())
{
Intent i = new Intent(this,Activity5.class)
startActivity(i);
}
else if (checkboxA.isChecked() && checkboxB.isChecked() && checkboxD.isChecked())
{
Intent i = new Intent(this,Activity7.class)
startActivity(i);
}
}
};
checkboxA.setOnClickListener(checkBoxListener);
checkboxB.setOnClickListener(checkBoxListener);
checkboxC.setOnClickListener(checkBoxListener);
checkboxD.setOnClickListener(checkBoxListener);
Please give this a try.
I have a view to create an account. If the save button is clicked and any of the fields are left open, it displays a toast. If all fields are filled in, the account is saved. I tried to accomplish this with an onClickListener that has an iteration through all the fields. It works perfectly if a field is not filled in and it works perfectly if alle fields are filled, but when a field isn't filled, I type something in there, try to save again and the button doesn't do anything.
I think it has something to do with the return, but I don't know what to do else. If the return wouldn't be there, I would get a toast for each field that isn't filled in.
Here's the relevant code:
private void registerButtonListeners() {
mCRUDAccountButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < mEditTexts.length; i++) {
if(mEditTexts[i].getText().length() == 0){
CommonCode.showToast(mNoTextTitles[i], mContext, mViewGroup);
mEmptyField = 1;
return;
}
};
if (mEmptyField == 0){
saveState();
}
}
});
}
thanks guys!
You're never resetting your flag back to 0!
so...
#Override
public void onClick(View v) {
mEmptyField = 0;//RIGHT HERE (give them the benefit of the doubt)
for (int i = 0; i < mEditTexts.length; i++) {
if(mEditTexts[i].getText().length() == 0){
CommonCode.showToast(mNoTextTitles[i], mContext, mViewGroup);
mEmptyField = 1; //You were too optimistic, they failed.
return;
}
};
if (mEmptyField == 0){
saveState();
}
}
});
Now, you're doing this test for the first time, every time. Otherwise, you go through and set that flag to 1, and next time, even though your loop never finds a match, when you get to the if mPentyField == 0 test, it fails cause you set that to 1 in the previous go around.