Hello Everybody
How do I get validate my form specially for "gender", where I have used RadioButton, so that when "male" is selected "female" will automatically get unselected. Again for that will I have to take RadioButton or it'll be better to take RadioGroup?
Thanks.
Put your gender RadioButtons inside a RadioGroup
Use RadioGroup.
You can see the full documentation RadioGroup
You can refer Example from developer.android.com -> http://developer.android.com/resources/tutorials/views/hello-formstuff.html#RadioButtons
For Example in your xml file :
<RadioGroup android:layout_height="wrap_content" android:id="#+id/gender_group"
android:layout_width="match_parent">
<RadioButton android:text="Male"
android:layout_width="wrap_content" android:id="#+id/radio0"
android:layout_height="wrap_content" android:checked="true"></RadioButton>
<RadioButton android:text="Female"
android:layout_width="wrap_content" android:id="#+id/radio1"
android:layout_height="wrap_content"></RadioButton>
</RadioGroup>
try this
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radio_pirates:
if (checked)
// Pirates are the best
break;
case R.id.radio_ninjas:
if (checked)
// Ninjas rule
break;
}
}
Related
I am working on a mobile app in which I have used the radio buttons. The strange thing is that the radio button's setOnClickListener does not run complete code. Here is the code of my radio Button
radioButtonWeekly.setOnClickListener {
btnSelectWeek.isEnabled = true
btnSelectWeek.setBackgroundColor(Color.WHITE)
btnSelectMonth.isEnabled = false
btnSelectMonth.setBackgroundColor(Color.GRAY)
Log.i("${PerformanceFragment::class.java.simpleName}: ", "Selected Week No: ${selectedWeekNo}")
}
Only the above four statements run. The last statement (Log.i) does not run. In fact, any statement below the above four statements does not run. Please explain what is going on wrong?
first create radio group
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="#+id/radio_pirates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/pirates"
android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="#+id/radio_ninjas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ninjas"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
then for handle radio buttons
fun onRadioButtonClicked(view: View) {
if (view is RadioButton) {
// Is the button now checked?
val checked = view.isChecked
// Check which radio button was clicked
when (view.getId()) {
R.id.radio_pirates ->
if (checked) {
// Pirates are the best
}
R.id.radio_ninjas ->
if (checked) {
// Ninjas rule
}
}
}
}
source https://developer.android.com/guide/topics/ui/controls/radiobutton
Trying to assign each Radio Buttons in a Radio Group, with a unique Integer value or id such as 10,20,30...so-on, and return the particular value/id for further actions such as printing that id or sending it to the backend.
For eg. : for RadioButton radio_visa it should return a value of 20, for radio_mastercard, it should return 30, and so-on.
No idea how to approach.
Thanks for help in advance.
My XML file:
<RadioGroup
android:id="#+id/radioGroupCard"
android:layout_width="151dp"
android:layout_height="130dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.165"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.384">
<RadioButton
android:id="#+id/radio_visa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClickedCards"
android:text="VISA" />
<RadioButton
android:id="#+id/radio_mastercard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClickedCards"
android:text="Master Card" />
<RadioButton
android:id="#+id/radio_rupay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClickedCards"
android:text="Rupay" />
<RadioButton
android:id="#+id/radio_amex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClickedCards"
android:text="AMEX" />
</RadioGroup>
My Activity file:
public void onRadioButtonClickedCards(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.radio_visa:
if (checked)
// actions
{
IVCardType.setImageResource(R.drawable.visa);
}
break;
case R.id.radio_mastercard:
if (checked)
// actions
{
IVCardType.setImageResource(R.drawable.mastercard);
}
break;
case R.id.radio_rupay:
if (checked)
// actions
{
IVCardType.setImageResource(R.drawable.amex);
}
break;
case R.id.radio_amex:
if (checked)
// actions
{
IVCardType.setImageResource(R.drawable.rupay);
}
break;
}
}
You can use the tag property of a RadioButton for that.
android:tag="30"
Actually, any widget have this property. Then just get it by
((RadioButton) view).getTag()
How can I retrieve radio group of the radio button that is checked? I have two radio groups with some radio buttons and I would like to know which group they belong to.
void populateSpinner(#ArrayRes int radioId, RadioGroup radioGroup) {
adapter = ArrayAdapter.createFromResource(SecondActivity.this,
radioId, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spn_districts.setAdapter(adapter);
}
public void onRadioButtonClick(View view)
{
boolean checked = ((RadioButton) view).isChecked();
switch(view.getId())
{
case R.id.radio_1:
if (checked)
populateSpinner(R.array.radio_1_array, <RADIO BUTTON GROUP>);
break;
// so on
How can I get the Radio Button Group?
You can set tag for each of your RadioGroup in your xml code , and when you click any RadioButton, you can get the Parent RadioGroup's Tag and easily you can identify the parent RadioGroup
Your activity_layout XML code
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="FirstRadioGroup"
android:id="#+id/firstRG">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one"
android:id="#+id/firstRB"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two"
android:id="#+id/secondRB"/>
</RadioGroup>
JAVA Activity Code
RadioGroup firstRG = (RadioGroup) findViewById(R.id.firstRG);
firstRG.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch(i){
case R.id.firstRB:
Toast.makeText(MainActivity.this, "Parent RadioGroup is --"+radioGroup.getTag(), Toast.LENGTH_SHORT).show();
break;
case R.id.secondRB:
break;
}
}
});
Like this when you will check the first RadioButton it will show you the Prent RadioGroup Tag Value
I am creating a set of radio options where the radio buttons are removed, and I would like to know which option is selected.
Here is the code
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="200dp"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:id="#+id/languages"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<RadioButton
android:id="#+id/english"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="20dp"
android:text="#string/langtext_english"
android:textSize="16dp"
android:checked="true"
android:button="#null" />
<RadioButton
android:id="#+id/bahasaIndonesia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/langtext_indonesian"
android:textSize="16dp"
android:button="hide"/>
</RadioGroup>
I don't want to show the radio button, but I would like to know which option is selected. onCheckedChangedListener doesn't work as there is no button. How to do this?
When the option is touched or clicked, I would like to show a Toast.
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
langChosen = (RadioGroup) rootView.findViewById(R.id.languages);
langChosen.setOnClickListener(new RadioGroup.OnClickListener() {
#Override
public void onClick(View v) {
Toast abc = new Toast(getContext());
abc.makeText(getContext(), v.toString(),Toast.LENGTH_LONG);
}
You mentioned
I don't want to show the radio button, but I would like to know which option is selected. onCheckedChangedListener doesn't work as there is no button.
I would not say that is true. The RadioGroup still registers changes of the checked radio button. The android:button attribute of RadioButtonjust represents the button you see next to the text/content of the radio button. You still can register to click events of the radio button element itself.
Could it be that you just forgot to call show() on your toast, so you think it's not working?
I supply a tested solution for you here. Let me know if it works for you too.
RadioGroup group = (RadioGroup)this.findViewById(R.id.languages);
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
RadioButton b = (RadioButton)findViewById(radioGroup.getCheckedRadioButtonId());
Toast.makeText(MainActivity.this, b.getText(), Toast.LENGTH_SHORT).show();
}
});
Note that MainActivity.this must be replaced with your corresponding context.
I would try the OnClick method with the two buttons, then the method is called, use the ischecked() method to detremine which of the buttons is checked.
I'm creating a RadioGroup with RadioButtons dynamically and need to have one radio button checked by default.
I've done this by using both radioButton.setChecked(true) and radioButton.toggle();
The problem I have is that when I at runtime select another radio button the first one stays checked so I end up with two checked radio buttons in the radio group.
Has anyone had this problem and know how to solve it?
private RadioButton addRadioButton(String type, String price){
RadioButton radio = new RadioButton(Order.this);
radio.setText(type + " (" + Utils.formatCurrency(price) + ")");
radio.setTextAppearance(Order.this, R.style.portalCellTextStyle);
radio.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
radio.setTag(price);
if(type.toLowerCase().equals("m"))
radio.toggle();
return radio;
}
I know I'm late but for those who search for the answer like me this can be useful.
When you add RadioButton to a RadioGroup, you must set the button checked after adding it to the parent like:
RadioGroup radioGroup = new RadioGroup(getContext())
RadioButton radioButton = new RadioButton(getContext());
radioButton.setText("text");
mRadioGroup.addView(radioButton);
radioButton.setChecked(true);
If the view is checked before adding it to the parent, it will be impossible to uncheck it.
You can do this simply:
JAVA:
radiogroup =(RadioGroup)findViewById(R.id.radiogroup);
radiobutton1 =(RadioButton)findViewById(R.id.radiobutton1);
radiobutton2 =(RadioButton)findViewById(R.id.radiobutton2);
if(your first assessment){
radiobutton1.setChecked(true);
}else{
radiobutton2.setChecked(true);
}
XML:
<RadioGroup
android:id="#+id/radiogroup"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/radiobutton1"
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:id="#+id/radiobutton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
Simple, at first time load the screen check the position of In Getview method and then apply condition:
if (position == 0) {
holder.act_delivery_rbtn.setChecked(true);
}
If you are only using one radio box for checking on and off, maybe you should use checkbox or toggle button instead.
http://developer.android.com/resources/tutorials/views/hello-formstuff.html
Scroll down and see checkbox and toggle button.
When using radios you usually have more than one and choose between them. Like easy, medium, hard.
Replace
radio.toggle();
with
radio.setChecked(true);
Just make checked = "true" in your code
you should check it in radio group...
radiogroup.check(IdOfYourButton)
use the clearCheck () function to clear the already checked buttons.I hope this solves your problem.
Turned out to be because I called the checked() method on the radio group straight after setting the OnCheckedChangeListener() causing it to be called immediately and throw a NPE for some reason.
Moved the checked() method call to just before the OnCheckedChangeListener() and it works now.
radios.check(2);
radios.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
}
});
private RadioButton addRadioButton(String type, String price){
RadioButton radio = new RadioButton(Order.this);
radio.setText(type + " (" + Utils.formatCurrency(price) + ")");
radio.setTextAppearance(Order.this, R.style.portalCellTextStyle);
radio.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
radio.setTag(price);
if(type.toLowerCase().equals("m"))
radio.setId(2);
return radio;
}
in Xml add android:checkedButton="#+id/storeRB" to RadioGroup
<RadioGroup
android:id="#+id/storeTypeRG"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkedButton="#+id/storeRB"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="#+id/text2"
app:layout_constraintStart_toEndOf="#+id/text2"
app:layout_constraintTop_toTopOf="#+id/text2">
<RadioButton
android:id="#+id/storeRB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:button="#drawable/radio_button_bg"
android:paddingStart="8dp"
android:text="#string/store_str"
app:layout_constraintBottom_toBottomOf="#+id/text2"
app:layout_constraintStart_toEndOf="#+id/text2"
app:layout_constraintTop_toTopOf="#+id/text2" />
<RadioButton
android:id="#+id/hyperRB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:button="#drawable/radio_button_bg"
android:paddingStart="8dp"
android:text="#string/hyper_str"
app:layout_constraintBottom_toBottomOf="#+id/text2"
app:layout_constraintStart_toEndOf="#+id/storeRB"
app:layout_constraintTop_toTopOf="#+id/text2" />
<RadioButton
android:id="#+id/restaurantRB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:button="#drawable/radio_button_bg"
android:paddingStart="8dp"
android:text="#string/restaurant_str"
app:layout_constraintBottom_toBottomOf="#+id/text2"
app:layout_constraintStart_toEndOf="#+id/hyperRB"
app:layout_constraintTop_toTopOf="#+id/text2" />
</RadioGroup>