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.
}
Related
When I choose the first choice (correct answer), click Submit to take the value and update the variable does not happen, but when click on the second option without clicking on submit button and back and click on the first option and press submit, it takes the value and the variable updated.
how can take the value from the first selection.
xml
<RadioGroup
android:id="#+id/radioGroup"
android:layout_width="fill_parent"
android:layout_height="90dp"
android:layout_marginTop="8dp">
<RadioButton
android:id="#+id/radioButton0"
style="#style/MyRadio"
android:text="True"
/>
<RadioButton
android:id="#+id/radioButton1"
style="#style/MyRadio"
android:text="False" />
</RadioGroup>
java
RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup);
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
score2=0;
switch(checkedId){
case R.id.radioButton0:
score2+=0;
break;
case R.id.radioButton1:
score2+=10;
break;
}
}
});
When radioButton0 is clicked, you aren't doing anything. You are adding 0 to 0.
Additionally, each time you click a radio button the value is reset, since score2=0 is inside onCheckedChanged.
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>
How do I ensure that user is checked at least one radiobutton from each radiogroup. Like I have this radiogroup:
<RadioGroup
android:id="#+id/myradio_group_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="first"
android:checked="true" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="second" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="third" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fourt" />
</RadioGroup>
<RadioGroup
android:id="#+id/myradio_group_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="first"
android:checked="true" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="second" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="third" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fourt" />
</RadioGroup>
I want to check programmatically if user select at least one radiobutton or not?
Get a reference of the RadioGroup and then call getCheckedRadioButtonId() on the radio group if nothing is selected then the call will return -1.
See public int getCheckedRadioButtonId ()
RadioGroup g = (RadioGroup) findViewById(R.id.rBtnDigits);
// Returns an integer which represents the selected radio button's ID
int selected = g.getCheckedRadioButtonId();
// Gets a reference to our "selected" radio button
RadioButton b = (RadioButton) findViewById(selected);
// Now you can get the text or whatever you want from the "selected" radio button
b.getText();
if (radioGroup.getCheckedRadioButtonId() != -1)
{
// hurray at-least on radio button is checked.
}
else
{
// pls select at-least one radio button.. since id is -1 means no button is check
}
This is the code i have used in one of my quiz app.. Have a look at the code if this helps..
private boolean checkAnswer(){
String answer = getSelection();
if(answer==null) {
Log.d("Questions", "No checkbox selected");
return false;
}
else {
// Do your stuff here
return true;
}
}
private String getSelection(){
if (c1.isChecked())
{
return c1.getText().toString();
}
if (c2.isChecked())
{
return c2.getText().toString();
}
if (c3.isChecked())
{
return c3.getText().toString();
}
if (c4.isChecked())
{
return c4.getText().toString();
}
return null;
}
You can use this method
radioGroup.getCheckedRadioButtonId();
It returns the identifier of the selected radio button in this group. Upon empty selection, the returned value is -1.
As mentioned in the documentation https://developer.android.com/reference/android/widget/RadioGroup.html#getCheckedRadioButtonId()
I am trying to develop a Log in page, where I have Username, password and a button. When I click the button for the first time, nothing happens, but when I click the button for the second time, then it works properly. I am confused, why it is happening ?
activity_login.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/splash_bg"
tools:context=".LoginActivity" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:orientation="vertical"
android:layout_margin="30dp" >
<EditText
style="#style/EditText1"
android:id="#+id/userEditText"
android:layout_marginBottom="50dp"
android:inputType="textNoSuggestions"
android:singleLine="true"
android:hint="username" />
<EditText
style="#style/EditText1"
android:id="#+id/passEditText"
android:inputType="textPassword"
android:layout_marginBottom="50dp"
android:hint="password" />
<Spinner
android:id="#+id/locationSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:popupBackground="#ffffff"
style="#style/EditText1"
android:layout_marginBottom="50dp" />
<Button
style="#style/Button1"
android:focusable="true"
android:focusableInTouchMode="true"
android:onClick="onLoginClick"
android:text="continue"
/>
loginActivity.java
#SuppressLint("DefaultLocale")
public void onLoginClick(View view) {
String username = mUserEditText.getText().toString();
String password = mPassEditText.getText().toString();
String location = mLocationData.get(
mLocationSpinner.getSelectedItemPosition()).toLowerCase();
if (username.isEmpty() || password.isEmpty()) {
CreatorMessenger
.getInstance()
.showMessage(this, "Error!!",
"You need to enter username and password both to continue!!");
return;
}
User user;
user = new User(username);/*
* }
*/
user.setLocation(location);
AppManager.getInstance().setLoggedInUser(user);
APICaller.getInstance().login(username, password, location);
}
You are setting android:focusableInTouchMode="true"
Hence on 1st click it receives focus.
Refer Android button - How to set focusable to true and still accept onClick listener on first click?
Set this -
android:focusableInTouchMode="true"
to this -
android:focusableInTouchMode="false"
on button.
Explanation - If you'll make the button focusable then on first click the focus is passed to the button, then the click is passed on second touch. EditText is a focusable View which gains focus first and therefore other views must first regain the focus from EditText, unless they do not need focus to work, just like buttons. If you just need the OnClick function, then you don't need focus, so you can spre one extra click.
PS: Although it shouldn't require, but setting android:focusable to false will help too, if the first one doesn't work.
Yes Yes I got the answer, after a lot of RND, I got the solution, I just need to implement setOnClickListener(), and setOnFocusChangeListener(). So I am putting here the solution.
ActivityLogin.java
buttonLogin= (Button) findViewById(R.id.buttonLogin);
buttonLogin.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("hello", "hellow");
String username = mUserEditText.getText().toString();
String password = mPassEditText.getText().toString();
String location = mLocationData.get(mLocationSpinner.getSelectedItemPosition()).toLowerCase();
if(username.isEmpty()||password.isEmpty()){
popbox();
return;
}
User user;
user = new User(username);
user.setLocation(location);
AppManager.getInstance().setLoggedInUser(user);
APICaller.getInstance().login(username, password, location);
}
});
buttonLogin.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
v.performClick();
}
}
});
}
activity_login.xml
<Button
android:id="#+id/buttonLogin"
style="#style/Button1"
android:text="continue"
/>
if in your xml in button, or img, or liner layout has:
android:focusableInTouchMode="true"
To resolve, just
remove this.
I suggested to use request focus in the "onCreate()" method.
When you using android:focusableInTouchMode="true" you may don't want the focus catch by an 'EditText' field.
For example:
1. An activity has a view contain -> android:focusableInTouchMode
2. You open a Fragment Dialog, but the back button need click two times to fire it.
3. You should call requestFocus() after the Fragment Dialog onCreateView()
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
...
}