Saving UI state when moving between screens/activities Android app - android

I am doing a multiscreen quiz app. For each question I have a separate activity / screen. At the bottom of each screen there are next/previous "buttons" which navigate to the next/previous screen. Please see the UI example of a screen with a question:
I have a problem though. Let's assume a user selects answers to a question 2 and then clicks "Previous", selects an answer in question 1 and hits "Next".
I would like to save the UI state of the Question 2, so the selected answer stays if a user comes back to a question either by clicking previous or next.
One thing I managed to accomplish is when a user clicks "previous" the UI stays, I used the following code in the manifest file:
android:launchMode="singleTask"
However I cannot make it saved when a user comes bak to a question via "next". Here is my code for the activity with the question 2:
package com.example.justynagolawska.quizappiteration2;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
public class Question2Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question2);
ActionBar actionbar = getSupportActionBar();
// Applies the custom action bar style
getSupportActionBar().setDisplayOptions(actionbar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.action_bar);
// Changes the action bar title
TextView title = (TextView) getSupportActionBar().getCustomView().findViewById(R.id.action_bar_title);
title.setText(R.string.q2_name);
//Getting the intent with score for question 1
Intent question2Intent = getIntent();
final int resultQ1 = question2Intent.getIntExtra("q1result", 0);
// Find the View that shows the next TextView
TextView nextQuestion = (TextView) findViewById(R.id.next);
// Set a click listener on that View
nextQuestion.setOnClickListener(new View.OnClickListener() {
// The code in this method will be executed when next View is clicked on.
#Override
public void onClick(View view) {
//Getting the answer to question 2 checkbox 1
CheckBox checkBox1Q2 = (CheckBox) findViewById(R.id.checkbox1Q2);
boolean isCheckBox1Q2 = checkBox1Q2.isChecked();
//Getting the answer to question 2 checkbox 2
CheckBox checkBox2Q2 = (CheckBox) findViewById(R.id.checkbox2Q2);
boolean isCheckBox2Q2 = checkBox2Q2.isChecked();
//Getting the answer to question 2 checkbox 3
CheckBox checkBox3Q2 = (CheckBox) findViewById(R.id.checkbox3Q2);
boolean isCheckBox3Q2 = checkBox3Q2.isChecked();
//Calculate Question 2 score
int resultQ2 = calculateResultQ2(isCheckBox1Q2, isCheckBox2Q2, isCheckBox3Q2);
Intent question3Intent = new Intent(Question2Activity.this, Question3Activity.class);
question3Intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
question3Intent.putExtra ("q1result", resultQ1);
question3Intent.putExtra ("q2result", resultQ2);
startActivity(question3Intent);
}
});
// Find the View that shows the next TextView
TextView previousQuestion = (TextView) findViewById(R.id.previous);
// Set a click listener on that View
previousQuestion.setOnClickListener(new View.OnClickListener() {
// The code in this method will be executed when next View is clicked on.
#Override
public void onClick(View view) {
Intent question1Intent = new Intent(Question2Activity.this, Question1Activity.class);
startActivity(question1Intent);
}
});
}
/**
* Check which checkbox was selected in the question 2
*
* #param checkBox1 is whether or not the user checked the checkbox1
* #param checkBox2 is whether or not the user checked the checkbox2
* #param checkBox3 is whether or not the user checked the checkbox3
* #return the score the user got for question 2
*/
private int calculateResultQ2(boolean checkBox1, boolean checkBox2, boolean checkBox3) {
int result = 0;
if (checkBox1 && checkBox2 && checkBox3) {
result = 1;
}
return result;
}
I would appreciate very much if anyone could help me out. Thank you!
EDIT: Below is my working code using sharedPreferences, the solution proposed by #tahsinRupam
package com.example.justynagolawska.quizappiteration2;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
public class Question2Activity extends AppCompatActivity {
SharedPreferences mypref;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question2);
final CheckBox checkBox1Q2 = (CheckBox) findViewById(R.id.checkbox1Q2);
final CheckBox checkBox2Q2 = (CheckBox) findViewById(R.id.checkbox2Q2);
final CheckBox checkBox3Q2 = (CheckBox) findViewById(R.id.checkbox3Q2);
mypref = PreferenceManager.getDefaultSharedPreferences(this);
ActionBar actionbar = getSupportActionBar();
// Applies the custom action bar style
getSupportActionBar().setDisplayOptions(actionbar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.action_bar);
// Changes the action bar title
TextView title = (TextView) getSupportActionBar().getCustomView().findViewById(R.id.action_bar_title);
title.setText(R.string.q2_name);
//Getting the intent with score for question 1
Intent question2Intent = getIntent();
final int resultQ1 = question2Intent.getIntExtra("q1result", 0);
// Find the View that shows the next TextView
TextView nextQuestion = (TextView) findViewById(R.id.next);
// Set a click listener on that View
nextQuestion.setOnClickListener(new View.OnClickListener() {
// The code in this method will be executed when next View is clicked on.
#Override
public void onClick(View view) {
//Getting the answer to question 2 checkbox 1
boolean isCheckBox1Q2 = checkBox1Q2.isChecked();
//Getting the answer to question 2 checkbox 2
boolean isCheckBox2Q2 = checkBox2Q2.isChecked();
//Getting the answer to question 2 checkbox 3
boolean isCheckBox3Q2 = checkBox3Q2.isChecked();
//Calculate Question 2 score
int resultQ2 = calculateResultQ2(isCheckBox1Q2, isCheckBox2Q2, isCheckBox3Q2);
Intent question3Intent = new Intent(Question2Activity.this, Question3Activity.class);
question3Intent.putExtra ("q1result", resultQ1);
question3Intent.putExtra ("q2result", resultQ2);
startActivity(question3Intent);
}
});
// Find the View that shows the next TextView
TextView previousQuestion = (TextView) findViewById(R.id.previous);
// Set a click listener on that View
previousQuestion.setOnClickListener(new View.OnClickListener() {
// The code in this method will be executed when next View is clicked on.
#Override
public void onClick(View view) {
Intent question1Intent = new Intent(Question2Activity.this, Question1Activity.class);
startActivity(question1Intent);
}
});
}
/**
* Check which checkbox was selected in the question 2
*
* #param checkBox1 is whether or not the user checked the checkbox1
* #param checkBox2 is whether or not the user checked the checkbox2
* #param checkBox3 is whether or not the user checked the checkbox3
* #return the score the user got for question 2
*/
private int calculateResultQ2(boolean checkBox1, boolean checkBox2, boolean checkBox3) {
int result = 0;
if (checkBox1 && checkBox2 && checkBox3) {
result = 1;
}
return result;
}
#Override
protected void onPause() {
super.onPause();
//Getting the answer to question 2 checkbox 1
CheckBox checkBox1Q2 = (CheckBox) findViewById(R.id.checkbox1Q2);
boolean isCheckBox1Q2 = checkBox1Q2.isChecked();
//Getting the answer to question 2 checkbox 2
CheckBox checkBox2Q2 = (CheckBox) findViewById(R.id.checkbox2Q2);
boolean isCheckBox2Q2 = checkBox2Q2.isChecked();
//Getting the answer to question 2 checkbox 3
CheckBox checkBox3Q2 = (CheckBox) findViewById(R.id.checkbox3Q2);
boolean isCheckBox3Q2 = checkBox3Q2.isChecked();
if(isCheckBox1Q2 == true){
mypref.edit().putBoolean("Iscb1Checked", true).apply();
}
else if(isCheckBox1Q2 == false){
mypref.edit().putBoolean("Iscb1Checked", false).apply();
}
if(isCheckBox2Q2 == true){
mypref.edit().putBoolean("Iscb2Checked", true).apply();
}
else if(isCheckBox2Q2 == false){
mypref.edit().putBoolean("Iscb2Checked", false).apply();
}
if(isCheckBox3Q2 == true){
mypref.edit().putBoolean("Iscb3Checked", true).apply();
}
else if(isCheckBox3Q2 == false){
mypref.edit().putBoolean("Iscb3Checked", false).apply();
}
}
#Override
protected void onResume() {
super.onResume();
//Getting the answer to question 2 checkbox 1
CheckBox checkBox1Q2 = (CheckBox) findViewById(R.id.checkbox1Q2);
//Getting the answer to question 2 checkbox 2
CheckBox checkBox2Q2 = (CheckBox) findViewById(R.id.checkbox2Q2);
//Getting the answer to question 2 checkbox 3
CheckBox checkBox3Q2 = (CheckBox) findViewById(R.id.checkbox3Q2);
if(mypref.contains("Iscb1Checked")){
if(mypref.getBoolean("Iscb1Checked",false)){
checkBox1Q2.setChecked(true);
}
}
if(mypref.contains("Iscb2Checked")){
if(mypref.getBoolean("Iscb2Checked",false)){
checkBox2Q2.setChecked(true);
}
}
if(mypref.contains("Iscb3Checked")){
if(mypref.getBoolean("Iscb3Checked",false)){
checkBox3Q2.setChecked(true);
}
}
}
}
Please note that I replaced the below in the onPause(); method as I was getting null exception:
checkBox1Q2.isChecked()
!checkBox1Q2.isChecked()
with
isCheckBox1Q2 == true
isCheckBox1Q2 == false

