How to select Multiple options from RadioGroup Buttons? - android

it is possible to select multiple options from radiogroup buttons in android plz give some solution.
this my code in this code i can only select one radio button from the group plz give solution for select more then one selection.
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:id="#+id/text"
android:text="#string/ChoiceText" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text"
android:id="#+id/myRadioGroup"
android:background="#abf234"
android:checkedButton="#+id/sound" >
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/sound"
android:text="#string/Sound" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/vibration"
android:text="#string/Vibration" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/silent"
android:text="#string/Silent" />
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/myRadioGroup"
android:layout_marginTop="10dp"
android:id="#+id/chooseBtn"
android:text="#string/Choose" />
</RelativeLayout>
Activity
public class MainActivity extends Activity {
private RadioGroup radioGroup;
private RadioButton sound, vibration, silent;
private Button button;
private TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup = (RadioGroup) findViewById(R.id.myRadioGroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// find which radio button is selected
if(checkedId == R.id.silent) {
Toast.makeText(getApplicationContext(), "choice: Silent",
Toast.LENGTH_SHORT).show();
} else if(checkedId == R.id.sound) {
Toast.makeText(getApplicationContext(), "choice: Sound",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "choice: Vibration",
Toast.LENGTH_SHORT).show();
}
}
});
sound = (RadioButton) findViewById(R.id.sound);
vibration = (RadioButton) findViewById(R.id.vibration);
silent = (RadioButton) findViewById(R.id.silent);
textView = (TextView) findViewById(R.id.text);
button = (Button)findViewById(R.id.chooseBtn);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int selectedId = radioGroup.getCheckedRadioButtonId();
// find which radioButton is checked by id
if(selectedId == sound.getId()) {
textView.setText("You chose 'Sound' option");
} else if(selectedId == vibration.getId()) {
textView.setText("You chose 'Vibration' option");
} else {
textView.setText("You chose 'Silent' option");
}
}
});
}
}

Instead of using a radio group, just use some collection of radio buttons. RadioGroup is meant for selecting one from the list of radio buttons. So your xml will look like this after the changes
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:id="#+id/text"
android:text="#string/ChoiceText" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/sound"
android:text="#string/Sound" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/vibration"
android:text="#string/Vibration" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/silent"
android:text="#string/Silent" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/myRadioGroup"
android:layout_marginTop="10dp"
android:id="#+id/chooseBtn"
android:text="#string/Choose" />
See that the radio group is removed.

Please use checkbox if you want to have multiple entries. :)
http://developer.android.com/reference/android/widget/CheckBox.html

Related

How to set only one RadioButton Can be selected at the time in RadioGroup

