radioGroup how to hide button android - java

Hello I want to create a list. On long-press on the toolbar will be shown option to select all and delete selected. I don't know whether I should RadioGroup and hide button or use listView and create own row example and there add radio button.

Default standard android behavior is Contextual Action Bar(which I can interpret) should come when user long presses an item list
as in
One of the many resources are
http://theopentutorials.com/examples/android/listview/android-contextual-action-bar-for-listview-item-deletion-using-actionbarsherlock/
https://androidkennel.org/contextual-toolbar-actionbar-tutorial/

Where I cant get very specific, I can say, usually to achieve your own specific goals creating your own row will prove beneficial to your end goal. Rather then hiding a RadioGroup

I have a little problem with understanding menuInflater. This class instantiate menu XML files into Menu objects. But there set new menu
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) { //when this method is going to be made? Menu is int the toolbar and ListView isn't connected with toolbar so which menu I get in the next next line?
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.toolbar_cab, menu); // in this line set a new menu
return true;
}`

To hide RadioButton from RadioGroup is very simple. You just write btnRadio1.setVisibility(View.INVISIBLE);. BUT you have to know this rule: If you have, for example, 4 RadioButtons in RadioGroup, you can make them invisible in reverse order only! I mean the order they are defined in RadioGroup in your layout .xml file. It is impossible to hide btnRadio3 only, and btnRadio4 to be visible! You have to hide btnRadio3 and btnRadio4. Or only btnRadio4. So, if you want to hide 1 button, it is button 4. If you want to hide 2 buttons - they are 4 and 3. If you want to hide 3 buttons they are 4, 3, and 2. All other combinations, simply doesn't work.
Here is code from my Quiz app, where every question may have from 2 to 6 answers. The answers of current question are stored in array of strings answers [].
RadioButton btnAnswer1;
RadioButton btnAnswer2;
RadioButton btnAnswer3;
RadioButton btnAnswer4;
RadioButton btnAnswer5;
RadioButton btnAnswer6;
RadioGroup radioGroup;
// onCreate activity
btnAnswer1 = (RadioButton) findViewById(R.id.btnAnswer1);
btnAnswer2 = (RadioButton) findViewById(R.id.btnAnswer2);
btnAnswer3 = (RadioButton) findViewById(R.id.btnAnswer3);
btnAnswer4 = (RadioButton) findViewById(R.id.btnAnswer4);
btnAnswer5 = (RadioButton) findViewById(R.id.btnAnswer5);
btnAnswer6 = (RadioButton) findViewById(R.id.btnAnswer6);
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioGroup.clearCheck();
btnAnswer1.setVisibility(View.VISIBLE);
btnAnswer2.setVisibility(View.VISIBLE);
numberOfAnswers = 2; //at least 2 answers
//if 3-d element is empty i.e. 2 answers only
//i.e. buttons 3,4,5,6 must be hidden
if (answers[2].isEmpty()) {
btnAnswer3.setVisibility(View.INVISIBLE);
btnAnswer4.setVisibility(View.INVISIBLE);
btnAnswer5.setVisibility(View.INVISIBLE);
btnAnswer6.setVisibility(View.INVISIBLE);
} else {
btnAnswer3.setVisibility(View.VISIBLE);
numberOfAnswers = 3;
}
if (answers[3].isEmpty()) {
btnAnswer4.setVisibility(View.INVISIBLE);
btnAnswer5.setVisibility(View.INVISIBLE);
btnAnswer6.setVisibility(View.INVISIBLE);
} else {
btnAnswer4.setVisibility(View.VISIBLE);
numberOfAnswers = 4;
}
if (answers[4].isEmpty()) {
btnAnswer5.setVisibility(View.INVISIBLE);
btnAnswer6.setVisibility(View.INVISIBLE);
} else {
btnAnswer5.setVisibility(View.VISIBLE);
numberOfAnswers = 5;
}
if (answers[5].isEmpty()) {
btnAnswer6.setVisibility(View.INVISIBLE);
} else {
btnAnswer6.setVisibility(View.VISIBLE);
numberOfAnswers = 6;
}
And here is xml file:
<ScrollView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_marginLeft="5dip"
android:orientation="vertical">
<RadioGroup
android:id="#+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/btnAnswer1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="#+id/btnAnswer2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="#+id/btnAnswer3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="#+id/btnAnswer4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="#+id/btnAnswer5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="#+id/btnAnswer6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RadioGroup>
</ScrollView>

Related

Reverse show / hide of layouts with OnCheckedChangeListener on a RadioButton

THE CODE
For the checkout in my app, I let the user decide the payment method he wants to use by selecting it in a RadioButton Group:
<RadioGroup
android:id="#+id/payment_method"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<RadioButton
android:id="#+id/radio_prepaid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="prepaid"
android:tag="prepaid"
/>
<RadioButton
android:id="#+id/radio_creditcard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="creditcard"
android:textAllCaps="true"
android:tag="creditcard"
/>
</RadioGroup>
From the user selection I want to show or hide the matching Layout:
<RelativeLayout
android:id="#+id/relativeLayout_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
>
...
</RelativeLayout>
or
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/cL_preapid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
>
...
</ConstraintLayout>
I did it with this part of code:
CompoundButton.OnCheckedChangeListener onPaymentChangedListener = (compoundButton,b) -> {
if(compoundButton.getTag().equals("prepaid")) {
relativeLayout_card.setVisibility(View.GONE);
cl_prepaid.setVisibility(View.VISIBLE);
}
if(compoundButton.getTag().equals("creditcard")) {
cl_prepaid.setVisibility(View.GONE);
relativeLayout_card.setVisibility(View.VISIBLE);
}
};
rb_creditcard.setOnCheckedChangeListener(onPaymentChangedListener);
rb_prepaid.setOnCheckedChangeListener(onPaymentChangedListener);
THE PROBLEM
1. CLICK on my Prepaid / CreditCard RadioButton it shows the matching view perfectly
2. CLICK on my Prepaid / CreditCard RadioButton it does simply nothing
3. CLICK on my Prepaid / CreditCard RadioButton it reverses the views: "prepaid" shows the CreditCard Layout, "creditcard" shows me PrepaidPay Layout
Is this a problem with the Layouts (should I implement a ViewSwwichter / ViewPager) or is there an error in my code?
Thank you
Instead of each RadioButton try to set listener on RadioGroup
payment_method.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId)
{
RadioButton checkedRadioButton = (RadioButton)group.findViewById(checkedId);
if(checkedRadioButton.isChecked()) {
if (checkedId == R.id.radio_prepaid)
{
relativeLayout_card.setVisibility(View.GONE);
cl_prepaid.setVisibility(View.VISIBLE);
}
else if (checkedId == R.id.radio_creditcard)
{
cl_prepaid.setVisibility(View.GONE);
relativeLayout_card.setVisibility(View.VISIBLE);
}
}
}
});
Try to implement one listener for each button. Its easier to read and reduces code. No if branch.
You can write a lambda in each setOnCheckedChangeListener((compoundButton,b) -> {}) or better on the radiogroup.
The visibility issue isnt really clear due missing view hierarchy.

Android java - is it possible to get text from RadioButton in RadioGroup without each RadioButton having a defined id?

For android programming, I have a RadioGroup which contains some RadioButtons.
I want to just get the text from the button without need to get the id of the RadioButton.
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/radioGroup"
>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="text1"
/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="text2"
/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="text3"
/>
</RadioGroup>
Is it possible to just get the text?
You can access all Views within a RadioGroup with this code:
int count = radioGroup.getChildCount();
for (int i=0;i<count;i++) {
View radioButton = radioGroup.getChildAt(i);
if (radioButton instanceof RadioButton) {
Log.d(TAG,"text: "+ radioButton.getText().toString());
}
}
reference: Get the array of RadioButtons in a RadioGroup in Android
I don't think its possible.
Why don't you just provide id to each radiobutton ?

Android: Changing the contents of a Text View using a radio button

So, I am trying to write code that will show that Radio Button clicks register. Ideally, when a radio button is chosen, the contents of a TextView will change. To start, I have a radio button for 'North'. When North is checked, the contents of the TextView will become 'North'. I know there are action listeners involved, but I am not familiar with Java. This will surely pop my Java cherry. That being said, the code I have written is not working. Can anyone tell me if I am on the right track, or offer some suggestions? Note, this is not for a class assignment. This is for a very open ended class project that I am working on with another person.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onRadioButtonClicked(View v){
TextView text = (TextView)findViewById(R.id.text);
RadioButton N = (RadioButton) findViewById(R.id.north);
//evaluates 'checked' value of radio button
boolean checked = ((RadioButton) v).isChecked();
if(N.isChecked () ){
text.setText("N");
}
}
}
To use RadioButton properly you'd better group a bunch of RadioButtons into a set, named RadioGroup.
<RadioGroup
android:id="#+id/rg1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioButton
android:id="#+id/rg1_rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="North" />
<RadioButton
android:id="#+id/rg1_rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="South" />
<RadioButton
android:id="#+id/rg1_rb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Whatever" />
</RadioGroup>
The critical part is that you have to set unique android:id for each RadioButtons, or they won't work!
Next, find RadioButtons from your XML.
RadioButton rb1, rb2, rb3;
rb1 = (RadioButton) findViewById(R.id.rg1_rb1);
rb2 = (RadioButton) findViewById(R.id.rg1_rb2);
rb3 = (RadioButton) findViewById(R.id.rg1_rb3);
Finally, prepare a RadioButton.OnClickListener class instance and attach it to RadioButtons.
View.OnClickListener optionOnClickListener
= new View.OnClickListener() {
public void onClick(View v) {
TextView tv = (TextView) findViewById(R.id.textview);
String str = null;
// you can simply copy the string of clicked button.
str = ((RadioButton)v).getText().toString();
tv.setText(str);
// to go further with check state you can manually check each radiobutton and find which one is checked.
if(rb1.isChecked()) {
// do something
}
if(rb2.isChecked()) {
// do something
}
if(rb3.isChecked()) {
// do something
}
}
};
rb1.setOnClickListener(optionOnClickListener);
rb2.setOnClickListener(optionOnClickListener);
rb3.setOnClickListener(optionOnClickListener);
// check rb1 by default, if you want.
rb1.setChecked(true);
ADDED:
I'm sorry but I couldn't understand the edited version of my answer, since calling setOnClickListener() inside the View.OnClickLister.OnClick() was somewhat weird to me.
So I rolled back to my original answer.
Try
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.your_radio_group_id);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.north ;
// set text North for your textview here
break;
case R.id.another_radio_button_id:
// do something
break;
}
}
});
Check your xml file. radio button must be inside the radio group for work perfectly.
<RadioGroup
android:id="#+id/Type"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/n"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="I am a Blood Donor" />
<RadioButton
android:id="#+id/s"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RadioGroup>
then change in your java file
mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if (i == R.id.s) {
//Your action
} else if (i == R.id.n) {
//Your action
}
}
});
it will work.. :) :)
Okay, so making 'onCheckedChange' the onClick event seemed to do the trick. Thanks for all the help people.

Checking radio group either empty/null

I want to check in android that radiogroup is empty or null i.e submitted without clicking on any radio button in that group??
Xml Code
<RadioGroup
android:id="#+id/lift"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<RadioButton
android:paddingRight="50dip"
android:textSize="20dp"
android:id="#+id/r91"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/yes" />
<RadioButton
android:paddingRight="50dip"
android:textSize="20dp"
android:id="#+id/r92"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/no" />
</RadioGroup>
Activity Code
RadioGroup radioGroup1 = (RadioGroup) findViewById(R.id.lift);
if(radioGroup1=='NULL')
{
Toast.makeText(getApplOicationContext(),"first radiogroup is null",Toast.LENGTH_LONG).show();
}
Use below method to get checked radio button ID, if it returns -1 ,means user didn’t selected any radio button.
radioGroup.getCheckedRadioButtonId(); // it will return unique ID of checked radio button or -1 on empty selection.
see the doc here >> getCheckedRadioButtonID();
public int getCheckedRadioButtonId ()
Returns the identifier of the selected radio button in this group. Upon empty selection, the returned value is -1.
Related XML Attributes
android:checkedButton
Returns
the unique id of the selected radio button in this group
Edit :-
if(radioGroup1.getCheckedRadioButtonId() == -1) {
//empty selection
} else {
// user selected one radio button.
}

Get the ID of clicked radio button in android

Trying to get the id of the radio button that is checked in android, this is my XML code,
<RadioButton
android:id="#+id/RadioAuction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="55dp"
android:onClick="showAuctionOptions"
android:textColor="#3DCC00"
android:text="#string/RadioButton1" />
<RadioButton
android:id="#+id/RadioBin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:textColor="#FF0000"
android:text="Buy it now" />
so once it has been clicked it runs showAuctionOptions, this is my java code,
public void showAuctionOptions(View v){
if(findViewById(R.id.v=="RadioAuction")){
//Display start price
LinearLayout ShowPrice = (LinearLayout)findViewById(R.id.LayoutStartPrice);
ShowPrice.setVisibility(View.VISIBLE);
//Display reserve price
LinearLayout ShowReservePrice = (LinearLayout)findViewById(R.id.LayoutReservePrice);
ShowReservePrice.setVisibility(View.VISIBLE);
}
}
However this doesn't work, does anyone know why? Thanks.
Try
if (v == findViewById(R.id.RadioAuction)){
LinearLayout ShowPrice = (LinearLayout)findViewById(R.id.LayoutStartPrice);
ShowPrice.setVisibility(View.VISIBLE);
//Display reserve price
LinearLayout ShowReservePrice = (LinearLayout)findViewById(R.id.LayoutReservePrice);
ShowReservePrice.setVisibility(View.VISIBLE);
}
If this doesn't cut it either, make sure you've set the on click listeners for those radio buttons.
Probably faster would be:
if (v.getId() == R.id.RadioAuction){
LinearLayout ShowPrice = (LinearLayout)findViewById(R.id.LayoutStartPrice);
ShowPrice.setVisibility(View.VISIBLE);
//Display reserve price
LinearLayout ShowReservePrice = (LinearLayout)findViewById(R.id.LayoutReservePrice);
ShowReservePrice.setVisibility(View.VISIBLE);
}
And that way you can use the R values in a switch statement, if you have a long radio list:
switch (v.getId()) {
case R.id.RadioAuction:
// Do stuff
...
}

Categories

Resources