-2

Basically, I have a navigation bar activity consisting of fragments, and I am making a unit converter. I've got the concept of how to do that cleared, just have one simple issue. Since I'm using radiobuttons, every time I change the value to be converted for the same unit conversion, I need to click on a different radiobutton and then click back on the same radiobutton

final RadioGroup radioGroup = (RadioGroup) myView.findViewById(R.id.radio);
    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, final int checkedId) {
            // checkedId is the RadioButton selected
            switch (checkedId) {
                case R.id.radioGram:
                    final TextView textView = (TextView) myView.findViewById(R.id.EditTextFrom);
                    textView.setText("Gram");

                    final RadioGroup radioGroup = (RadioGroup) myView.findViewById(R.id.radio2);
                    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(RadioGroup radioGroup, int checkedId2) {
                            switch (checkedId2) {
                                case R.id.radioGram2:
                                    TextView textView = (TextView) myView.findViewById(R.id.EditTextTo);
                                    textView.setText("Gram");

                                    EditText calc = (EditText) myView.findViewById(R.id.From);
                                    String gramholder = calc.getText().toString();
                                    gram = Integer.parseInt(gramholder) * 1;

                                    EditText editText = (EditText) myView.findViewById(R.id.To);
                                    editText.setText(String.valueOf(gram));
                                    break;

                                case R.id.radioKilogram2:
                                    TextView textView2 = (TextView) myView.findViewById(R.id.EditTextTo);
                                    textView2.setText("Kilogram");

                                    EditText calc2 = (EditText) myView.findViewById(R.id.From);
                                    String gramholder2 = calc2.getText().toString();
                                    gram2 = Integer.parseInt(gramholder2) * 0.001;

                                    EditText editText2 = (EditText) myView.findViewById(R.id.To);
                                    editText2.setText(String.valueOf(gram2));
                                    break;


                            }

                        }


                    });

So for example, if I want to convert 500 grams to kilograms, I'd just click on the radiobuttons gram (in the first radiogroup) and kilogram (in the second radiogroup), but then if I want to convert 520 grams to kilograms, I would need to click on gram and then back to kilogram in the second radiogroup

Taha
  • 65
  • 1
  • 10

1 Answers1

0

This is happening because you are changing the value of editText by listening only to only CheckedChangeListener for Radio Buttons.

You just need to add a TextChangeListener to your editText's and use a TextWatcher to listen for changes.

Then, you won't be needing to click on radio button again.

Edit : Wrote this code. Did not test it, but probably enough to understand the solution.

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

/**
 * Created by Kartik Sharma on 01/01/17.
 */
public class Stack extends Activity implements RadioGroup.OnCheckedChangeListener{

    EditText editText_From, editText_To; 
    RadioGroup radioGroup_From, radioGroup_To; 
    View myView;
    private final String GRAM = "GRAM", KILOGRAM = "KILOGRAM";
    String currentSelectedFrom = GRAM, currentSelectedTo = GRAM;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myView = findViewById(R.id.fragment);

        editText_From = (EditText) myView.findViewById(R.id.From);
        editText_To = (EditText) myView.findViewById(R.id.To);


        radioGroup_From = (RadioGroup) findViewById(R.id.radio);
        radioGroup_To = (RadioGroup) myView.findViewById(R.id.radio2);

        editText_From.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(currentSelectedTo == KILOGRAM){
                    double gram2 = Integer.parseInt(s.toString()) * 0.001;

                    editText_To.setText(String.valueOf(gram2));
                }else if(currentSelectedTo == GRAM){
                    double gram = Integer.parseInt(s.toString()) * 1;

                    editText_To.setText(String.valueOf(gram));
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        //SIMILARLY FOR EDITTEXT_TO IF REVERSE CALCULATION IS NEEDED
        radioGroup_From.setOnCheckedChangeListener(this);
        radioGroup_To.setOnCheckedChangeListener(this);


    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if(group == radioGroup_From){
            switch(checkedId){
                case R.id.radioGram :
                    //PERSON SELECTED GRAM IN FROM SECTION

                    final TextView textView = (TextView) myView.findViewById(R.id.EditTextFrom);
                    textView.setText("Gram");

                    currentSelectedFrom = GRAM;
                    break;
            }
        }else if(group == radioGroup_To){
            switch (checkedId) {
                case R.id.radioGram2:
                    //PERSON SELECTED GRAM IN TO SECTION
                    TextView textView = (TextView) myView.findViewById(R.id.EditTextTo);
                    textView.setText("Gram");

                    String gramholder = editText_From.getText().toString();
                    double gram = Integer.parseInt(gramholder) * 1;

                    editText_To.setText(String.valueOf(gram));

                    currentSelectedTo = GRAM;
                    break;

                case R.id.radioKilogram2:
                    //PERSON SELECTED KILOGRAM IN TO SECTION

                    TextView textView2 = (TextView) myView.findViewById(R.id.EditTextTo);
                    textView2.setText("Kilogram");

                    String gramholder2 = editText_From.getText().toString();
                    double gram2 = Integer.parseInt(gramholder2) * 0.001;

                    editText_To.setText(String.valueOf(gram2));

                    currentSelectedTo = KILOGRAM;
                    break;


            }

        }
    }

}
Community
  • 1
  • 1
Kartik Sharma
  • 723
  • 8
  • 19
  • I did use TextChangeListener and TextWatcher, and added the part where it checks for radiobuttons into the onTextChanged, but I have the same problem, anything else I could try @Kartik Sharma – Taha Jan 01 '17 at 13:22
  • @Taha I have added some code, which could help you out in understanding the pattern of the solution. – Kartik Sharma Jan 01 '17 at 13:50
  • Thank you, I'll see if I can get something working with the sample you've provided @Kartik Sharma – Taha Jan 01 '17 at 15:02