I've created a radio button in radiogroup, but when I try running the apps all radio button can be selected all the time, and how to set only one radiobutton can be selected at one time?
I'm using Fragment
RadioGroup radioGroup = (RadioGroup) rootView.findViewById(R.id.RGroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// find which radio button is selected
if(checkedId == R.id.Abdominal) {
Toast.makeText(getActivity().getApplicationContext(), "choice: A",
Toast.LENGTH_SHORT).show();
} else if(checkedId == R.id.Arm) {
Toast.makeText(getActivity().getApplicationContext(), "choice: B",
Toast.LENGTH_SHORT).show();
} else if(checkedId == R.id.Back){
Toast.makeText(getActivity().getApplicationContext(), "choice: C",
Toast.LENGTH_SHORT).show();
} else if(checkedId == R.id.Chest){
Toast.makeText(getActivity().getApplicationContext(), "choice: D",
Toast.LENGTH_SHORT).show();
} else if(checkedId == R.id.Leg){
Toast.makeText(getActivity().getApplicationContext(), "choice: E",
Toast.LENGTH_SHORT).show();
} else if(checkedId == R.id.Shoulder){
Toast.makeText(getActivity().getApplicationContext(), "choice: F",
Toast.LENGTH_SHORT).show();
}
}
});
here my xml code for RG and RB
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/RGroup">
<TableRow android:weightSum="1">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Abdominal"
android:id="#+id/Abdominal"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Arm"
android:id="#+id/Arm"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:id="#+id/Back" />
</TableRow>
<TableRow>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chest"
android:id="#+id/Chest"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Leg"
android:id="#+id/Leg"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Shoulder"
android:id="#+id/Shoulder"/>
</TableRow>
</RadioGroup>
EDITED 1 : Answer :
If you dont want radio button can be selected in one time, so dont use Tablerow
It's not working because of TableRow inside RadioGroup. All RadioButtons are not grouped together because of TableRow between them.
RadioButton should be the direct child of RadioGroup, Otherwise grouping does not work.
Just change your code like this it will work :
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/RGroup">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Abdominal"
android:id="#+id/Abdominal"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Arm"
android:id="#+id/Arm"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:id="#+id/Back" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chest"
android:id="#+id/Chest"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Leg"
android:id="#+id/Leg"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Shoulder"
android:id="#+id/Shoulder"/>
</RadioGroup>
Hope this helps. :)
I have noticed that single selection does not work without setting id to radio buttons.
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/expenseRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/expense" />
<RadioButton
android:id="#+id/incomeRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/income" />
</RadioGroup>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:id="#+id/radioGroup">
<RadioButton
android:layout_width="0dp"
android:layout_weight="50"
android:layout_height="wrap_content"
android:text="Debit"
android:id="#+id/rDebit"
android:checked="false"
/>
<RadioButton
android:layout_width="0dp"
android:layout_weight="50"
android:layout_height="wrap_content"
android:text="Credit"
android:id="#+id/rCredit"
android:checked="false" />
</RadioGroup>
And in java file
RadioGroup radioGroup;
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
And when to do something
if (radioGroup.getCheckedRadioButtonId() == R.id.rCredit)
{
// do something
}
I encountered the same issue and found where I was making mistakes, i'm describing it below:
If you are using RadioButtons in RadioGroup, then RadioButtons should be Direct Child of RadioGroup.
Check first condition, if it is correct then give id to RatioButton and run project, It will solve the problem automatically.
Simple way. onclick of radio button. do code as per below.
public void clearRadioChecked() {
rdopa.setChecked(false);
rdopb.setChecked(false);
rdopc.setChecked(false);
rdopd.setChecked(false);
}
if you wann select rdopa then on click of rdopa do as below.
clearRadioChecked()
rdopa.setChecked(true);
You can use android:checkedButton attribute on RadioGroup, providing the id of the RadioButton you want to be checked initially and selecting another RadioButton will clear the previous selection.
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkedButton="#+id/rbNo"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="50dp"
android:text="Yes" />
<RadioButton
android:id="#+id/rbNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No" />
</RadioGroup>
You can create your own RadioGroup, that is able to find all RadioButtons that are nested in your group regardless of direct child or not. Below the code of my NestedRadioGroup. Using this instead of your RadioGroup in your xml file should do the trick.
public class NestedRadioGroup extends LinearLayout {
private SparseArray<RadioButton> radioButtons;
private int checkedId;
public NestedRadioGroup(Context context) {
this(context, null);
}
public NestedRadioGroup(Context context, AttributeSet attrs) {
super(context, attrs);
radioButtons = new SparseArray<>();
checkedId = -1;
}
#Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
super.addView(child, index, params);
findRadioButtons(child);
}
private void findRadioButtons(View child) {
if (child instanceof RadioButton) {
RadioButton newButton = (RadioButton) child;
newButton.setId(radioButtons.size());
newButton.setOnCheckedChangeListener((buttonView, isChecked) -> {
if (isChecked) {
if (checkedId != -1) {
radioButtons.get(checkedId).setChecked(false);
}
radioButtons.get(buttonView.getId()).setChecked(true);
checkedId = buttonView.getId();
}
});
radioButtons.put(newButton.getId(), newButton);
} else if (child instanceof ViewGroup) {
ViewGroup group = (ViewGroup) child;
for (int i = 0; i < group.getChildCount(); i++) {
this.findRadioButtons(group.getChildAt(i));
}
}
}
}
Any ideas to make the code cleaner?
I hope it works out for you :)
We can use any layout like LinearLayout, RelativeLayout, ConstraintLayout or GridLayout as RadioGroup using the following method in Kotlin.
For the following example I used ConstraintLayout to get fixed width and height for RadioButton.
layout.xml
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/rbYellow"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="#dimen/space_2x"
android:layout_marginEnd="#dimen/space"
android:gravity="center"
android:text="Yellow"
android:textColor="#android:color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toStartOf="#+id/rbOrange"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<RadioButton
android:id="#+id/rbOrange"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="#dimen/space"
android:layout_marginEnd="#dimen/space"
android:gravity="center"
android:text="Orange"
android:textColor="#android:color/white"
app:layout_constraintBottom_toBottomOf="#id/rbYellow"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toStartOf="#+id/rbRed"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/rbYellow"
app:layout_constraintTop_toTopOf="#id/rbYellow" />
<RadioButton
android:id="#+id/rbRed"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="#dimen/space"
android:layout_marginEnd="#dimen/space"
android:gravity="center"
android:text="Red"
android:textColor="#android:color/white"
app:layout_constraintBottom_toBottomOf="#id/rbYellow"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toStartOf="#+id/rbGreen"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/rbOrange"
app:layout_constraintTop_toTopOf="#id/rbYellow" />
<RadioButton
android:id="#+id/rbGreen"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="#dimen/space"
android:layout_marginEnd="#dimen/space_2x"
android:gravity="center"
android:text="Green"
android:textColor="#android:color/white"
app:layout_constraintBottom_toBottomOf="#id/rbYellow"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/rbRed"
app:layout_constraintTop_toTopOf="#id/rbYellow" />
</androidx.constraintlayout.widget.ConstraintLayout>
Kotlin code
radioGroup.children.forEach { aBtn ->
(aBtn as RadioButton).setOnCheckedChangeListener { button, isChecked ->
if(isChecked) {
radioGroup.children.forEach { bBtn ->
if(bBtn.id != button.id) {
(bBtn as RadioButton).isChecked = false
}
}
}
}
}

