-2

I am creating a basic quiz app, where the user is shown one question after another.

In order to get to the next question, the user has to click on a button.

Now, I only want this button, which gets the user to the next question, to be visible after he chose one of the possible answers (RadioButtons). It does not matter which of the RadioButtons in the RadioGroup has been selected, it is only important that he has clicked on an answer! I found a way to do this, but I am unsure this is the best way to do this:

> public void startQuiz(View view) {
>     setContentView(R.layout.question_1);
>     RadioGroup rg1 = (RadioGroup) findViewById(R.id.r_group_1);
>     rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
>         @Override
>         public void onCheckedChanged(RadioGroup group, int checkedId) {
>             switch (checkedId) {
>                 case R.id.radio_button_a_q1:
>                     View nb1 = findViewById(R.id.nb1);
>                     nb1.setVisibility(View.VISIBLE);
>                     break;
>                 case R.id.radio_button_b_q1:
>                     nb1 = findViewById(R.id.nb1);
>                     nb1.setVisibility(View.VISIBLE);
>                     break;
>                 case R.id.radio_button_c_q1:
>                     nb1 = findViewById(R.id.nb1);
>                     nb1.setVisibility(View.VISIBLE);
>                     break;
>             }
>         }
>     }
>     ); }

Thank you so so much in advance! :-)

  • 1
    What have you tried? Show us some piece of code that you can't get to work. Normally this is matter of setting an listener on the radiobuttons that unhide the hidden button (alpha or visibility) – Ruben Weerts Apr 12 '17 at 11:55

3 Answers3

0

try this

 radiobutton.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            return false;
        }
    });
Hitesh Gehlot
  • 1,307
  • 1
  • 15
  • 30
0
if(radioButton.isChecked()){
    button.setVisibility(View.VISIBLE);
else{
    button.setVisibility(View.GONE);
}
Mithun Adhikari
  • 521
  • 6
  • 13
  • I want exactly this behavior, but I want to apply it to a whole RadioGroup and am asking if that's possible without applying the code to every radio button of the group. – Fedor von Bock Apr 12 '17 at 12:28
  • if(radioButton1.isChecked() || radioButton2.isChecked() || radioButton3.isChecked()){ button.setVisibility(View.VISIBLE); else{ button.setVisibility(View.GONE); } Or you might want to take a look at this – Mithun Adhikari Apr 12 '17 at 18:50
0

If I understand the question properly, you may want something like this

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Test extends Application{

    @Override
    public void start(Stage primaryStage) throws Exception {
        GridPane root = new GridPane();
        root.setAlignment(Pos.CENTER);
        root.setHgap(20);
        VBox container = new VBox();
        container.setAlignment(Pos.CENTER);

        final ToggleGroup group = new ToggleGroup();
        RadioButton rb1 = new RadioButton("First Choice");
        rb1.setToggleGroup(group);
        RadioButton rb2 = new RadioButton("Second Choice");
        rb2.setToggleGroup(group);
        RadioButton rb3 = new RadioButton("Third Choice");
        rb3.setToggleGroup(group);

        container.getChildren().addAll(rb1,rb2,rb3);

        Button btn = new Button("I should appear");
        btn.setVisible(false);

        root.add(container, 0, 0);
        root.add(btn, 1, 0);

        group.selectedToggleProperty().addListener(new ChangeListener<Toggle>(){
            public void changed(ObservableValue<? extends Toggle> ov,
                 Toggle old_toggle, Toggle new_toggle) {
                    if (group.getSelectedToggle() != null) {
                        btn.setVisible(true);
                    }                
                }
        });

        Scene s = new Scene(root,400,200 );
        primaryStage.setScene(s);
        primaryStage.setResizable(false);
        primaryStage.setTitle("Example");
        primaryStage.show();    


    }

    public static void main(String[] args){
        launch();
    }

}

enter image description here

enter image description here

Edit: This using JavaFX , I do not know about android.

Yahya
  • 13,349
  • 6
  • 30
  • 42