You can use SharedPreference to store your value.
1) Declare checkbox and SharedPreference publicly:
CheckBox checkBox1Q2;
CheckBox checkBox2Q2;
CheckBox checkBox3Q2;
SharedPreferences mypref;
2) In your onCreate() initialize SharedPreference:
mypref = PreferenceManager.getDefaultSharedPreferences(this);
3) Save Checkbox states in onPause() (cause onPause() is called when the back button is pressed). You could declare in onStop() or onDestroy() too.
#Override
protected void onPause() {
super.onPause();
if(checkBox1Q2.isChecked()){
mypref.edit().putBoolean("Iscb1Checked", true).apply();
}
else if(!checkBox1Q2.isChecked()){
mypref.edit().putBoolean("Iscb1Checked", false).apply();
}
if(checkBox2Q2.isChecked()){
mypref.edit().putBoolean("Iscb2Checked", true).apply();
}
else if(!checkBox2Q2.isChecked()){
mypref.edit().putBoolean("Iscb2Checked", false).apply();
}
if(checkBox3Q2.isChecked()){
mypref.edit().putBoolean("Iscb3Checked", true).apply();
}
else if(!checkBox3Q2.isChecked()){
mypref.edit().putBoolean("Iscb3Checked", false).apply();
}
}
4) Get CheckBox states in onResume() method:
#Override
protected void onResume() {
super.onResume();
if(mypref.contains("Iscb1Checked")){
if(mypref.getBoolean("Iscb1Checked",false)){
checkBox1Q2.setChecked(true);
}
}
if(mypref.contains("Iscb2Checked")){
if(mypref.getBoolean("Iscb2Checked",false)){
checkBox2Q2.setChecked(true);
}
}
if(mypref.contains("Iscb3Checked")){
if(mypref.getBoolean("Iscb3Checked",false)){
checkBox3Q2.setChecked(true);
}
}
}
You're now getting the Checkbox view in onClick. Try to avoid that. Always get views (findViewById) in the initial part of onCreate (After setContentView).

