I want to know/learn how to enable a button when/if a radio button is checked
Steps done
I created a fragment and inside a radio group with 3 radio buttons inside
Goal
My main goal is to enable the button when a radio button is checked, and disable it when the radio button is unchecked
Code
So far I have this code
public class Operations extends Fragment
{
RadioButton surfArea, rad, diam;
RadioGroup radG;
Button openSelect;
public Operations()
{
// Required empty public constructor
}
public static Operations newInstance()
{
return new Operations();
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_operations_sphere, container, false);
surfArea = (RadioButton) rootView.findViewById(R.id.RB_surfArea);
rad = (RadioButton) rootView.findViewById(R.id.RB_Rad);
diam = (RadioButton) rootView.findViewById(R.id.RB_Diam);
openSelect = (Button) rootView.findViewById(R.id.btn_open_select);
radG = (RadioGroup) rootView.findViewById(R.id.RG_group);
openSelect.setEnabled(false);
openSelect.setOnClickListener(new View.OnClickListener()
{ //This piece of code is for testing purposes
@Override
public void onClick(View v)
{
if (surfArea.isChecked())
{
openSelect.setEnabled(true); //I detected my mistake here, so I would like to know a better way to achieve this
Intent sa = new Intent(getContext(), OperSphere.class);
startActivity(sa);
}
}
});
return rootView;
}
@Override
public void onResume()
{ //Here the radio button unchecks
radG.clearCheck();
super.onResume();
}
}
Question
How to enable a button when a radio button is checked?
Some examples would be great
Thanks in advance
Sorry my bad, here try this solution;
Since you are using RadioGroup, set the listener on this to disable or enable button accordingly.
RadioGroup radG = (RadioGroup) findViewById(R.id.yourRadioGroup);
radG.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
switch(checkedId)
{
case R.id.surfArea:
//enable or disable button
break;
case R.id.rad:
//enable or disable button
break;
case R.id.diam:
//enable or disable button
break;
}
}
});