Radio Button checking and unchecking programmatically in android

My requirement is to be able to be able to check only 1 radio button from the no of options available.
My layout is simple
User should be able to select only one of p1, p2 and p3.
The IDs for for the buttons are rB1,rB2 and rB3.
PS: I know it is possible to achieve the same with RadioGroup , but in this particular instance I want to go with Radio Button hence the query
Try this..
r1.setOnCheckedChangeListener(this);
r2.setOnCheckedChangeListener(this);
r3.setOnCheckedChangeListener(this);
And CheckedChangeListener
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
if (buttonView.getId() == R.id.rB1) {
r2.setChecked(false);
r3.setChecked(false);
}
if (buttonView.getId() == R.id.rB2) {
r1.setChecked(false);
r3.setChecked(false);
}
if (buttonView.getId() == R.id.rB3) {
r1.setChecked(false);
r2.setChecked(false);
}
}
}
and don't forget to implements OnCheckedChangeListener in your activity or fragment.
Try this way:
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/p1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="P1" />
<RadioButton
android:id="#+id/p2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="P2" />
<RadioButton
android:id="#+id/p3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="P3" />
</RadioGroup>
Use Radio group
XML:
<RadioGroup
android:id="#+id/radiogroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/rB1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="P1"
android:checked="true" />
<RadioButton
android:id="#+id/rB2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="P2" />
<RadioButton
android:id="#+id/rB3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="P3" />
</RadioGroup>
try below code:-
java
public class MyAndroidAppActivity extends Activity {
private RadioGroup radioSexGroup;
private RadioButton radioSexButton;
private Button btnDisplay;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
btnDisplay = (Button) findViewById(R.id.btnDisplay);
btnDisplay.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// get selected radio button from radioGroup
int selectedId = radioSexGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioSexButton = (RadioButton) findViewById(selectedId);
Toast.makeText(MyAndroidAppActivity.this,
radioSexButton.getText(), Toast.LENGTH_SHORT).show();
}
});
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RadioGroup
android:id="#+id/radioSex"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_male"
android:checked="true" />
<RadioButton
android:id="#+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_female" />
</RadioGroup>
<Button
android:id="#+id/btnDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btn_display" />
</LinearLayout>
for more detail see below link:-
http://www.mkyong.com/android/android-radio-buttons-example/
http://www.tutorialspoint.com/android/android_radiogroup_control.htm
The solution can be achieved by setting a android:onClick="onRadioButtonClick" in the layout.xml file and creating a onRadioButtonClick method in Activity.java
public void onRadioButtonClick(View v)
{
boolean checked = ((RadioButton) v).isChecked();
RadioButton r1 = (RadioButton) findViewById(R.id.rB1);
RadioButton r2 = (RadioButton) findViewById(R.id.rB2);
RadioButton r3 = (RadioButton) findViewById(R.id.rB3);
switch(v.getId()) {
case R.id.rB1:
if (checked){
r2.setChecked(false);
r3.setChecked(false);
}
break;
case R.id.rB2:
if (checked){
r1.setChecked(false);
r3.setChecked(false);
}
break;
case R.id.rB2:
if (checked){
r1.setChecked(false);
r2.setChecked(false);
}
break;
}
I am posting this soulution because I couldn't find the solution to this query on this site(when I was looking for it earlier in the day) and had to figure it out myself. This might be useful for someone else looking for a similar solution.

Problems with Radio Button Group

Am trying to use radio button group widget to select gender in my application. Now here is the thing. The user will select a gender and on clicking the button the user will be guided to the respective activity. I wrote the code . The second problem is that my program does nothing though am launching new activities in the if part. I debugged my program to see if the if condition is satisfied seems to me it does. Here is the xml and the .java file
public class Gender extends Activity{
private RadioGroup radioSexGroup;
private RadioButton radioSexButton;
private Button btnDisplay;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gender_asking);
addListenerOnButton();
}
public void addListenerOnButton() {
radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
btnDisplay = (Button) findViewById(R.id.gender_button);
btnDisplay.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//****************************
int selectedId = radioSexGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioSexButton = (RadioButton) findViewById(selectedId);
if("Iam a Guy!".equals(radioSexButton.getText()))
{
Intent male_activity = new Intent(Gender.this, Meter.class);
startActivity(male_activity);
}
if("Iam a Girl!".equals(radioSexButton.getText()))
{
Intent male_activity = new Intent(Gender.this, For_Girl.class);
startActivity(male_activity);
}
//********************************
}
});
}
}
and the associated xml is:
<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/hyu_bg"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".KissingMeter" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp"
android:text="Tell Us Your Gender!"
android:textSize="30dp" />
<RadioGroup
android:id="#+id/radioSex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/gender_button"
android:layout_below="#+id/textView1"
android:layout_marginTop="30dp" >
<RadioButton
android:id="#+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/radioFemale"
android:layout_alignLeft="#+id/radioFemale"
android:layout_marginBottom="34dp"
android:checked="true"
android:text="Iam a Guy!" />
<RadioButton
android:id="#+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/gender_button"
android:layout_centerHorizontal="true"
android:layout_marginBottom="44dp"
android:text="Iam a Girl!" />
</RadioGroup>
<Button
android:id="#+id/gender_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/radioSex"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="Lol" />
</RelativeLayout>
First of all, don't rely on the text of a View in this situation. If you decide to change it later then it will create problems. Most likely, you won't change the id of it so just use that. Change it to something like this
btnDisplay.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
int selectedId = radioSexGroup.getCheckedRadioButtonId(); / get the id
switch (selectedId) // switch on the button selected
{
case R.id.radioMale:
Intent male_activity = new Intent(Gender.this, Meter.class);
startActivity(male_activity);
break;
case R.id.radioFemale:
Intent male_activity = new Intent(Gender.this, For_Girl.class);
startActivity(male_activity);
break;
default:
Toast.makeText(v.getContext(), "No gender selected",
Toast.LENGTH_SHORT).show();
break;
}
//********************************
}
});

