I have created a layout xml file where I have two RadioButtons.
By default, RadioButton 1 is selected and I am displaying a DatePicker component on the screen but when the user selects RadioButton2 the DatePicker should disappear from the screen.
How can I handle the scenario? Should I make the change in layout / Java code?
It is actually very simple.
Get a reference of your RadioGroup and your DatePicker. Implement an OnCheckedChangeListener for the RadioGroup and check which RadioButton was checked in there.
If RadioButton A was checked set the visibility on your DatePicker to visible and if RadioButton B was checked set the visibility to gone or invisible depending on your requirement.
As an example.
public class MyActivity extends Activity {
private RadioGroup choice;
private DatePicker datePicker;
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.your_layout);
choice = (RadioGroup) findViewById(R.id.choice);
datePicker = (DatePicker) findViewById(R.id.date_picker);
choice.setOnCheckedChangeListener(
new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId) {
case R.id.radio_button_a:
datePicker.setVisibility(View.VISIBLE);
break;
case R.id.radio_button_b:
datePicker.setVisibility(View.GONE);
break;
}
}
});
}
}
In theory it should be something like that.
Related
I'm developing a quiz-like app in android. Each question is a recycler view item so that I can have multiple question types presented sequentially. My question is: is there any way of implementing a CheckBox group just like the RadioButton group? With the RadioButton group, I'm able to add answers (RadioButton's) by simply calling:
RadioGroup radioGroup = view.findViewById(R.id.answer_grp);
RadioButton rb = new RadioButton("context...");
rb.setText("text");
radioGroup.addView(rb);
Is there any implementation of a Checkbox group similar to this? Should I use the implementation of the RadioButton group provided and customize the layout of the RadioButton to look like a checkbox? What's the best possible practice in this case?
Thanks very much in advance!
You can try this library https://github.com/xeoh/CheckBoxGroup
Hope this helps!
You can use a ViewGroup like LinearLayout to group CheckBox together, and implement CompoundButton.OnCheckedChangeListener for your class to handle checked change event:
public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
CheckBox q1A1CheckBox;
CheckBox q1A2CheckBox;
CheckBox q1A3CheckBox;
CheckBox q1A4CheckBox;
CheckBox q2A1CheckBox;
CheckBox q2A2CheckBox;
CheckBox q2A3CheckBox;
CheckBox q2A4CheckBox;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
q1A1CheckBox = findViewById(R.id.main_q1_a1_checkBox);
q1A2CheckBox = findViewById(R.id.main_q1_a2_checkBox);
q1A3CheckBox = findViewById(R.id.main_q1_a3_checkBox);
q1A4CheckBox = findViewById(R.id.main_q1_a4_checkBox);
q2A1CheckBox = findViewById(R.id.main_q2_a1_checkBox);
q2A2CheckBox = findViewById(R.id.main_q2_a2_checkBox);
q2A3CheckBox = findViewById(R.id.main_q2_a3_checkBox);
q2A4CheckBox = findViewById(R.id.main_q2_a4_checkBox);
q1A1CheckBox.setOnCheckedChangeListener(this);
q1A2CheckBox.setOnCheckedChangeListener(this);
q1A3CheckBox.setOnCheckedChangeListener(this);
q1A4CheckBox.setOnCheckedChangeListener(this);
q2A1CheckBox.setOnCheckedChangeListener(this);
q2A2CheckBox.setOnCheckedChangeListener(this);
q2A3CheckBox.setOnCheckedChangeListener(this);
q2A4CheckBox.setOnCheckedChangeListener(this);
}
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
int currentCheckBoxGroupId = ((LinearLayout) compoundButton.getParent()).getId();
switch (currentCheckBoxGroupId) {
case R.id.main_q1_linearLayout:
Toast.makeText(this, "Question one", Toast.LENGTH_SHORT).show();
//do whatever you want
break;
case R.id.main_q2_linearLayout:
Toast.makeText(this, "Question two", Toast.LENGTH_SHORT).show();
//do whatever you want
break;
}
}
}
Is it possible to identify for which Radio Group a specific Radio Button belongs to in Android?
Eg:
RadioGroup_A
- RadioButton_1A
- RadioButton_2A
RadioGroup_B
- RadioButton_1B
- RadioButton_2B
When I have RadioButton_1A, to idetify that belongs to RadioGroup_A ?
I need to implement a logic to ignore triggering CheckedChange listener for a speacial case. Need to restrict that only for a one RadioGroup When I know that RadioButton belongs to that specific RadioGroup.
Thanks
What about radioGroup.indexOfChild method? The doc says that returns:
a positive integer representing the position of the view in the group,
or -1 if the view does not exist in the group
Maybe you can check if your RadioButton exits in the RadioGroup:
int idx = radioGroup.indexOfChild(radioButton);
if (idx != -1)
{
//radioButton is in the radioGroup
}
else {
//radioButton isnt in the radioGroup
}
Hope it helps!
In one of my apps, I did this by having two RadioButon ArrayLists, one for each group. In onCreate() after the layout is inflated, the RadioButtons are added to each group as described in this answer.
Then, by checking for which ArrayList contains the RadioButton, the required logic can be implemented.
i described full example of your question please apply my solution
public class MyClass extends Activity implements RadioGroup.OnCheckedChangeListener
{
RadioGroup rg1,rg2,rg3;
public void onCreate(Bundle b)
{
super.onCreate(b);
setContentView(R.layout.main);
rg1=(RadioGroup)findViewById(R.id.rg1);
rg2=(RadioGroup)findViewById(R.id.rg2);
rg3=(RadioGroup)findViewById(R.id.rg3);
rg1.setOnCheckedChangeListener(this);
rg2.setOnCheckedChangeListener(this);
rg3.setOnCheckedChangeListener(this);
}
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if(radioGroup==rg1)
{
//First RadioGroup Clicked
RadioButton radio=(RadioButton)findViewById(i);
Toast.makeText(this,radio.getText().toString(),Toast.LENGTH_SHORT).show();
}
if(radioGroup==rg2)
{
//Second RadioGroup Clicked
RadioButton radio=(RadioButton)findViewById(i);
Toast.makeText(this,radio.getText().toString(),Toast.LENGTH_SHORT).show();
}
if(radioGroup==rg3)
{
//Third RadioGroup Clicked
RadioButton radio=(RadioButton)findViewById(i);
Toast.makeText(this,radio.getText().toString(),Toast.LENGTH_SHORT).show();
}
}
}
I have a simple program that has 3 radio buttons in a group. When I click on any of the radio buttons I have a Toast appear to let the user know which button they have selected.
My problem I am facing is I have a onClick event for a button that clearCheck the radio group. When that happens, the Toast message appears again with the previous selected information from the radio button.
How do I prevent the toast from happening when I clearCheck the radio group?
public class MainActivity extends AppCompatActivity {
RadioGroup radioGroup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize Radio Group and attach click handler
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioGroup.clearCheck();
// Attach CheckedChangeListener to Radio Group
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb = (RadioButton) group.findViewById(checkedId);
if(null!=rb && checkedId > -1){
Toast.makeText(MainActivity.this, rb.getText(), Toast.LENGTH_SHORT).show();
}
}
});
}
public void onClear(View v){
// Clears the radio buttons when the clear button is pressed
radioGroup.clearCheck();
}
That is because clearing the check is changing the checked status of the radio group. You need to set a flag to let the listening know it should ignore that change.
Just a simple boolean declared in the class, flagged in onClear and checked in onCheckChanged (and reset the flag in onCheckChanged also).
I have a ListView of questions. In each row there is a question and a RadioGroup with possibile answers. Not all the questions have the same number of answers, so I add the RadioButtons dynamically in my custom ListAdapter. My question is how to add Listener to these dynamically created RadioButtons. My code:
RadioGroup rg = (RadioGroup) convertView.findViewById(R.id.question);
rg.removeAllViews();
for (String qA : question.answers) {
RadioButton rb = new RadioButton(getContext());
rb.setText(qA.toString());
rg.addView(rb);
}
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId) {
//WHAT SHOULD I PUT HERE?
}
}
});
Maby something like this:
#Override public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton btn = (RadioButton)findViewById(checkedId);
}
and then something with the btn? :)
To find the checked button
View checkedButton = rg.findViewById(checkedId);
To find the index of checked button
int checkedIndex = rg.indexOfChild(checkedButton);
Before doing the above you have to set id to the buttons like mentioned in the comments rb.setId(count). Count has to be unique for each button within a group.
1 first create global variable of CompoundButton.OnCheckedChangeListener in your adapter for example private CompoundButton.OnCheckedChangeListener listener
2 second you have to attach listener variable with your radio button at the time of creating the radio button.
3 third when you bind the data or value with your ui you have to call this method
listener = new CompoundButton.OnCheckedChangeListener()
{
#Override
public void onCheckedChanged ( CompoundButton buttonView, boolean isChecked )
{
}
};
I have an activity ColorChoiceActivity that displays radio group for the user to select a color preference from. I would like to be able to change the xml layout displayed in my PlayGameActivity based on which of these buttons is selected.
Currently I have the PlayGameActivity just display the default layout:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.battlefield_default);
}
My ColorChoiceActivity has the following so far:
public class ColorChoiceActivity extends Activity {
private RadioGroup radioColorGroup;
private RadioButton radioColorButton;
private Button saveColorBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.color_choice_screen);
addListenerOnButton();
}
public void addListenerOnButton()
{
radioColorGroup = (RadioGroup) findViewById(R.id.radioColor);
saveColorBtn = (Button) findViewById(R.id.saveColorBtn);
saveColorBtn.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v){
//get selected radio button from radioGroup
int selectedId = radioColorGroup.getCheckedRadioButtonId();
//find the radiobutton by returned id
radioColorButton = (RadioButton) findViewById(selectedId);
//change battlefield layout based on selected color
}
});
}
}
I am not sure how to go about changing the layout displayed in the PlayGameActivity based on what is chosen in ColorChoice. Currently I have several separate battlefield layout files but I don't know how to switch the content view to them based on user choice. Is there a way to do this or am I approaching the problem in the wrong way?
In onClick() listner, just remember selected battlefield and then, conditionally set layout using
setContentView("your selected layout")
in on create method.
Better option is to use the menu instead of the radiogroup
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()) {
case R.id.color1:
break;
}
}