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
...
}
Related
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.
<TableRow
android:id="#+id/Row3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">
<TextView
android:id="#+id/cell31"
android:layout_height="fill_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:textSize="50sp"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceLarge"
android:background="#drawable/back"
android:onClick="cellClick"
android:clickable="true"/>
<TextView
android:id="#+id/cell32"
android:layout_height="fill_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:textSize="50sp"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceLarge"
android:background="#drawable/back"
android:onClick="cellClick"
android:clickable="true"/>
.... this is 5x5 row column table. with 25 cells in it.
My .xml file looks like this. I have bunch of rows in LinearLayout contains TableLayouts which contains TextViews as cells. I am making a basic game with them.
I am trying to assign each cell from .xml to TextView array in .java file. But i can't figure out how to make it. I need to change color of each cell based on some algorithm i made so i want to access of cells.
private TextView colorBoard[][];
this is my .java array.
I can check if they are clicked with
public void cellClick(View v) {
TextView cell = (TextView) findViewById(v.getId());
switch (cell.getId()) {
case R.id.cell11:
xloc = 0;
break;
case R.id.cell12:
xloc = 1;
break ;
...rest of cells
But i only can assign which cell clicked at moment.In my game color of cells change differently. Not the one you just clicked. Assume you clicked 1x3 and 4x4 may change colors.
I'm new to android and this is my first project so sorry if i'm missing something basic.
I think you should use ListView instead of TextView. In ListView you can use an array for assigning those things where-ever you want to use them.
You can create layout file like this:
<ListView>
android:weight="match_parent"
android:height="match_parent"
android:id="+id/lst_view"
</ListView>
And Your Java Code looks like this
String[] array={"stack","queue","arraylist"};
ListView lst;
//In onCreate function
lst=(ListView)findViewById(R.id.lst_view);
ArrayAdapter<String> adpt=new ArrayAdapter<String>(this,R.layout.what_ever_your_xml,array);
lst.setAdapter(adpt);
//function of listview
//arrayadapter is used for converting string type array into listview
lst.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String s;
s=((TextView)view).getText().toString();
Toast.makeText(getApplicationContext(), s,Toast.LENGTH_LONG).show();
}
});
If you want to assign values to TextView Array.
Then do it like this in oncreate method after setContentView
colorBoard[0][0] = (TextView) findViewById(R.id.cell11);
colorBoard[0][1] = (TextView) findViewById(R.id.cell12);
You can use findviewbyId to get the TextView from xml to Java.
Suppose if you want to access cell44 when you tap on cell11, then you could do something like this.
public void cellClick(View v) {
TextView cell = (TextView) findViewById(v.getId());
switch (cell.getId()) {
case R.id.cell11:
xloc = 0;
colorBoard[0][0].setBackground()//As you wish
break;
case R.id.cell12:
xloc = 1;
break ;
...rest of cells
Once you have access to cell44 you can set the background color or any other properties to it.
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>
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.
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.
}