Related

Showing results for a quiz app (Android Studio)

I have a quiz app that is working properly, but the thing is the user must answer all questions correctly in order to win the game(if the player gets it wrong the game will be over) .
What I wanted to do is have the questions answered and then at the end there will be an activity that will show how many the player has answered then there will be the options to retry and go back to menu
This is the code for the maingameactivity
public class MainGameActivity extends AppCompatActivity {
FButton buttonA, buttonB, buttonC, buttonD;
TextView questionText, triviaQuizText, timeText, resultText, coinText;
TriviaQuizHelper triviaQuizHelper;
TriviaQuestion currentQuestion;
List<TriviaQuestion> list;
int qid = 0;
int timeValue = 20;
int coinValue = 0;
CountDownTimer countDownTimer;
Typeface tb, sb;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_main);
//Initializing variables
questionText = (TextView) findViewById(R.id.triviaQuestion);
buttonA = (FButton) findViewById(R.id.buttonA);
buttonB = (FButton) findViewById(R.id.buttonB);
buttonC = (FButton) findViewById(R.id.buttonC);
buttonD = (FButton) findViewById(R.id.buttonD);
triviaQuizText = (TextView) findViewById(R.id.triviaQuizText);
timeText = (TextView) findViewById(R.id.timeText);
resultText = (TextView) findViewById(R.id.resultText);
coinText = (TextView) findViewById(R.id.coinText);
//Setting typefaces for textview and buttons - this will give stylish fonts on textview and button etc
tb = Typeface.createFromAsset(getAssets(), "fonts/TitilliumWeb-Bold.ttf");
sb = Typeface.createFromAsset(getAssets(), "fonts/shablagooital.ttf");
triviaQuizText.setTypeface(sb);
questionText.setTypeface(tb);
buttonA.setTypeface(tb);
buttonB.setTypeface(tb);
buttonC.setTypeface(tb);
buttonD.setTypeface(tb);
timeText.setTypeface(tb);
resultText.setTypeface(sb);
coinText.setTypeface(tb);
//Our database helper class
triviaQuizHelper = new TriviaQuizHelper(this);
//Make db writable
triviaQuizHelper.getWritableDatabase();
//It will check if the ques,options are already added in table or not
//If they are not added then the getAllOfTheQuestions() will return a list of size zero
if (triviaQuizHelper.getAllOfTheQuestions().size() == 0) {
//If not added then add the ques,options in table
triviaQuizHelper.allQuestion();
}
//This will return us a list of data type TriviaQuestion
list = triviaQuizHelper.getAllOfTheQuestions();
//Now we gonna shuffle the elements of the list so that we will get questions randomly
Collections.shuffle(list);
//currentQuestion will hold the que, 4 option and ans for particular id
currentQuestion = list.get(qid);
//countDownTimer
countDownTimer = new CountDownTimer(22000, 1000) {
public void onTick(long millisUntilFinished) {
//here you can have your logic to set text to timeText
timeText.setText(String.valueOf(timeValue) + "\"");
//With each iteration decrement the time by 1 sec
timeValue -= 1;
//This means the user is out of time so onFinished will called after this iteration
if (timeValue == -1) {
//Since user is out of time setText as time up
resultText.setText(getString(R.string.timeup));
//Since user is out of time he won't be able to click any buttons
//therefore we will disable all four options buttons using this method
disableButton();
}
}
//Now user is out of time
public void onFinish() {
//We will navigate him to the time up activity using below method
timeUp();
}
}.start();
//This method will set the que and four options
updateQueAndOptions();
}
public void updateQueAndOptions() {
//This method will setText for que and options
questionText.setText(currentQuestion.getQuestion());
buttonA.setText(currentQuestion.getOptA());
buttonB.setText(currentQuestion.getOptB());
buttonC.setText(currentQuestion.getOptC());
buttonD.setText(currentQuestion.getOptD());
timeValue = 20;
//Now since the user has ans correct just reset timer back for another que- by cancel and start
countDownTimer.cancel();
countDownTimer.start();
//set the value of coin text
coinText.setText(String.valueOf(coinValue));
//Now since user has ans correct increment the coinvalue
coinValue++;
}
//Onclick listener for first button
public void buttonA(View view) {
//compare the option with the ans if yes then make button color green
if (currentQuestion.getOptA().equals(currentQuestion.getAnswer())) {
buttonA.setButtonColor(ContextCompat.getColor(getApplicationContext(),R.color.lightGreen));
//Check if user has not exceeds the que limit
if (qid < list.size() - 1) {
//Now disable all the option button since user ans is correct so
//user won't be able to press another option button after pressing one button
disableButton();
//Show the dialog that ans is correct
correctDialog();
}
//If user has exceeds the que limit just navigate him to GameWon activity
else {
gameWon();
}
}
//User ans is wrong then just navigate him to the PlayAgain activity
else {
gameLostPlayAgain();
}
}
//Onclick listener for sec button
public void buttonB(View view) {
if (currentQuestion.getOptB().equals(currentQuestion.getAnswer())) {
buttonB.setButtonColor(ContextCompat.getColor(getApplicationContext(),R.color.lightGreen));
if (qid < list.size() - 1) {
disableButton();
correctDialog();
} else {
gameWon();
}
} else {
gameLostPlayAgain();
}
}
//Onclick listener for third button
public void buttonC(View view) {
if (currentQuestion.getOptC().equals(currentQuestion.getAnswer())) {
buttonC.setButtonColor(ContextCompat.getColor(getApplicationContext(),R.color.lightGreen));
if (qid < list.size() - 1) {
disableButton();
correctDialog();
} else {
gameWon();
}
} else {
gameLostPlayAgain();
}
}
//Onclick listener for fourth button
public void buttonD(View view) {
if (currentQuestion.getOptD().equals(currentQuestion.getAnswer())) {
buttonD.setButtonColor(ContextCompat.getColor(getApplicationContext(),R.color.lightGreen));
if (qid < list.size() - 1) {
disableButton();
correctDialog();
} else {
gameWon();
}
} else {
gameLostPlayAgain();
}
}
//This method will navigate from current activity to GameWon
public void gameWon() {
Intent intent = new Intent(this, GameWon.class);
startActivity(intent);
finish();
}
//This method is called when user ans is wrong
//this method will navigate user to the activity PlayAgain
public void gameLostPlayAgain() {
Intent intent = new Intent(this, PlayAgain.class);
startActivity(intent);
finish();
}
//This method is called when time is up
//this method will navigate user to the activity Time_Up
public void timeUp() {
Intent intent = new Intent(this, Time_Up.class);
startActivity(intent);
finish();
}
//If user press home button and come in the game from memory then this
//method will continue the timer from the previous time it left
#Override
protected void onRestart() {
super.onRestart();
countDownTimer.start();
}
//When activity is destroyed then this will cancel the timer
#Override
protected void onStop() {
super.onStop();
countDownTimer.cancel();
}
//This will pause the time
#Override
protected void onPause() {
super.onPause();
countDownTimer.cancel();
}
//On BackPressed
#Override
public void onBackPressed() {
Intent intent = new Intent(this, HomeScreen.class);
startActivity(intent);
finish();
}
//This dialog is show to the user after he ans correct
public void correctDialog() {
final Dialog dialogCorrect = new Dialog(MainGameActivity.this);
dialogCorrect.requestWindowFeature(Window.FEATURE_NO_TITLE);
if (dialogCorrect.getWindow() != null) {
ColorDrawable colorDrawable = new ColorDrawable(Color.TRANSPARENT);
dialogCorrect.getWindow().setBackgroundDrawable(colorDrawable);
}
dialogCorrect.setContentView(R.layout.dialog_correct);
dialogCorrect.setCancelable(false);
dialogCorrect.show();
//Since the dialog is show to user just pause the timer in background
onPause();
TextView correctText = (TextView) dialogCorrect.findViewById(R.id.correctText);
FButton buttonNext = (FButton) dialogCorrect.findViewById(R.id.dialogNext);
//Setting type faces
correctText.setTypeface(sb);
buttonNext.setTypeface(sb);
//OnCLick listener to go next que
buttonNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//This will dismiss the dialog
dialogCorrect.dismiss();
//it will increment the question number
qid++;
//get the que and 4 option and store in the currentQuestion
currentQuestion = list.get(qid);
//Now this method will set the new que and 4 options
updateQueAndOptions();
//reset the color of buttons back to white
resetColor();
//Enable button - remember we had disable them when user ans was correct in there particular button methods
enableButton();
}
});
}
//This method will make button color white again since our one button color was turned green
public void resetColor() {
buttonA.setButtonColor(ContextCompat.getColor(getApplicationContext(),R.color.white));
buttonB.setButtonColor(ContextCompat.getColor(getApplicationContext(),R.color.white));
buttonC.setButtonColor(ContextCompat.getColor(getApplicationContext(),R.color.white));
buttonD.setButtonColor(ContextCompat.getColor(getApplicationContext(),R.color.white));
}
//This method will disable all the option button
public void disableButton() {
buttonA.setEnabled(false);
buttonB.setEnabled(false);
buttonC.setEnabled(false);
buttonD.setEnabled(false);
}
//This method will all enable the option buttons
public void enableButton() {
buttonA.setEnabled(true);
buttonB.setEnabled(true);
buttonC.setEnabled(true);
buttonD.setEnabled(true);
}
}
Edited
Just remove the wrapper if else inside all the buttons better to keep it as, don't repeat the code. I am assuming the screen that shows result is handled inside gameWon and you have implemented functionality for inCorrectDialog
public void buttonA(View view) {
Button button = (Button) view;
buttonPressed(button);
}
public void buttonB(View view) {
Button button = (Button) view;
buttonPressed(button);
}
public void buttonC(View view) {
Button button = (Button) view;
buttonPressed(button);
}
public void buttonD(View view) {
Button button = (Button) view;
buttonPressed(button);
}
public void buttonPressed(Button button) {
button.setButtonColor(ContextCompat.getColor(getApplicationContext(), R.color.lightGreen));
if (qid < list.size() - 1) {
disableButton();
if (currentQuestion.getOptA().equals(currentQuestion.getAnswer())) {
correctDialog();
} else {
inCorrectDialog();
}
} else {
gameWon();
}
}

