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;
}
//********************************
}
});
Related
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
I have array in xml and I am trying to get a particular element from array using its number from user input but my app is crashing when click on button.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
String [] tabela;
TextView textView;
TextView textView2;
TextView textView3;
Button next, prev;
int index;
SharedPreferences sPref;
SharedPreferences.Editor editor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sPref = getSharedPreferences("daniel.myapplication", Context.MODE_PRIVATE);
editor = sPref.edit();
// Importing the string array from Valuses folder
tabela = getResources().getStringArray(R.array.array);
// initialization of textview
textView =(TextView)findViewById(R.id.textView);
textView2 =(TextView)findViewById(R.id.textView2);
textView3 =(TextView)findViewById(R.id.textView3);
//Initialization of buttons
next =(Button)findViewById(R.id.button);
prev =(Button)findViewById(R.id.button2);
//OnClickListener for buttons
next.setOnClickListener(this);
prev.setOnClickListener(this);
// Setting values for variable and textviews
index = sPref.getInt("key", 0);
textView.setText(tabela[index]);
textView2.setText(String.valueOf(index+1));
textView3.setText("/"+String.valueOf(tabela.length));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onPause(){
super.onPause();
editor.putInt("key",index);
editor.commit();
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button2:
index++;
if (index==tabela.length){
index=0;
textView.setText(tabela[index]);
textView2.setText(String.valueOf(index+1));
}else {
textView.setText(tabela[index]);
textView2.setText(String.valueOf(index + 1));
}
break;
case R.id.button:
index--;
if (index ==-1){
index = tabela.length -1;
textView.setText(tabela[index]);
textView2.setText(String.valueOf(index+1));
}else {
textView.setText(tabela[index]);
textView2.setText(String.valueOf(index + 1));
}
break;
}
}
public void random(View view) {
Random losuj = new Random();
int i = losuj.nextInt(tabela.length);
textView.setText(tabela[i]);
textView2.setText(String.valueOf(i + 1));
}
public void press(View view) {
EditText editText = (EditText) findViewById(R.id.editText);
int b = Integer.parseInt(editText.getText().toString());
// b = (int) Array.get(tabela, b);
textView.setText(tabela[b]);
textView2.setText(String.valueOf(b + 1));
}
}
This is my 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="84dp"
android:textSize="25sp"
android:id="#+id/textView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/previous"
android:id="#+id/button"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/next"
android:id="#+id/button2"
android:layout_alignBottom="#+id/button"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/xx"
android:id="#+id/textView2"
android:textSize="20sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/yy"
android:id="#+id/textView3"
android:textSize="20sp"
/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/rand"
android:id="#+id/button3"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="random"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button4"
android:layout_below="#+id/button3"
android:layout_centerHorizontal="true"
android:layout_marginTop="78dp"
android:onClick="press"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_alignTop="#+id/button4"
android:layout_toRightOf="#+id/button4"
android:onClick="press"/>
int[] myArray = getResources().getIntArray(R.array.my_xml_int_array);
EditText editText = (EditText) findViewById(R.id.editText);
int b = Integer.valueOf(editText.getText().toString());
// Check the number is within range
if (b >= 0 && b < (myArray.length - 1)) {
textView.setText(String.valueOf(myArray[b]));
}
You should probably also make sure you set the input type for your EditText as number in your layout xml.
<EditText
android:id="#+id/editText"
...
android:inputType="number"/>
Additional:
As per my comments under your question "remove onClick from the EditText".
Your final EditText definition should look something like this:
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button4"
android:layout_toRightOf="#+id/button4"
android:inputType="number"/>
The most important thing is removing android:onClick="press" from the EditText.
When you run it, enter your number in the edit text and then press button4 (which you have labeled "New Button". The required string is then correctly shown in the TextView.
I have tested your code, copy/pasting it exactly as it is, and only changed the `EditText' as described, and created a dummy string-array in xml for testing. It works fine. If you are still having problems, provide some more description of what is happening.
In order to get an array from resource, do this somewhere within your onCreate():
ArrayList<String> yourArray;
yourArray = getResources().getStringArray(R.array.your_array_name_in_xml);
After that, you could just call yourArray.get(index) to get its content.
Given that, you could do this in order to get what you want:
int index = Integer.parseInt(yourEditText.getText().toString());
String textFromArray = yourArray.get(index);
yourTextView.setText(textFromArray);
I have two radio buttons i.e., call and email. When call is selected then the edit box should get enabled and then user may enter the mobile number.
I've checked these links:
Enable a text box only when 1 of the radio button is clicked
Enable text box based on radio button selected
Making Text box not editable
but am not able to achieve this task.
Here is my code:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp" >
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2"
android:orientation="horizontal">
<RadioButton
android:id="#+id/radio0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/call"/>
<RadioButton
android:id="#+id/radio1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/email"
android:layout_weight="1" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:weightSum="1">
<TextView
android:id="#+id/textView21"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/mobile"
android:textColor="#000000"
android:textSize="15sp"
android:layout_weight="0.3" />
<EditText
android:id="#+id/editText1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal"
android:maxLength="10"
android:layout_weight="0.7"
android:inputType="none" />
</LinearLayout>
.java
public class MainActivity extends Activity {
String mobile;
RadioGroup rg1 = (RadioGroup) findViewById(R.id.radioGroup1);
EditText edit = (EditText) findViewById(R.id.editText1);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit.setEnabled(false);
edit.setInputType(InputType.TYPE_NULL);
edit.setFocusable(false);
rg1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.radio0:
op1="call";
actv();
break;
case R.id.radio1:
op1="email";
break;
}
}
});
public void actv() {
edit.setEnabled(true);
edit.setInputType(InputType.TYPE_CLASS_TEXT);
edit.setFocusable(true);
}
Besides correcting your member initialization (as suggested in each of the previous answers), you need to fix your layout (it is closed too early, resulting in an invalid xml).
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:weightSum="2" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/call" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="true"
android:text="#string/email" />
</RadioGroup>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:weightSum="1" >
<TextView
android:id="#+id/textView21"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="#string/mobile"
android:textColor="#000000"
android:textSize="15sp" />
<EditText
android:id="#+id/editText1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:ems="10"
android:maxLength="10" />
</LinearLayout>
</LinearLayout>
Also decide which way you'd like to prevent the user from typing into the edittext (disable / hide / input type tweaks).
And last but not least, put all of these together in a clean manner:
public class MainActivity extends Activity implements OnCheckedChangeListener
{
private String op1;
private RadioGroup rg1;
private EditText edit;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rg1 = (RadioGroup) findViewById(R.id.radioGroup1);
rg1.setOnCheckedChangeListener(this);
edit = (EditText) findViewById(R.id.editText1);
actv(false);
}
#Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
switch (checkedId)
{
case R.id.radio0:
op1 = "call";
actv(true);
break;
case R.id.radio1:
op1 = "email";
actv(false);
break;
}
}
private void actv(final boolean active)
{
edit.setEnabled(active);
if (active)
{
edit.requestFocus();
}
}
}
#Zoya RadioGroup rg1 = (RadioGroup) findViewById(R.id.radioGroup1);
included this before onCreate method # MagicalPhoenixĻ” Not working
means the screen is not displayed..this is my 1st screen so as soon as
i start the app it says "the application has stopped
unexpectedly..please try again.."
It's giving crash.
REASON:
RadioGroup rg1 = (RadioGroup) findViewById(R.id.radioGroup1);
EditText edit = (EditText) findViewById(R.id.editText1);
You have included these before onCreate method.
This won't work, obviously.
Add that after your setContentView call inside onCreate method.
Hope it helps.
This is What you need to do
Change this
RadioGroup rg1 = (RadioGroup) findViewById(R.id.radioGroup1);
EditText edit = (EditText) findViewById(R.id.editText1);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
to this
RadioGroup rg1 = null;
EditText edit = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rg1 = (RadioGroup) findViewById(R.id.radioGroup1);
edit = (EditText) findViewById(R.id.editText1);
}
Thanks :)
add:
rg1 = (RadioGroup) findViewById(R.id.radioGroup1);
edit = (EditText) findViewById(R.id.editText1);
after:
setContentView(R.layout.activity_main);
in your onCreate();
Also make sure to set edit.setEditable(false) when radio1 is checked.
Complete Code:
public class MainActivity extends Activity {
String mobile;
RadioGroup rg1 = null;
EditText edit = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rg1 = (RadioGroup) findViewById(R.id.radioGroup1);
edit = (EditText) findViewById(R.id.editText1);
edit.setEnabled(false);
edit.setInputType(InputType.TYPE_NULL);
edit.setFocusable(false);
rg1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.radio0:
op1="call";
enableEdit(true);
break;
case R.id.radio1:
op1="email";
enableEdit(false);
break;
}
}
});
}
public void enableEdit(boolean state) {
edit.setEnabled(state);
edit.setFocusable(state);
if(state){ edit.setInputType(InputType.TYPE_CLASS_TEXT); }
else { edit.setInputType(InputType.TYPE_NULL); }
}
}
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.
I have a custom dialog preference that have bunch of button.each button represent a number.something like clock application when you want to set alarm. how can I get onClick of each button in my dialog preference?
this is part of my dialog layout (time_picker) :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<LinearLayout
android:id="#+id/showTimer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="_ _ : _ _"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
<RelativeLayout
android:id="#+id/Buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_below="#+id/showTimer"
android:layout_marginTop="10dp">
<Button
android:id="#+id/button1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button5"
android:layout_alignBottom="#+id/button5"
android:layout_alignLeft="#+id/button3"
android:layout_centerInParent="true"
android:text="6"
android:textStyle="bold"
android:textSize="20dp"
android:tag="1"
android:background="#android:color/transparent" />
<Button
android:id="#+id/button2"
android:layout_width="100dp"
...
this is Setting layout that launch dialog preference:
<com.example.test.TimerForNoti
android:key = "pref_sync_noti_timer"
android:title = "#string/pref_sync_noti_timer"
android:summary = "#string/pref_sync_noti_timer_summ"
android:dialogMessage = "#string/pref_sync_noti_timer">
</com.example.test.TimerForNoti>
and this is class of dialog preference (TimerForNoti) :
public class TimerForNoti extends DialogPreference
{
public TimerForNoti(Context context, AttributeSet attrs) {
super(context, attrs);
setDialogLayoutResource(R.layout.time_picker);
setPositiveButtonText(R.string.ok);
setNegativeButtonText(R.string.cancel);
Dialog di = new Dialog(context);
di.setContentView(R.layout.time_picker);
OnClickListener test = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String tag = v.getTag().toString();
if(tag == "1"){
Log.v("onOne", "ok");
}
// .....
}
};
Button btn1 = (Button) di.findViewById(R.id.button1);
btn1.setOnClickListener(test);
}
}
If I understand your question right, you can set tag for each button and set one OnClickListener for all of them:
<Button
android:id="#+id/button1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button5"
android:layout_alignBottom="#+id/button5"
android:layout_alignLeft="#+id/button3"
android:layout_centerInParent="true"
android:text="6"
android:textStyle="bold"
android:textSize="20dp"
android:tag="6"
android:background="#android:color/transparent" />
Code:
OnClickListener buttonsListener = new OnClickListener() {
#Override
public void onClick(View v) {
String buttonText = v.getTag().toString();
// your logic here
}
};
Button btn1 = (Button)findViewById(R.id.button1);
btn1.setOnClickListener(buttonsListener);
Button btn2 = (Button)findViewById(R.id.button2);
btn2.setOnClickListener(buttonsListener);
Button btn3 = (Button)findViewById(R.id.button3);
btn3.setOnClickListener(buttonsListener);
// your buttons here
Also you can use v.getId() in OnClickListener for determining which button was clicked
As much I understood your question. You want one method to be called on every button clicked.
For that Add this to your every button element in xml:
<Button
android:onClick="OnClick"
.. />
And this method to your .java file where you want to perform task:
public void OnClick(View v){
// do something....
}
Or you can directly call setOnClickListener at button object
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// do something....
}
});