0

Simple switch statement with the next button as a final case. I want it so the user must select an image in the switch statement before moving on. I have no idea how to write something to stop them user just clicking the next button and moving onto the next activity without making a selection from the switch statement. Code below.

    public void onClick(View v) {

    SharedPreferences.Editor editorWorkout = workoutPref.edit();

    // get the constraint layout from its ID.
    ConstraintLayout mConstraintLayout = getView().findViewById(R.id.FragmentWorkoutMood1);

    switch (v.getId()) {
        case R.id.excitedFace:
            mConstraintLayout.setBackgroundResource(R.mipmap.background_clouds_excited);
            editorWorkout.putInt("excitedkey", EXCITED.getId());
            editorWorkout.commit();
            break;
        case R.id.happyFace:
            mConstraintLayout.setBackgroundResource(R.mipmap.background_clouds_happy);
            editorWorkout.putInt("happykey", HAPPY.getId());
            editorWorkout.commit();
            break;
        case R.id.fineFace:
                 mConstraintLayout.setBackgroundResource(R.mipmap.background_clouds_fine);
            editorWorkout.putInt("finekey", FINE.getId());
            editorWorkout.commit();
            break;
        case R.id.nextBtnMoodPage:
            Intent intent = new Intent(getActivity(),   WorkoutAutomaticThoughtActivity.class);
            startActivity(intent);
    }
Zoe
  • 27,060
  • 21
  • 118
  • 148
  • Whats the issue you are facing in your implementation? – Faisal Naseer May 21 '19 at 20:43
  • I have no idea how to write something to stop them user just clicking the next button and moving onto the next activity without making a selection from the switch statement – androidnewbie May 21 '19 at 20:44
  • try `break;` after `case R.id.nextBtnMoodPage:` and debug and check if it still falls in any of the cases? – Faisal Naseer May 21 '19 at 20:46
  • `case R.id.nextBtnMoodPage: Intent intent = new Intent(getActivity(), WorkoutAutomaticThoughtActivity.class); startActivity(intent); break;` – Faisal Naseer May 21 '19 at 20:48
  • Thanks for trying to help, but maybe you didnt understand my question properly. The user must select an image from the switch statament like R.id.happyface before selecting the nextButton. At present the user can just click the nextbutton and move on without selecting an image. does that make sense? – androidnewbie May 21 '19 at 20:58

2 Answers2

0

You need to disable the action that nextBtnMoodPage takes until a user has selected any of the other options. Do this using a simple boolean. See below:

// Inside the class itself
private boolean hasSelected = false;

public void onClick(View v) {

    SharedPreferences.Editor editorWorkout = workoutPref.edit();

    // get the constraint layout from its ID.
    ConstraintLayout mConstraintLayout = getView().findViewById(R.id.FragmentWorkoutMood1);

    switch (v.getId()) {
        case R.id.excitedFace:
            mConstraintLayout.setBackgroundResource(R.mipmap.background_clouds_excited);
            editorWorkout.putInt("excitedkey", EXCITED.getId());
            editorWorkout.commit();
            hasSelected = true;
            break;
        case R.id.happyFace:
            mConstraintLayout.setBackgroundResource(R.mipmap.background_clouds_happy);
            editorWorkout.putInt("happykey", HAPPY.getId());
            editorWorkout.commit();
            hasSelected = true;
            break;
        case R.id.fineFace:
            mConstraintLayout.setBackgroundResource(R.mipmap.background_clouds_fine);
            editorWorkout.putInt("finekey", FINE.getId());
            editorWorkout.commit();
            hasSelected = true;
            break;
        case R.id.nextBtnMoodPage:
            if(hasSelected){
                Intent intent = new Intent(getActivity(),   WorkoutAutomaticThoughtActivity.class);
                startActivity(intent);
            }
    }
}

In a nutshell, all you need to do is flip a flag once the user has selected an option. If that flag isn't flipped, make the action for the next button do nothing.

It would also be a good idea to grey out the button or otherwise show it as disabled if it does no action, but I won't explain how to do that as it is out of the scope of this question.

Good luck!

michael
  • 463
  • 3
  • 9
  • You're awesome, thank you so much that worked so simply! Any chance you could PM me or post below the bit about the greyed out button? That would really make it look much more professional. – androidnewbie May 21 '19 at 21:25
  • If you're just using normal Android buttons (which it looks like you are) you can reference this: https://stackoverflow.com/questions/4384890/how-to-disable-an-android-button If you want the style of the button to change you'll have to attach extra layout styles. See the following answer: https://stackoverflow.com/questions/8743120/how-to-grey-out-a-button Seeing as what you're asking is already answered many times on SO, IMO it wouldn't be best to post another answer to a similar question here. Good luck with finishing your app! – michael May 21 '19 at 21:55
  • Also if you found that an answer answered your question, make sure to accept it as that'll let other people know that the question has been answered. (also I'll get some rep ;)) – michael May 21 '19 at 21:57
0

You can do start actvity from outside the switch statement Just write

Button buttonNext. = findViewById..

buttonNext.SetOnClickListener...