unable to deploy data in an intent

problem: I set point breaks at the following code:
intent.putExtra(WorkoutRoutinesActivity.EXTRA_WORKOUT_NAME, workoutName);
intent.putExtra(WorkoutRoutinesActivity.EXTRA_WORKOUT_DAYS, workoutDays);
, both showed up as null when I ran the app in debug mode. workoutName contains a simple String that is passed to a new activity, whereas workoutDays constains an array of String.
the full code is provided below:
public class CreateWorkoutActivity extends Activity {
public final String TAG = this.getClass().getSimpleName();
protected String[] workoutDays = new String[7];
protected String workoutName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_workout);
Button mNextButton = (Button) findViewById(R.id.next_button1);
CheckBox satBox = (CheckBox) findViewById(R.id.sat_checkbox);
CheckBox sunBox = (CheckBox) findViewById(R.id.sun_checkbox);
CheckBox monBox = (CheckBox) findViewById(R.id.mon_checkbox);
CheckBox tuesBox = (CheckBox) findViewById(R.id.tues_checkbox);
CheckBox wedBox = (CheckBox) findViewById(R.id.wed_checkbox);
CheckBox thursBox = (CheckBox) findViewById(R.id.thurs_checkbox);
CheckBox friBox = (CheckBox) findViewById(R.id.fri_checkbox);
final EditText mWorkoutName = (EditText) findViewById(R.id.workout_name1);
workoutName = mWorkoutName.getText().toString();
Log.i(TAG, workoutName);
if (satBox.isChecked()) {
workoutDays[0] = new String(satBox.getText().toString());
}
if (sunBox.isChecked()) {
workoutDays[1] = new String(sunBox.getText().toString());
}
if (monBox.isChecked()) {
workoutDays[2] = new String(monBox.getText().toString());
}
if (tuesBox.isChecked()) {
workoutDays[3] = new String(tuesBox.getText().toString());
}
if (wedBox.isChecked()) {
workoutDays[4] = wedBox.getText().toString();
}
if (thursBox.isChecked()) {
workoutDays[5] = satBox.getText().toString();
}
if (friBox.isChecked()) {
workoutDays[6] = friBox.getText().toString();
Log.i(TAG, workoutDays[6]);
}
mNextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i(TAG, workoutDays.toString());
Intent intent = new Intent(CreateWorkoutActivity.this, WorkoutRoutinesActivity.class);
intent.putExtra(WorkoutRoutinesActivity.EXTRA_WORKOUT_NAME, workoutName);
intent.putExtra(WorkoutRoutinesActivity.EXTRA_WORKOUT_DAYS, workoutDays);
Log.i(TAG, workoutDays.toString());
startActivity(intent);
}
});
}
The problem is not in the intent, but in the way you obtain workoutName (this is the null value). You create the activity, set up final EditText mWorkoutName = (EditText) findViewById(R.id.workout_name1); and then immediately ask for the input value through workoutName = mWorkoutName.getText().toString();, but at this time the user still hasn't entered anything. You should put that second line in the listener below (so its activated only after the user presses mNextButton. It's a good idea to put some check after it and send a message to user that they need to fill in that field (if it is indeed necessary).
Looks like the values for workoutName and workoutDays are not filled in initially when the view is created. You should move retrieving the value from the text fields to your onClickListener function.
you checking the CheckBox and EditText in onCreate, absolutely the EditText will be empty and all CheckBox it not checked

