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?
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 a radio group with the choices "Yes," "No," and "Maybe". I want a function that will return which choice the user picked.
Here is my code
<RadioGroup
android:id="#+id/answerRadioGroup"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:orientation="horizontal"
app:layout_constraintBottom_toTopOf="#+id/submitButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/myTextView"
app:layout_constraintVertical_bias="0.26">
<RadioButton
android:id="#+id/yesRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Yes" />
<RadioButton
android:id="#+id/noRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="No" />
<RadioButton
android:id="#+id/maybeRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Maybe" />
</RadioGroup>
And here is the function I am trying to use:
private String checkGuess(){
guessRadioGroup = (RadioGroup) findViewById(R.id.guessRadioGroup);
int selectedId = guessRadioGroup.getCheckedRadioButtonId();
radioButton = (RadioButton) findViewById(selectedId);
String s = (String) radioButton.getText().toString();
return s;
}
This function seems to work when I replace String s = (String) radioButton.getText().toString(); with something simply like String s = "Whatever". The problem seems to be with using the getText() method.
This is what I use in MainActivity.java to display the text:
TextView tempTextView = (TextView) findViewById(R.id.tempTextView);
tempTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String abc = checkGuess();
tempTextView.setText("You chose: " + abc);
}
});
You should use CustomListener for radioButton
radioButton.setOnCheckedChangeListener(new CustomListener());
class CustomListener implements CompoundButton.OnCheckedChangeListener{
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()){
case R.id.rd1:
break;
}
}
}
You should use your radioGroup as parent for find radio button. And also you don't need cast as String while using toString().
private String checkGuess(){
guessRadioGroup = (RadioGroup) findViewById(R.id.guessRadioGroup);
int selectedId = guessRadioGroup.getCheckedRadioButtonId();
radioButton = (RadioButton) guessRadioGroup.findViewById(selectedId);
String s = radioButton.getText().toString();
return s;
}
Hope it will help you.
I wan retrieve radio buttons, created in XML. It's OK. But when, I check if this radio button is checked, Android Studio tell me than the radio button retrieve is empty.
XML :
<RadioGroup
android:layout_width="148dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/RadioGroup_MODE">
<!-- android:buttonTint="#color/BouttonsRadio"-->
<RadioButton
android:layout_width="137dp"
android:layout_height="wrap_content"
android:text="#string/Mode_J"
android:id="#+id/BouttonRADIO_JOUR"
android:layout_gravity="center_horizontal"
android:textSize="20dp"
android:textStyle="bold" />
<RadioButton
android:layout_width="137dp"
android:layout_height="wrap_content"
android:text="#string/Mode_N"
android:id="#+id/BouttonRADIO_NUIT"
android:layout_gravity="center_horizontal"
android:textSize="20dp"
android:textStyle="bold" />
</RadioGroup>
JAVA :
final RadioButton radiobutton_mode_j = (RadioButton) findViewById(R.id.BouttonRADIO_JOUR);
final RadioButton radiobutton_mode_n = (RadioButton) findViewById(R.id.BouttonRADIO_NUIT);
I use this code for check if it's this radiobutton which is checked :
radiobutton_mode_j.isChecked()
radiobutton_mode_n.isChecked()
Try below code:
int selectedId = radioGroup.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) findViewById(selectedId);
Toast.makeText(MyAndroidAppActivity.this,
radioButton.getText(), Toast.LENGTH_SHORT).show();
Edit
Here is the full code as per your request:
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:layout_width="148dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/RadioGroup_MODE">
<!-- android:buttonTint="#color/BouttonsRadio"-->
<RadioButton
android:layout_width="137dp"
android:layout_height="wrap_content"
android:text="#string/Mode_J"
android:id="#+id/BouttonRADIO_JOUR"
android:layout_gravity="center_horizontal"
android:textSize="20dp"
android:textStyle="bold" />
<RadioButton
android:layout_width="137dp"
android:layout_height="wrap_content"
android:text="#string/Mode_N"
android:id="#+id/BouttonRADIO_NUIT"
android:layout_gravity="center_horizontal"
android:textSize="20dp"
android:textStyle="bold" />
</RadioGroup>
<Button
android:id="#+id/btnDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
Activity:
private RadioGroup radioGroup;
private RadioButton radioButton;
private Button btnDisplay;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
radioGroup = (RadioGroup) findViewById(R.id.RadioGroup_MODE);
btnDisplay = (Button) findViewById(R.id.btnDisplay);
btnDisplay.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// get selected radio button from radioGroup
int selectedId = radioGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioButton = (RadioButton) findViewById(selectedId);
Toast.makeText(MyAndroidAppActivity.this,
radioButton.getText(), Toast.LENGTH_SHORT).show();
}
});
}
Try this:
RadioGroup rg = (RadioGroup) v.findViewById(R.id.RadioGroup_MODE);
if(rg.getCheckedRadioButtonId() != -1){
//one of the Radiobuttons is checked
}else {
//none of the Radiobuttons is checked
}
I am creating a simple calculator and I use radio buttons for choosing operators, but I faced the problem with radio button check. I hope somebody will help me. Thanks in advance.
Here are the following codes:
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/tekstas"
android:id="#+id/textas"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_gravity="center_horizontal" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:id="#+id/nr1"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_gravity="center_horizontal" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:id="#+id/nr2"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_gravity="center_horizontal" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:id="#+id/nr3"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_gravity="center_horizontal" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:id="#+id/nr4"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_gravity="center_horizontal" />
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/veiksmai">
<RadioButton android:id="#+id/plius"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/pliusas"
android:padding="30dp"
android:onClick="onRadioButtonClicked"
/>
<RadioButton android:id="#+id/minus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/minusas"
android:padding="30dp"
android:onClick="onRadioButtonClicked"
/>
<RadioButton android:id="#+id/daugyba"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/daugyba"
android:padding="30dp"
android:onClick="onRadioButtonClicked"
/>
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/rezultatas"
android:id="#+id/btn"
android:layout_gravity="center_horizontal"
android:onClick="onClick"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_gravity="center_horizontal" />
</LinearLayout>
java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View view) {
RadioGroup rg =(RadioGroup) findViewById(R.id.veiksmai);
EditText n1 = (EditText) findViewById(R.id.nr1);
EditText n2 = (EditText) findViewById(R.id.nr2);
EditText n3 = (EditText) findViewById(R.id.nr3);
EditText n4 = (EditText) findViewById(R.id.nr4);
EditText edtResult = (EditText) findViewById(R.id.editText);
int selected = rg.getCheckedRadioButtonId();
RadioButton plus2 = (RadioButton) findViewById(selected);
RadioButton minus2 = (RadioButton) findViewById(selected);
RadioButton multi = (RadioButton) findViewById(selected);
if("+".equals(plus2.getText())) {
float result = Float.parseFloat(n1.getText().toString()) + Float.parseFloat(n2.getText().toString())+ Float.parseFloat(n3.getText().toString()) + Float.parseFloat(n4.getText().toString());
edtResult.setText(Float.toString(result));
}else if("-".equals(minus2.getText())){
float result = Float.parseFloat(n1.getText().toString()) - Float.parseFloat(n2.getText().toString())- Float.parseFloat(n3.getText().toString())- Float.parseFloat(n4.getText().toString());
edtResult.setText(Float.toString(result));
}else if("*".equals(multi.getText())){
float result = Float.parseFloat(n1.getText().toString()) * Float.parseFloat(n2.getText().toString())* Float.parseFloat(n3.getText().toString())* Float.parseFloat(n4.getText().toString());
edtResult.setText(Float.toString(result));
}
}
};
// Getting reference of the button btnResult
Button btn = (Button) findViewById(R.id. btn);
// Setting onclick listener
btn.setOnClickListener(listener);
}
}
You need to id your elements in onCreate:
RadioGroup rg =(RadioGroup) findViewById(R.id.veiksmai);
EditText n1 = (EditText) findViewById(R.id.nr1);
EditText n2 = (EditText) findViewById(R.id.nr2);
EditText n3 = (EditText) findViewById(R.id.nr3);
EditText n4 = (EditText) findViewById(R.id.nr4);
EditText edtResult = (EditText) findViewById(R.id.editText);
RadioButton plus2 = (RadioButton) findViewById(R.id.plius);
RadioButton minus2 = (RadioButton) findViewById(R.id.minus);
RadioButton multi = (RadioButton) findViewById(R.id.daugyba);
And the attach an onCheckedChangedListener to it, not an onClick listener:
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup arg0, int arg1) {
if (arg0.getChildAt(0).isPressed()
|| arg0.getChildAt(1).isPressed()
|| arg0.getChildAt(2).isPressed()) {
if (arg0.getChildAt(0).isPressed()) {
//do your stuff here
} else if (arg0.getChildAt(1).isPressed()) {
//do your stuff here
} else if (arg0.getChildAt(2).isPressed()) {
//do your stuff here
}
}
}
});
In your xml you have
android:inputType="number"
this does not allow decimals or negatives. if you want decimals use
android:inputType="numberDecimal"
if you want to allow decimals and negative numbers use
android:inputType="numberDecimal|numberSigned"
When you use:
android:onClick="onRadioButtonClicked"
All you need to do is make a method passing in a view and do whatever it is you want on button (or radioButton) click. I made each radio button have their own method named onRadioButtonClicked for plus, onRadioButtonClicked1 for minus , and onRadioButtonClicked2 for multiplication. Then used the changed sign to get results in an onClickListener for a button.
private char sign;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RadioGroup rg = (RadioGroup) findViewById(R.id.veiksmai);
final EditText n1 = (EditText) findViewById(R.id.nr1);
final EditText n2 = (EditText) findViewById(R.id.nr2);
final EditText n3 = (EditText) findViewById(R.id.nr3);
final EditText n4 = (EditText) findViewById(R.id.nr4);
final EditText edtResult = (EditText) findViewById(R.id.editText);
RadioButton plus2 = (RadioButton) findViewById(R.id.plius);
RadioButton minus2 = (RadioButton) findViewById(R.id.minus);
RadioButton multi = (RadioButton) findViewById(R.id.daugyba);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (sign == '+') {
float result = Float.parseFloat(n1.getText().toString()) + Float.parseFloat(n2.getText().toString()) + Float.parseFloat(n3.getText().toString()) + Float.parseFloat(n4.getText().toString());
edtResult.setText(Float.toString(result));
} else if (sign == '-') {
float result = Float.parseFloat(n1.getText().toString()) - Float.parseFloat(n2.getText().toString()) - Float.parseFloat(n3.getText().toString()) - Float.parseFloat(n4.getText().toString());
edtResult.setText(Float.toString(result));
} else if (sign == '*') {
float result = Float.parseFloat(n1.getText().toString()) * Float.parseFloat(n2.getText().toString()) * Float.parseFloat(n3.getText().toString()) * Float.parseFloat(n4.getText().toString());
edtResult.setText(Float.toString(result));
}
}
});
}
public void onRadioButtonClicked(View view) {
sign = '+';
}
public void onRadioButtonClicked1(View view) {
sign = '-';
}
public void onRadioButtonClicked2(View view) {
sign = '*';
}
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