How to make button come down after edit text field?

I have a piece of code which shows a static text, an edit text as a search box and a submit button to submit the query .
The code kind of looks like this :
public class MyActivity extends Activity {
TextView textView;
private EditText edittext;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mine);
textView = (TextView) findViewById(R.id.textView1);
textView.setText("Enter your search String :");
addKeyListener();
addListenerOnButton();
}
//Implement the method
public void addKeyListener() {
// get edit text component
edittext = (EditText) findViewById(R.id.searchText);
// add a key listener to keep track user input
edittext.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// if key down and "enter" is pressed
if ((event.getAction() == KeyEvent.ACTION_DOWN)
&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
// display a floating message
Toast.makeText(RecipeActivity.this,
edittext.getText(), Toast.LENGTH_LONG).show();
return true;
} else if ((event.getAction() == KeyEvent.ACTION_DOWN)
&& (keyCode == KeyEvent.KEYCODE_9)) {
// display a floating message
Toast.makeText(RecipeActivity.this,
"Aboriginal text!", Toast.LENGTH_LONG).show();
return true;
}
return false;
}
});
}
//Implement the button
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
/* I will capture the search string here */
}
});
}
}
The activity screen comes up like this :
EDIT : The XML layout:
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".RecipeActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<EditText
android:id="#+id/searchText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:label="#string/search_label"
android:hint="#string/search_hint" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
</RelativeLayout>
As you can see the submit button is overlapping the edit text search box. How can i make the button element come down ?
It would be better to use LinearLayout with android:orientation="vertical" in your xml Layout because it's much cheaper than RelativeLayout to render.
Solution with LinearLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="#+id/searchText"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>
If you have to use RelativeLayout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="#+id/searchText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/textView1"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/searchText"
android:text="Submit" />
</RelativeLayout>
See RelativeLayout Documentation for further Informations.
Use android:layout_alignParentBottom="true" in your
add android:below reference
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:below="#+id/searchText"
android:text="Submit" />