If-else statement and visibility - android

I have five checkboxes in my app along with five progressbars, and have called visibility according to checkboxes to make progressbars visible/gone. so is there any alternative to if-else statements to use in visibility method , cus in that case there are endless possibilities in the five checkboxes so i have to write endless if else statements in order to make the progressbars visible/gone in all the possibilities.
for example- if CheckBox1&checkbox2 are checked, make progressbar2 visible
if checkbox1&checkbox3 are checked, make progressbar2 visible
in other words, i want to make Progressbar 1 visible if any One of the checkboxes are checked and progressbar 2 visible if any two are checked and so on..
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ProgressBar;
public class Progress extends Activity {
ProgressBar progressBar1;
ProgressBar progressBar2;
CheckBox checkBox1;
CheckBox checkBox2;
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
SharedPreferences setprefsd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_progress);
setprefsd = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
progressBar1 = (ProgressBar) findViewById(R.id.progressBar1);
progressBar2 = (ProgressBar) findViewById(R.id.progressBar2);
checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
if (setprefsd.getBoolean("FirstCheckBox", false) == true) {
checkBox1.setChecked(true);
updateProgressBars();
}
if (setprefsd.getBoolean("SecondCheckBox", false) == true) {
checkBox2.setChecked(true);
updateProgressBars();
}
}
public void updateProgressBars() {
progressBar1.setVisibility(View.GONE);
progressBar2.setVisibility(View.GONE);
if (checkBox1.isChecked() && checkBox2.isChecked()) {
progressBar2.setVisibility(View.VISIBLE);
} else if (checkBox1.isChecked()) {
progressBar1.setVisibility(View.VISIBLE);
}
}
}
Short answer
You can use the ternary operator to make the visibility change simpler (instead of ifs).
I guess the approach here would be to count the number of checkboxes, then set the visibilities. You would just have to change your updateProgressBars() method like this:
public void updateProgressBars() {
int nbCheckboxes = 0;
if (checkBox1.isChecked())
nbCheckboxes++;
if (checkBox2.isChecked())
nbCheckboxes++;
progressBar1.setVisibility(nbCheckboxes >= 1 ? View.VISIBLE : View.GONE);
progressBar2.setVisibility(nbCheckboxes >= 2 ? View.VISIBLE : View.GONE);
}
Other remarks
IF statement with boolean expressiosn
Your are using constructs such as:
if (myBooleanExpression == true)
The == true is not needed because you're using a boolean expression here. Use instead:
if (myBooleanExpression)
Using arrays for your variables
If you use several numbered variables that have the same kind of purpose, you might want to consider using arrays.
public class Progress extends Activity {
ProgressBar[] progressBars;
CheckBox[] checkBoxes;
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
SharedPreferences setprefsd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_progress);
setprefsd = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
progressBars = new ProgressBar[3]; // 3, or whatever number you have
progressBars[0] = (ProgressBar) findViewById(R.id.progressBar1);
progressBars[1] = (ProgressBar) findViewById(R.id.progressBar2);
progressBars[2] = (ProgressBar) findViewById(R.id.progressBar3);
checkBoxes = new CheckBox[3]; // 3 or whatever number you have
checkBoxes[0] = (CheckBox) findViewById(R.id.checkBox1);
checkBoxes[1] = (CheckBox) findViewById(R.id.checkBox2);
checkBoxes[2] = (CheckBox) findViewById(R.id.checkBox3);
checkBoxes[0].setChecked(setprefsd.getBoolean("FirstCheckBox", false));
checkBoxes[1].setChecked(setprefsd.getBoolean("SecondCheckBox", false));
checkBoxes[2].setChecked(setprefsd.getBoolean("ThirdCheckBox", false));
updateProgressBars();
}
public void updateProgressBars() {
int nbCheckBoxes = 0;
for (CheckBox cb : checkBoxes) {
if (cb.isChecked())
nbCheckBoxes++;
}
for (int i = 0; i < progressBars.length; i++) {
progressBars[i].setVisibility(nbCheckboxes > i ? View.VISIBLE : View.GONE);
}
}
}
If you have 3 scenarios to check condition with 9 times.
Like:
if (!mCallType.isIncall() && mCallType.isOutcall() && mCallType.isTelecall()) {
glInCall.setVisibility(View.GONE);
glOutCall.setVisibility(View.VISIBLE);
glTeleCall.setVisibility(View.VISIBLE);
} else if (mCallType.isIncall() && !mCallType.isOutcall() && mCallType.isTelecall()) {
glOutCall.setVisibility(View.GONE);
glInCall.setVisibility(View.VISIBLE);
glTeleCall.setVisibility(View.VISIBLE);
} else if (mCallType.isIncall() && mCallType.isOutcall() && !mCallType.isTelecall()) {
glTeleCall.setVisibility(View.GONE);
glOutCall.setVisibility(View.VISIBLE);
glInCall.setVisibility(View.VISIBLE);
} else {
glOutCall.setVisibility(View.VISIBLE);
glInCall.setVisibility(View.VISIBLE);
glTeleCall.setVisibility(View.VISIBLE);
}
then don't use "IF, ELSE IF" condition.
The Best Replacement is HashMap
HashMap visiblityManager = new HashMap<>(); // Visibility Manage without if else condition.
visiblityManager.put(true, View.VISIBLE);
visiblityManager.put(false, View.GONE);
glInCall.setVisibility((Integer) visiblityManager.get(mCallType.isIncall())); // This Data comes from API or Database. it is a Boolean data.
glOutCall.setVisibility((Integer) visiblityManager.get(mCallType.isOutcall())); // This Data comes from API or Database. it is a Boolean data.
glTeleCall.setVisibility((Integer) visiblityManager.get(mCallType.isTelecall())); // This Data comes from API or Database. it is a Boolean data.
The Problem Solved.
☻♥ Done Keep Code.

