-1

Brilliant stuff Umesh, thanks! You've taught me a lot there! I've one more question if you don't mind.

I would now like to choose one radio button from each radiogroup (for Example, Chinese, Level 1, Guess the City) and then have the message at the top read "You chose Chinese, Level 1, Guess the City". How can I write this in the Java script. so far I have:


import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity {


    private RadioGroup radioGroup1;
    private RadioGroup radioGroup2;
    private RadioGroup radioGroup3;
    private RadioButton chinese, indian, russian;
    private Button button;
    private TextView textView;


    public String string_first_radiogroup;
    public String string_second_radiogroup;
    public String string_third_radiogroup;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        radioGroup1 = (RadioGroup) findViewById(R.id.radio1);// your radio group1
        radioGroup2 = (RadioGroup) findViewById(R.id.radio2);// your radio group2
        radioGroup3 = (RadioGroup) findViewById(R.id.radio3);// your radio group3

        radioGroup1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override

            public void onCheckedChanged(RadioGroup group, int checkedId) {

                // find which radio button is selected

                if(checkedId == R.id.chinese) {

                    string_first_radiogroup="Chinese"; // String updated on radio check

                } else if(checkedId == R.id.indian) {

                    string_first_radiogroup="Indian"; // String updated on radio check

                } else {

                    string_first_radiogroup="Russian"; // String updated on radio check

                }
            }

        });

        chinese = (RadioButton) findViewById(R.id.chinese);
        indian = (RadioButton) findViewById(R.id.indian);
        russian = (RadioButton) findViewById(R.id.russian);
        textView = (TextView) findViewById(R.id.text);

        radioGroup1.check(R.id.chinese);// setting Chinese selected by default
        string_first_radiogroup="Chinese"; //setting "Chinese" by default in string also
        radioGroup2.check(R.id.difficulty1);
        string_second_radiogroup="Level 1 (Starter)";

        button = (Button)findViewById(R.id.choose);
        button.setOnClickListener(new OnClickListener() {

            @Override

            public void onClick(View v) {

                textView.setText("You chose " + string_first_radiogroup + " option");
                textView.setText("You chose " + string_second_radiogroup + " option");

            }

        });

    }

}
Dan
  • 1
  • 2
  • "I've edited the original question with a new question" -> Please don't do this, but instead, create a new question. If not, people coming back to this will see your new question and the answer to the old one which seem unrelated. Thanks in advance. – 2Dee Mar 12 '15 at 14:05
  • Apologies, thanks for letting me know :) – Dan Mar 12 '15 at 17:28

1 Answers1

0

Do the same in onCreate() method for radioGroup2 and radioGroup3 . Reduce your code by Declaring a public string And update it in radioGroup.onCheckedChangeListener Where you are using toast

On button click just display that string . No need to check in button clickListener.

For 3 radio groups use 3 strings.

public String string_first_radiogroup; 


@Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        radioGroup1 = (RadioGroup) findViewById(R.id.radio1);// your radio group1
        radioGroup2 = (RadioGroup) findViewById(R.id.radio2);// your radio group2

radioGroup3 = (RadioGroup) findViewById(R.id.radio3);// your radio group3

        radioGroup1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override

            public void onCheckedChanged(RadioGroup group, int checkedId) {

                // find which radio button is selected

                if(checkedId == R.id.chinese) {

                    Toast.makeText(getApplicationContext(), "Choice: Chinese",
                    Toast.LENGTH_SHORT).show();

             string_first_radiogroup="Chinese"; // String updated on radio check

            } else if(checkedId == R.id.indian) {

                    Toast.makeText(getApplicationContext(), "Choice: Indian",
                             Toast.LENGTH_SHORT).show();
             string_first_radiogroup="Indian"; // String updated on radio check

            } else {

                    Toast.makeText(getApplicationContext(), "Choice: Russian",
                             Toast.LENGTH_SHORT).show();
             string_first_radiogroup="Russian"; // String updated on radio check

                }
    }

 });

        chinese = (RadioButton) findViewById(R.id.chinese);
        indian = (RadioButton) findViewById(R.id.indian);
        russian = (RadioButton) findViewById(R.id.russian);
        textView = (TextView) findViewById(R.id.text);

       radioGroup.check(R.id.chinese);// setting Chinese slected by default
     string_first_radiogroup="Chinese"; //setting "Chinese" by default in string also 

        button = (Button)findViewById(R.id.choose);
        button.setOnClickListener(new OnClickListener() {

            @Override

            public void onClick(View v) {

           textView.setText("You chose " + string_first_radiogroup + " option");

            }

        });

    }
Umesh Chhabra
  • 268
  • 1
  • 8
  • Thanks for the answer! Sadly I don't quite understand what you mean by "declare a public string and update it..." – Dan Mar 11 '15 at 11:14
  • Thanks so much Umesh. I've edited the original question with a new question I wonder if you could take a look :-) Great help so far! – Dan Mar 11 '15 at 13:54
  • Sorry buddy! I do not know very much about JavaScript (syntax) – Umesh Chhabra Mar 12 '15 at 03:49