0

This is my code in first activity:

Button GoToNewActivity = (Button)findViewById(R.id.send);


GoToNewActivity.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        String str = null;
        boolean checked = ((RadioButton) v).isChecked();

        // Check which radio button was clicked
        switch(v.getId()) {
            case R.id.radioMale:
                if (checked)
                    str = "Male";
                break;
            case R.id.radioFemale:
                if (checked)
                    str = "Female";
                break;
        }



        EditText editText = (EditText) findViewById(R.id.edit_text);
        String plateNumberEdited = editText.getText().toString();

        Intent intent = new Intent(Index.this, DVLAresult.class);
        Bundle extras = new Bundle();
        extras.putString("dvlaNumber", plateNumberEdited);
        extras.putString("radioChosen", str);
        intent.putExtras(extras);

        startActivity(intent);
    }

});

This is my first activity XML :

android:id="@+id/radioSex"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<RadioButton
    android:id="@+id/radioMale"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="male"/>

<RadioButton
    android:id="@+id/radioFemale"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="female"/>

And this is how I try to get value from first activity and use us in second:

Bundle extras = getIntent().getExtras();
String message= extras.getString("radioChosen");

Question is, I would like to use values from first activity like values from text and from selected radio choice, and by using intent use this values in second activity.

  • How are you trying to pass those values? – codeMagic Apr 06 '16 at 16:23
  • Updated was not copied full code –  Apr 06 '16 at 16:27
  • You still haven't said *exactly what problem you are having*. The link above should show you how to do it. – codeMagic Apr 06 '16 at 16:28
  • @codeMagic Currently I with out any problems use the text value, BUT problem is with Radio choice, can't get value from RADIO. –  Apr 06 '16 at 16:31
  • Put them in a `RadioGroup` and use `onCheckChanged` listener http://stackoverflow.com/a/21213176/1380752 – codeMagic Apr 06 '16 at 16:33
  • @codeMagic can you show me how should I do this ? The main key is that I need to use plateNumberEdited and selected choice from radio in second activity in different places. –  Apr 06 '16 at 16:36
  • @codeMagic could you help with this question ? –  Apr 06 '16 at 19:12
  • No because I don't know where you are stuck any more. What do you mean you want to use them in "different places"? Is that what the problem is now? You should edit your question and make it much more clear exactly what you are stuck on. – codeMagic Apr 06 '16 at 19:21
  • @codeMagic Could you explain please how to send values from first activity (from Radio) and use in in second activity? –  Apr 06 '16 at 19:35
  • FIXED do not need any answer –  Apr 06 '16 at 20:01

1 Answers1

0

To use.

   GoToNewActivity.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    RadioGroup radiogroup =  (RadioGroup) findViewById(R.id.radioSex);
                    Button bt = (Button) findViewById(R.id.send);


                    // get selected radio button from radioGroup
                    int selectedId = radiogroup .getCheckedRadioButtonId();
                    // find the radio button by returned id
                    RadioButton radioButton = (RadioButton) findViewById(selectedId);
                    //Toast.makeText(Index.this,radioButton.getText(), Toast.LENGTH_SHORT).show();
                    String choice = radioButton.getText().toString();



                    EditText editText = (EditText) findViewById(R.id.edit_text);
                    String plateNumberEdited = editText.getText().toString();

                    Intent intent = new Intent(Index.this, DVLAresult.class);
                    Bundle extras = new Bundle();
                    extras.putString("dvlaNumber", plateNumberEdited);
                    extras.putString("radioChosen", choice);
                    intent.putExtras(extras);

                    startActivity(intent);
                }

            });

To get value in second activity.

Intent intent = getIntent();
String radioChoice2 = intent.getStringExtra("radioChosen");

Intent intent = getIntent();
String dvlaNumFin = intent.getStringExtra("dvlaNumber");