So, I have my buttons lined up like below:
Here's an image of what I'm trying to achieve:
In the image, the first row is just a row of buttons with no button pressed.
In the second row, only the first button is pressed. In the third row, I clicked on 4 and only the fourth button is pressed (not the first one anymore).
<RadioGroup
android:id="#+id/toggleGroup"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:useDefaultMargins="true"
android:layout_column="0"
android:columnCount="4"
android:rowCount="1"
android:orientation="horizontal"
>
<ToggleButton
android:id="#+id/number_zero"
android:layout_width="34sp"
android:layout_height="40sp"
android:textOn="#string/number_zero"
android:textOff="#string/number_zero"
android:onClick="onToggle"
android:checked="true"
android:background="#drawable/psc_number_color"
android:layout_margin="5sp"
/>
<ToggleButton
android:id="#+id/number_one"
android:layout_width="34sp"
android:layout_height="40sp"
android:textOn="#string/number_one"
android:textOff="#string/number_one"
android:onClick="onToggle"
android:checked="true"
android:background="#drawable/psc_number_color"
android:layout_margin="5sp"
/>
<ToggleButton
android:id="#+id/number_two"
android:layout_width="34sp"
android:layout_height="40sp"
android:textOn="#string/number_two"
android:textOff="#string/number_two"
android:background="#drawable/psc_number_color"
android:layout_margin="5sp"
/>
</RadioGroup>
I was wondering if it would be possible to toggle between these five buttons in such a way that if I pressed one down, the button would remain pressed. If I click on another button in that group, the button I clicked on before would depress and the new button I clicked on would be pressed down.
Trying to do this inside a pageradapter:
public void onToggle(View view) {
((RadioGroup)view.getParent()).check(view.getId());
// app specific stuff ..
}
public Object instantiateItem(ViewGroup parent, int position) {
one = (ToggleButton) view.findViewById(R.id.number_one);
two = (ToggleButton) view.findViewById(R.id.number_two);
three = (ToggleButton) view.findViewById(R.id.number_three);
four = (ToggleButton) view.findViewById(R.id.number_four);
final RadioGroup.OnCheckedChangeListener ToggleListener = new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(final RadioGroup radioGroup, final int i) {
for (int j = 0; j < radioGroup.getChildCount(); j++) {
final ToggleButton view = (ToggleButton) radioGroup.getChildAt(j);
view.setChecked(view.getId() == i);
}
}
};
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
one.toggle();
}
});
((RadioGroup) view.findViewById(R.id.toggleGroup)).setOnCheckedChangeListener(ToggleListener);
}
Try with this: I have done same things with Checkbox [ custom checkbox ].
#Override
public void onClick(View view) {
CheckBox cb = (CheckBox) view;
if(cb.isChecked()){
LinearLayout llLayout = (LinearLayout) cb.getParent();
for(int i=0; i<((ViewGroup)llLayout).getChildCount(); ++i) {
View nextChild = ((ViewGroup)llLayout).getChildAt(i);
if(nextChild instanceof CheckBox && nextChild.getId()==cb.getId() ){
}else if (nextChild instanceof CheckBox && nextChild.getId()!=cb.getId() ){
CheckBox cb2=(CheckBox) nextChild;
cb2.setChecked(false);
}
}
} else{
LinearLayout llLayout = (LinearLayout) cb.getParent();
for(int i=0; i<((ViewGroup)llLayout).getChildCount(); ++i) {
View nextChild = ((ViewGroup)llLayout).getChildAt(i);
if(nextChild instanceof CheckBox && nextChild.getId()==cb.getId() ){
CheckBox cb2=(CheckBox) nextChild;
cb2.setChecked(false);
}else if (nextChild instanceof CheckBox && nextChild.getId()!=cb.getId() ){
CheckBox cb2=(CheckBox) nextChild;
cb2.setChecked(false);
}
}
}
}
My XML:
<LinearLayout
android:id="#+id/llShift"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="4"
android:gravity="right"
android:orientation="horizontal" >
<CheckBox
android:id="#+id/cbMorning"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/custom_radio_button_morning"
android:paddingBottom="5dp"
android:paddingRight="3dp"
android:paddingTop="5dp" />
<CheckBox
android:id="#+id/cbEvning"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/custom_radio_button_evening"
android:paddingBottom="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp" />
</LinearLayout>
custom_radio_button_morning.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#drawable/morning_chk"
android:state_checked="true"/>
<item
android:drawable="#drawable/morning_unchk"
android:state_checked="false"/>
</selector>
You should use custom toggle button - check this link here and here
Related
I'm doing an android application where I have to dynamically put a button inside the table row. The problem is that the button that I create is stretched as in the image below:
I also put some of the application code here below so you can understand better.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_punteggio);
showAlertDialog_modality(R.layout.dialog_change_modality);
final TableLayout tableLayout = findViewById(R.id.tableLayout);
final RelativeLayout relativeLayout = findViewById(R.id.relativeLayout);
final Button btn_settings = findViewById(R.id.btn_settings);
Bundle datipassati = getIntent().getExtras();
String player = datipassati.getString("players");
giocatore = player.split("%");
Log.d("TAG", "array: " + giocatore[0]);
for (int i = 0; i < giocatore.length; i++) {
punti[i] = 0;
TableRow tbrow = new TableRow(this);
final TextView t3v = new TextView(this);
txPunti[i] = t3v;
//------------------------- Textview Player
final TextView t1v = new TextView(this);
t1v.setText(giocatore[i].toUpperCase());
t1v.setTextColor(Color.BLACK);
t1v.setGravity(Gravity.CENTER);
t1v.setTextSize(20);
t1v.setWidth(400);
tbrow.addView(t1v);
//-------------------- BTN MENO
Button btnMeno = new Button(this);
btnMeno.setText("-");
btnMeno.setTextColor(Color.BLACK);
btnMeno.setGravity(Gravity.CENTER);
tbrow.addView(btnMeno);
btnMeno.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
removepoint(t3v);
}
});
// --------------------- TEXT VIEW PUNTI
t3v.setText(punti[i]+"");
t3v.setTextSize(25);
t3v.setMaxWidth(150);
t3v.setPadding(20,0,20,0);
t3v.setTypeface(t3v.getTypeface(), Typeface.BOLD);
t3v.setTextColor(Color.RED);
t3v.setGravity(Gravity.CENTER);
tbrow.addView(t3v);
//----------------------------- BTN PIU
Button btnPiu = new Button(this);
btnPiu.setBackground(ContextCompat.getDrawable(Activity_punteggio.this, R.drawable.ic_add_circle_black_24dp));
btnPiu.setTextColor(Color.BLACK);
btnPiu.setGravity(Gravity.CENTER);
tbrow.addView(btnPiu);
btnPiu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addPoint(t3v, t1v);
}
});
tableLayout.addView(tbrow);
}
btn_settings.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showAlertDialog_modality(R.layout.dialog_change_modality);
}
});
}
Here's also the xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activity_punteggio">
<Button
android:id="#+id/btn_settings"
android:layout_width="55dp"
android:layout_height="32dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp"
android:layout_marginRight="1dp"
android:background="#android:color/transparent"
android:drawableBottom="#drawable/ic_settings_black_24dp" />
<TextView
android:id="#+id/title_player"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="Chip-Chop"
android:textSize="25dp"
android:textStyle="bold|italic" />
<TableLayout
android:id="#+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="80dp">
</TableLayout>
</RelativeLayout>
I hope you could help me.
Please check this solution. setImageResource is the method assign
image resource. Set background method is recommended for the
background of the button.
ImageButton btnPiu = new ImageButton(this);
btnPiu.setImageDrawable(ContextCompat.getDrawable(Activity_punteggio.this, R.drawable.ic_add_circle_black_24dp));
btnPiu.setTextColor(Color.BLACK);
btnPiu.setGravity(Gravity.CENTER);
tbrow.addView(btnPiu);
btnPiu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addPoint(t3v, t1v);
}
});
tableLayout.addView(tbrow);
I used ImageButton other than Button.
setImageDrawable
try to edit android:layout_height="32dp" to android:layout_height="55dp"
android:id="#+id/btn_settings"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp"
android:layout_marginRight="1dp"
android:background="#android:color/transparent"
android:drawableBottom="#drawable/ic_settings_black_24dp" />
I hope it will work with you .
THIS is make very complex coding, Make it simpler.Don't create whole row dynamically in FOR loop. Instead of that
First Create Separate XML for that row.
Write Below code and your row append into tablelayout.
for (int i = 0; i < giocatore.length; i++) {
View trView = LayoutInflater.from(getActivity()).inflate(R.layout.xmlID,null);
TextView lblGuestName;
lblGuestName = trView.findViewById(R.id.lbl);
lblGuestName.setText("Guest 1");
tableLayout.addView(trView);
}
This Way you easily configure any complex row in tablelayout. No need to make it dynamically.
Feel free to reply.
I have one radiogroup with database populated radiobuttons. When I repopulated them with:
Radiogroup fofo = (Radiogroup) findViewById(R.id.AperturasRadio);
fofo.removeAllViews();
for(int i = 0; i < mStringArray.length ; i++){
RadioButton newRadioButton;
newRadioButton = (RadioButton)
getLayoutInflater().inflate(R.layout.radio_template, null);
String label = (String) mStringArray[i];
newRadioButton.setText(label);
newRadioButton.setId(i);
if (label.equals("2.8")) {
newradiobutton.setChecked(true);
}
fofo.addView(newRadioButton);
fofo.updateBackground();
The problem is that I have a RadioGroup.OnCheckedChangeListener who triggers a diferent method that need the CheckedRadiobutton.Getleft()
RadioGroup.OnCheckedChangeListener listenerAperturas = new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int id) {
RadioGroup grupo1 = (RadioGroup) findViewById(R.id.AperturasRadio);
int radioButtonID = grupo1.getCheckedRadioButtonId();
RadioButton radiogrButton = (RadioButton) findViewById(radioButtonID);
int left = radiogrButton.getLeft();
Toast.makeText(getApplicationContext(), "Botton radio: " + left + " " , Toast.LENGTH_SHORT).show();
But the value is 0. The first time radiogroup populates works fine, but when I trigger de repopulation just returns zero.
The layout is this:
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/horiz"
android:scrollbars="none">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Space
android:layout_width="500px"
android:layout_height="match_parent" />
<com.alfbishop.testing.testbbdd.ListasBotones
android:id="#+id/AperturasRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="41dp"
android:orientation="horizontal">
</com.alfbishop.testing.testbbdd.ListasBotones>
<Space
android:layout_width="500px"
android:layout_height="match_parent" />
</LinearLayout>
</HorizontalScrollView>
Could you help me to find a solution?
So currently, I am trying to create a selected state for three textviews
Currently, for each of the textviews ( H M S) the text is red:
However, when tap the H M or S I want it to turn another color--white.
So I tried to follow this:
Android - Textview change color on changing of state
and I did this (selected_text.xml):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:color="#ffd10011"/> <!-- selected -->
<item android:color="#color/red_highlight"/> <!-- default -->
</selector>
and applied that:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Hours"
android:textSize="30sp"
android:textColor="#color/selected_text"
android:id="#+id/hourtext"
android:layout_marginLeft="45dp"
android:layout_alignTop="#+id/minutetext"
android:layout_alignLeft="#+id/seekArc"
android:layout_alignStart="#+id/seekArc" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Minutes"
android:textSize="30dp"
android:textColor="#color/selected_text"
android:id="#+id/minutetext"
android:layout_below="#+id/seekArc"
android:layout_centerHorizontal="true"
android:layout_marginTop="28dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Second"
android:textSize="30dp"
android:textColor="#color/selected_text"
android:id="#+id/secondtext"
android:layout_alignTop="#+id/minutetext"
android:layout_alignRight="#+id/seekArc"
android:layout_alignEnd="#+id/seekArc"
android:layout_marginRight="43dp" />
However, the textviews do not change color after I click on them.
How do I fix this?
Also,
I was wondering if it is better to implement this in java code since I need to perform a different function after each textview is clicked/highlighted. If so, how can that be implemented?
You can do this programatically like this:
findViewById(R.id.txt).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView textView = (TextView) v;
if (textView.isSelected()) {
textView.setTextColor(Color.RED);
v.setSelected(false);
} else {
textView.setTextColor(Color.BLUE);
v.setSelected(true);
}
}
});
And here's my layout:
<LinearLayout 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"
tools:context=".MainActivity">
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="some text"
android:textSize="20sp"
android:textColor="#FF0000"/>
</LinearLayout>
EDIT:
For your specific case would be like:
View previousView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View.OnClickListener clickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView previousText = (TextView) previousView;
TextView curText = (TextView) v;
// If the clicked view is selected, deselect it
if (curText.isSelected()) {
curText.setSelected(false);
curText.setTextColor(Color.RED);
} else { // If this isn't selected, deselect the previous one (if any)
if (previousText != null && previousText.isSelected()) {
previousText.setSelected(false);
previousText.setTextColor(Color.RED);
}
curText.setSelected(true);
curText.setTextColor(Color.BLUE);
previousView = v;
}
}
};
findViewById(R.id.txt).setOnClickListener(clickListener);
findViewById(R.id.txt2).setOnClickListener(clickListener);
findViewById(R.id.txt3).setOnClickListener(clickListener);
}
I'm making a simple "novelty" app that counts how many sneezes are done, it's more of a fun app that builds up experience until I do my huge project during the summer. I have two buttons, one adds a sneeze and the other clears how many sneezes there currently are. It holds the highest number of sneezes that there were previously. The problem is, the TextViews never update, they only initialize to zero. I used a Toast.makeText() to make sure that the buttons are working (they are). Any help is appreciated. Thanks
Java code:
public class MainActivity extends ActionBarActivity {
private int record_number = 0;
private int current_number = 0;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
Button add_one = (Button) findViewById(R.id.addone);
Button clear_1 = (Button) findViewById(R.id.clear);
add_one.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
current_number += 1;
Toast.makeText(MainActivity.this, "Button Clicked " + current_number, Toast.LENGTH_SHORT).show();
}
});
clear_1.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
current_number = 0;
Toast.makeText(MainActivity.this, "Button Clicked", Toast.LENGTH_SHORT).show();
}
});
TextView rec_text = (TextView) findViewById(R.id.record_num);
TextView cur_text = (TextView) findViewById(R.id.current_num);
if (current_number >= record_number)
{
record_number = current_number;
}
rec_text.setText(String.valueOf(record_number));
cur_text.setText(String.valueOf(current_number));
}
}
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="com.michail.sneezecounter.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Sneeze Counter"
android:id="#+id/title"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Record Number of Sneezes:"
android:id="#+id/textView"
android:layout_below="#+id/title"
android:layout_centerHorizontal="true"
android:layout_marginTop="72dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add one Sneeze"
android:id="#+id/addone"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="76dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear Current Number of Sneezes"
android:id="#+id/clear"
android:layout_marginBottom="13dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/record_num"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:singleLine="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Current Number of Sneezes:"
android:id="#+id/currentLabel"
android:layout_centerVertical="true"
android:layout_alignRight="#+id/textView"
android:layout_alignEnd="#+id/textView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_below="#+id/currentLabel"
android:layout_alignLeft="#+id/record_num"
android:layout_alignStart="#+id/record_num"
android:layout_marginTop="21dp"
android:id="#+id/current_num" />
</RelativeLayout>
You need to update the text of the TextViews inside the onClickListeners. In fact, all your logic for counting, clearing, and recording the record needs to be done in your onClickListeners (or methods called by them). Right now you only do it once in onCreate, then never again. You can do this in onCreate:
final TextView cur_text = (TextView) findViewById(R.id.current_num);
add_one.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
current_number += 1;
cur_text.setText(Integer.toString(current_number);
}
});
And similar for the other TextView & onClickListener.
You only set the contents of your TextViews once. You should update them every time you get a click event. Specifically:
add_one.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
current_number += 1;
if (current_number >= record_number)
{
record_number = current_number;
rec_text.setText(String.valueOf(record_number));
}
cur_text.setText(String.valueOf(current_number));
}
});
clear_1.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
current_number = 0;
cur_text.setText(String.valueOf(current_number));
}
});
NOTE: if your variables current_number, record_number, cur_text, and rec_text aren't already declared as class member variables, you'll want to do that do that so that they're accessible once you leave the scope of the method you're doing all this in (I assume it's onCreate(...).
What you are going to need to do here is update the labels during the on click events of the button. You currently only update them on activity create. This doesn't execute every time there is a click. Can I answer any questions about the fixed up version below?
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
final TextView rec_text = (TextView) findViewById(R.id.record_num);
final TextView cur_text = (TextView) findViewById(R.id.current_num);
Button add_one = (Button) findViewById(R.id.addone);
Button clear_1 = (Button) findViewById(R.id.clear);
add_one.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
current_number += 1;
if (current_number >= record_number)
record_number = current_number;
cur_text.setText(String.valueOf(current_number));
rec_text.setText(String.valueOf(record_number));
}
});
clear_1.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
current_number = 0;
cur_text.setText(String.valueOf(current_number));
rec_text.setText(String.valueOf(record_number));
}
});
cur_text.setText(String.valueOf(current_number));
rec_text.setText(String.valueOf(record_number));
}
I would like to create a list of RadioButtons to click on a Preference. I did the radio buttons in this way:
<RadioGroup>
<Preference android:key="ex" android:summary="Example" android:title="Example"/>
<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>
After, in the Java file
Preference ex;
ex = (Preference) this.findPreference("ex");
ex.setOnPreferenceClickListener(new OnPreferenceClickListener() {
});
After this I do not know how to do. You can post a code to click on the preference displays a list of radio buttons? Thank you very much
There is no out-of-the-box RadioGroup preference available. You would need to write one, by extending DialogPreference class.
Create a separate layout file (/res/layout/pref_radiogroup.xml) with the Dialog content.
<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<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:checked="true"
android:text="#string/radio_male" />
<RadioButton
android:id="#+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_female" />
</RadioGroup>
</FrameLayout>
Create your RadioGroupPreference class which will extend DialogPreference.
public class RadioGroupPreference extends DialogPreference {
private RadioGroup radioGroup;
public RadioGroupPreference(Context context, AttributeSet attrs) {
super(context, attrs);
setDialogLayoutResource(R.layout.pref_radiogroup);
}
#Override
protected View onCreateDialogView() {
View root = super.onCreateDialogView();
radioGroup = (RadioGroup) root.findViewById(R.id.radioSex);
return root;
}
#Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
setSelectedValue(getPersistedString("Male"));
}
#Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
if (!positiveResult) {
return;
}
if (shouldPersist()) {
String valueToPersist = getSelectedValue();
if (callChangeListener(valueToPersist)) {
persistString(valueToPersist);
}
}
}
#Override
protected Object onGetDefaultValue(TypedArray a, int index) {
return a.getString(index);
}
private void setSelectedValue(String value) {
for (int i = 0; i < radioGroup.getChildCount(); i++) {
View view = radioGroup.getChildAt(i);
if (view instanceof RadioButton) {
RadioButton radioButton = (RadioButton) view;
if (radioButton.getText().equals(value)) {
radioButton.setChecked(true);
}
}
}
}
private String getSelectedValue() {
int radioButtonId = radioGroup.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) radioGroup.findViewById(radioButtonId);
return (String) radioButton.getText();
}
}
Now you only have to add your custom preference to your preference screen.
<com.package.RadioGroupPreference
android:summary="Radio Group Preference Summary"
android:title="Radio Group Preference"
android:key="preference_key"
android:defaultValue="#string/radio_male" />