How do I test the user name in android [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
i finally figured out where to find most of this.
but now i need to know how to test one part of this program for a username.
My goal is if the user doesn't put anything in the program then don't allow anything to continue.
This is a big app. I have like 3 layout files and 3 java files.
Any help would be good if you need me to send the file i can.
package edu.jones.demogamestartarrayadaptor;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
//import android.view.KeyEvent;
import android.view.View;
//import android.view.View.OnKeyListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
//import android.widget.Toast;
public class GameControlMainActivity extends ListActivity
{
//Class-wide variables for data passed/returned
private String userName = "";
//Use an int for gameLevel,naturally...but, this requires
//use of various methods to convert to String and back!
private int gameLevel = 1;
private EditText nameEntryET;
private TextView gameLevelAnnouncerTV;
private TextView gameLevelTV;
Button doneButton;
//This TV prompts user to enter name in the EditText
//Then, it is made invisible
private TextView namePromptTV;
//These two start out invisible and then show the name
private TextView nameSetTV;
private TextView nameEntTV;
//Array of choices for user
static final String[] CHOICES = new String[]
{
"Read directions",
"Play Game",
"Quit"
};
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Set up View ids
nameEntryET = (EditText)findViewById(R.id.enter_nameET);
gameLevelAnnouncerTV = (TextView) findViewById(R.id.game_level_announcer_TV);
gameLevelTV = (TextView) findViewById(R.id.game_level_TV);
//Set the game level in the TextView
gameLevelTV.setText(Integer.toString(gameLevel));
namePromptTV = (TextView)findViewById(R.id.name_prompt_tv);
nameSetTV = (TextView)findViewById(R.id.name_set_tv);
nameEntTV = (TextView)findViewById(R.id.name_entered_tv);
//Set Done button listener to get user's name
doneButton = (Button) findViewById(R.id.doneBtn);
setDoneButtonListener();
//Set up ArrayAdaptor for the options
setListAdapter(new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, CHOICES));
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
getListView().setTextFilterEnabled(true);
//Set up the listener for user clicks on the list
setListClickListener();
//this toast is for when it opens
Toast.makeText(this, "yo whats up", Toast.LENGTH_SHORT).show();
}//END onCreate
private void setDoneButtonListener()
{
doneButton.setOnClickListener
(
new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//Get user's name when button is clicked
//Call method to set text and hide button
setUserNameAndHideButton();
}
}
);//END setOnClickListener
}//END setDoneButtonListener
//Sets up username in its TextView, and game level Views,
//then hides the other Views & button
private void setUserNameAndHideButton()
{
userName = nameEntryET.getText().toString();
doneButton.setVisibility(View.GONE);
Toast.makeText(this, "Your name has been entered", Toast.LENGTH_SHORT).show();
//After getting the input, hide the EditText
//VISIBLE(0), INVISIBLE(4) or GONE(8)
nameEntryET.setVisibility(View.INVISIBLE);
namePromptTV.setVisibility(View.GONE);
nameEntTV.setText(userName);
nameSetTV.setVisibility(View.VISIBLE);
nameEntTV.setVisibility(View.VISIBLE);
gameLevelAnnouncerTV.setVisibility(View.VISIBLE);
gameLevelTV.setVisibility(View.VISIBLE);
}//END setUserNameAndHideButton
//Set up the listener for the ListView to interpret user clicks
private void setListClickListener()
{
//Set up the click listener for the options
getListView().setOnItemClickListener
(
new OnItemClickListener()
{
//#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
switch(arg2)
{
case 0: launchDirectionsPage();
break;
case 1: startGame();
break;
case 2: finish();
break;
default: break;
}
}
}//END OnItemClickListener
);//END setOnItemClickListener
}//END setListClickListener
//Launch a simple activity to show a scroll view of directions
protected void launchDirectionsPage()
{
//Set up Intent
Intent launchDirections = new Intent(this, DirectionsPageActivity.class);
startActivity(launchDirections);
}//END launchDirectionsPage
//Launch the activity that allows user to input new game value
//Upon return the onActivityResult method is called
protected void startGame()
{
//Set up Intent to launch other activity: PlayGame
Intent launchGame = new Intent(this, PlayGameActivity.class);
//Info added to the Intent's Bundle to pass to PlayGameActivity
launchGame.putExtra("bdl_username", userName);
launchGame.putExtra("bdl_gamelevel", gameLevel);
startActivityForResult(launchGame, 0);
}//END startGame
//This method will be called when the startGame activity terminates
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == 0 && resultCode == RESULT_OK)
{
//Reset the views to possibly updated info returned in the Intent
//First, access the Bundle's values
userName = data.getExtras().getString("bdl_returnUserName");
gameLevel = data.getExtras().getInt("bdl_returnGameLevel");
//Update the user name & game level with values from other activity
nameEntTV.setText(userName);
gameLevelTV.setText(Integer.toString(gameLevel));
}
}//END onActivityResult
#Override
protected void onSaveInstanceState (Bundle outState)
{
super.onSaveInstanceState(outState);
//Add the username and game level to the Bundle
outState.putString("bdl_savedusername", userName);
outState.putInt("bdl_savedgamelevel", gameLevel);
}//END onSaveInstanceState
#Override
public void onRestoreInstanceState (Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
//Restore the username and game level from the Bundle
userName = savedInstanceState.getString("bdl_savedusername");
gameLevel = savedInstanceState.getInt("bdl_savedgamelevel");
}//END onRestoreInstanceState
}//END GameControlMainActivity
If you are asking to stop if the username is not entered, just do this:
private void setDoneButtonListener()
{
doneButton.setOnClickListener
(
new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if (nameEntryET.getText().toString().equals("")) {
Toast.makeText(this, "Enter a username", Toast.LENGTH_LONG).show();
} else {
//Get user's name when button is clicked
//Call method to set text and hide button
setUserNameAndHideButton();
}
}
}
);//END setOnClickListener
}//END setDoneButtonListener
if nameEntryET does not have a value entered, nameEntryET.getText().toString() will return an empty string.

