Select a RadioButton from a RadioGroup, according to a condition

3

I have a radiogroup and according to one condition must be selected one or the other. How do I get it to scale 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. Thank you!

    
asked by anonymous 06.10.2016 / 16:40

1 answer

2

Use the check () method of the RadioGroup :

if (user.hasCar()){
    mSwitchButtonGroup.check(R.id.goes_by_car);
}else {
    mSwitchButtonGroup.check(R.id.doesnt_go_by_car)
}
    
06.10.2016 / 16:44