Selecting a Radiobutton from a Radiogroup, according to one condition

Asked

Viewed 732 times

3

I have a radiogroup and according to one condition should be selected one or the other. How to make it go up to the screen as described below? Code:

private RadioGroup mSwitchButtonGroup;
private boolean mGoesByCar;
private User user;

    .....
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View view = inflater.inflate(R.layout.fragment_confirm_routine, container, false);
                user = new User();
                mSwitchButtonGroup = (RadioGroup)view.findViewById(R.id.switch_rider_button);

                if (user.hasCar()){
                    mSwitchButtonGroup -> R.id.goes_by_car deve aparecer checked
                }else {
                    mSwitchButtonGroup -> R.id.doesnt_go_by_car deve aparecer checked
                }
    ......

                if(mSwitchButtonGroup!=null){
                    mSwitchButtonGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(RadioGroup group, int checkedId) {
                            if(checkedId == R.id.goes_by_car){
                                mGoesByCar = true;
                            }else if(checkedId == R.id.doesnt_go_by_car){
                                mGoesByCar = false;
                            }
                        }
                    });
                }
....

I hope I have been clear. I thank you in advance!

1 answer

2


Use the method check in() class Radiogroup:

if (user.hasCar()){
    mSwitchButtonGroup.check(R.id.goes_by_car);
}else {
    mSwitchButtonGroup.check(R.id.doesnt_go_by_car)
}
  • 1

    Thank you very much! I had already tried this way and it had not worked. But I ended up discovering, after q gave me the same answer, that my problem was in the information I seek from the server... Again, thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.