Hiding and showing a single Menu Button

I have a project for an Android class, so I'm still learning and this should be a basic question. We were given a tip calculator and already made some modifications, now we have to add a menu.
When it starts up, it will be in multi-person mode. Gives a text box and Text Field for how many people you want the bill split into. When you hit menu, it should show a Single person mode which eliminates a text box and text field. The menu then changes to show a multi-person mode button in the menu.
I've got everything to work except it's showing both buttons, I cannot figure out how to hide a button temporarily. The main error is:
Cannot invoke setVisibility(int) on the primitive type int
on the statement:
multiple_button.setVisibility(View.GONE);
I've tried every combination of hiding the button I can think of, and think that the above line is correct, but unsure of how make it work.
one_person_button = View.VISIBLE;
multiple_button = View.GONE;
I have this in the code, but it's not doing anything either.
Any help would be greatly appreciated.
edit: code. I've read through the link, but considering I don't have a OnPrepareOptions section, I need to re-read it
package com.android;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Button;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class tipcalc extends Activity
{
public static int one_person_button = Menu.FIRST;
private int multiple_button = Menu.FIRST +1;
static final private int reset_button = Menu.FIRST +2;
private static final int MENU_ITEM = 0;
private EditText txtbillamount;
private EditText txtpeople;
private EditText txtpercentage;
private TextView txtperperson;
private TextView txttipamount;
private TextView txttotal;
private Button btncalculate;
private Button btnreset;
private double billamount = 0;
private double percentage = 0;
private double numofpeople=0;
private double tipamount = 0;
private double totaltopay = 0;
private double perperson = 0;
private View view;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initControls();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuItem itemOne = menu.add(0, one_person_button, Menu.NONE,
R.string.one_person);
MenuItem itemMultiple = menu.add(1, multiple_button, Menu.NONE,
R.string.multiple);
MenuItem itemReset = menu.add(2, reset_button, Menu.NONE,
R.string.reset);
itemOne.setIcon(R.drawable.ic_menu_invite);
itemMultiple.setIcon(R.drawable.ic_menu_allfriends);
itemReset.setIcon(R.drawable.ic_menu_refresh);
one_person_button.setGroupVisible(0, true);
multiple_button.setVisibility(View.GONE);
one_person_button = View.VISIBLE;
multiple_button = View.GONE;
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
if (one_person_button == View.VISIBLE) {
((TextView)findViewById(R.id.txtpeople)).setVisibility(View.INVISIBLE) ;
((TextView)findViewById(R.id.widget30)).setVisibility(View.INVISIBLE) ;
multiple_button = View.VISIBLE;
one_person_button = View.GONE;
numofpeople = 1; }
else if (multiple_button == View.VISIBLE) {
((TextView)findViewById(R.id.txtpeople)).setVisibility(View.VISIBLE) ;
((TextView)findViewById(R.id.widget30)).setVisibility(View.VISIBLE) ;
multiple_button = View.GONE;
one_person_button = View.VISIBLE;
}
return false;
}
private void initControls()
{
txtbillamount = (EditText)findViewById(R.id.txtbillamount);
txtpeople = (EditText)findViewById(R.id.txtpeople);
txtperperson=(TextView)findViewById(R.id.txtperperson);
txttipamount=(TextView)findViewById(R.id.txttipamount);
txttotal=(TextView)findViewById(R.id.txttotal);
btncalculate = (Button)findViewById(R.id.btncalculate);
btnreset = (Button)findViewById(R.id.btnreset);
btncalculate.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){ calculate(); }});
btnreset.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){ reset(); }});
}
private void calculate()
{
billamount=Double.parseDouble(txtbillamount.getText().toString());
numofpeople=Double.parseDouble(txtpeople.getText().toString());
RadioButton poorButton = (RadioButton) findViewById(R.id.radioButton1);
RadioButton goodButton = (RadioButton) findViewById(R.id.radioButton2);
RadioButton excellentButton = (RadioButton) findViewById(R.id.radioButton3);
if (poorButton.isChecked()){
percentage = Double.parseDouble(poorButton.getText().toString());
} else if (goodButton.isChecked()){
percentage = Double.parseDouble(goodButton.getText().toString());
} else if (excellentButton.isChecked()){
percentage = Double.parseDouble(excellentButton.getText().toString());
}
tipamount=(billamount*percentage)/100;
totaltopay=billamount+tipamount;
perperson=totaltopay/numofpeople;
txttipamount.setText(Double.toString(tipamount));
txttotal.setText(Double.toString(totaltopay));
txtperperson.setText(Double.toString(perperson));
}
private void reset()
{
txtbillamount.setText("");
txtpeople.setText("");
txtperperson.setText("");
txttipamount.setText("");
txttotal.setText("");
}
}
Post all of your relavent source code. Without it, we cannot give you specific advice about what is going wrong.
I can tell you though you'll be needing to override onPrepareOptionsMenu() and inside there you'll want to check which mode your in and make the proper button be visible. But you need to call setVisibility(View.VISIBLE); on a reference to the button widget, not on an int.
This page holds the answer to your questions.
try calling setVisibility with 8

Categories

Resources