radio button reselecting not working properly in android,new in development

I'm having a set of 9 radio Buttons(a group of 3 each),user has to select 3 (1 of each group)
on right selection a increment in score while on wrong selection decrement in score,but problem is that if all 3 correct ans are selected score is coming right else not ,also it is not responding to re selecting of another radio button of the group.On submit button i have written the code, I am new in development so kindly guide
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()) {
case R.id.qa:cb.setChecked(false);
cc.setChecked(false);
cd.setChecked(false);
s[0] = false;
break;
case R.id.qb:
ca.setChecked(false);
cc.setChecked(false);
cd.setChecked(false);
s[0] = false;
break;
case R.id.qc:
cb.setChecked(false);
ca.setChecked(false);
cd.setChecked(false);
s[0] = true;
break;
}
public void onClick(View v) {
score = 0;
for (int i = 0; i < 3; i++) {
if (s[i] == true)
score++;
else
score--;
}
where s is boolean s[] = new boolean[];
I am new in development so not able to sort out problem
The code sample below will add the radiobuttons dynamically to a group so that you can arrange the buttons in any way you like using any layouts.
Layout : main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1" >
<RadioButton
android:id="#+id/rbtn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="A" />
<RadioButton
android:id="#+id/rbtn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="B" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1" >
<RadioButton
android:id="#+id/rbtn3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="C" />
<RadioButton
android:id="#+id/rbtn4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="D" />
</LinearLayout>
</LinearLayout>
Code : MainActivity.xml
public class MainActivity extends Activity {
private static final String GROUP_A = "GroupA";
private static final String GROUP_B = "GroupB";
private List<RadioButton> radioButtons;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RadioButton rbtn1 = (RadioButton) findViewById(R.id.rbtn1);
RadioButton rbtn2 = (RadioButton) findViewById(R.id.rbtn2);
RadioButton rbtn3 = (RadioButton) findViewById(R.id.rbtn3);
RadioButton rbtn4 = (RadioButton) findViewById(R.id.rbtn4);
addRadioButtonToGroup(rbtn1, GROUP_A);
addRadioButtonToGroup(rbtn2, GROUP_A);
addRadioButtonToGroup(rbtn3, GROUP_A);
addRadioButtonToGroup(rbtn4, GROUP_B);
}
private void addRadioButtonToGroup(RadioButton rbtn, String group) {
rbtn.setTag(group);
if (radioButtons == null) {
radioButtons = new ArrayList<RadioButton>();
}
radioButtons.add(rbtn);
rbtn.setOnCheckedChangeListener(new RadioButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
for (int i = 0; i < radioButtons.size(); i++) {
RadioButton radioButton = radioButtons.get(i);
if (radioButton
.getTag()
.toString()
.equalsIgnoreCase(
buttonView.getTag().toString())) {
if (!radioButton.equals(buttonView)) {
radioButton.setChecked(false);
}
}
}
}
}
});
}
}
Here..First 3 radio buttons will belong to the group A and other to Group B, which makes sure one radio button in a group can be checked at a time. We also dont need the RadioGroup and this code gives the control to arrange Radio Buttons in anyway you like.
Use RadioGroup instead of checking and un checking the RadioButtons manually.
If You put 3 radio buttons in a radio group, you will be able to check a radio button at one time, RadioGroup will manage it.
Check the sample below : On Submit button click it will calculate the score.
layout : main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="First Question" />
<RadioGroup
android:id="#+id/rg1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/rbtn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="A" />
<RadioButton
android:id="#+id/rbtn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="B" />
<RadioButton
android:id="#+id/rbtn3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="C" />
</RadioGroup>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Second Question" />
<RadioGroup
android:id="#+id/rg2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/rbtn4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="P" />
<RadioButton
android:id="#+id/rbtn5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Q" />
<RadioButton
android:id="#+id/rbtn6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="R" />
</RadioGroup>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Third Question" />
<RadioGroup
android:id="#+id/rg3"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/rbtn7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="X" />
<RadioButton
android:id="#+id/rbtn8"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Y" />
<RadioButton
android:id="#+id/rbtn9"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Z" />
</RadioGroup>
<Button
android:id="#+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>
</ScrollView>
Activity :
package com.example.demo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends Activity {
private RadioGroup rg1;
private RadioGroup rg2;
private RadioGroup rg3;
private OnClickListener btnClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
int answerCount = 0;
if (rg1.getCheckedRadioButtonId() == R.id.rbtn1)
answerCount++;
if (rg2.getCheckedRadioButtonId() == R.id.rbtn5)
answerCount++;
if (rg3.getCheckedRadioButtonId() == R.id.rbtn9)
answerCount++;
Toast.makeText(MainActivity.this, String.valueOf(answerCount),
Toast.LENGTH_SHORT).show();
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
rg1 = (RadioGroup) findViewById(R.id.rg1);
rg2 = (RadioGroup) findViewById(R.id.rg2);
rg3 = (RadioGroup) findViewById(R.id.rg3);
Button btnSumbit = (Button) findViewById(R.id.btnSubmit);
btnSumbit.setOnClickListener(btnClickListener);
}
}

Categories

Resources