i am unable to gettext() from selected radio button in radioGroup. in my case i am getting value from bottomsheet and trying to get radio button text from fragment but
only one button is displaying the text. would you please check and let me know where i am wrong. thanks very much.
public class BottomSheet extends BottomSheetDialogFragment {
EditText editText;
RadioGroup radioGroup;
RadioButton radioButtonCash,radioButtonCard;
Button btn;
int i=0;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate ( R.layout.fragment_bottom_sheet, container, false );
editText=(EditText)view.findViewById ( R.id.addAmount );
btn=(Button)view.findViewById ( R.id.saveBtn );
radioGroup=(RadioGroup)view.findViewById(R.id.radioGrp);
int selectedId=radioGroup.getCheckedRadioButtonId ();
radioButtonCash=(RadioButton)view.findViewById ( selectedId );
radioButtonCard=(RadioButton)view.findViewById ( selectedId );
btn.setOnClickListener ( new View.OnClickListener () {
@Override
public void onClick(View view) {
i = Integer.parseInt (editText.getText ().toString () );
new bgthread ().start ();
if (selectedId==-1)
{
Toast.makeText ( getContext (), "nothing to show", Toast.LENGTH_SHORT ).show ();
}
else if (selectedId==R.id.radioButtonCash)
{
Toast.makeText ( getContext (), "text is"+radioButtonCash.getText (), Toast.LENGTH_SHORT ).show ();
}
else if (selectedId==R.id.radioButtonCard)
{
Toast.makeText ( getContext (), "text is"+radioButtonCard.getText (), Toast.LENGTH_SHORT ).show ();
}
}
} );
return view;
}
}
<EditText
android:id="@+id/addAmount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:inputType="number|numberDecimal"
android:hint="@string/enter_amount"
app:layout_constraintBottom_toTopOf="@+id/freenowImg"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintEnd_toStartOf="@+id/freenowImg"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</EditText>
<ImageView
android:id="@+id/freenowImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/freenow"
app:layout_constraintStart_toStartOf="@+id/addAmount"
app:layout_constraintTop_toBottomOf="@+id/addAmount"
android:contentDescription="TODO">
</ImageView>
<ImageView
android:id="@+id/boltImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="120dp"
android:src="@drawable/bolt"
app:layout_constraintEnd_toEndOf="@+id/addAmount"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="@+id/freenowImg"
app:layout_constraintTop_toBottomOf="@+id/addAmount" />
<ImageView
android:id="@+id/uberImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:src="@drawable/uber"
app:layout_constraintEnd_toEndOf="@+id/freenowImg"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/freenowImg"
app:layout_constraintTop_toBottomOf="@+id/freenowImg" />
<Button
android:id="@+id/saveBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="#009688"
android:text="Save"
app:backgroundTint="@android:color/darker_gray"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/uberImg"
app:layout_constraintVertical_bias="0.951" />
<RadioGroup
android:id="@+id/radioGrp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toTopOf="@+id/saveBtn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/uberImg">
<RadioButton
android:id="@+id/radioButtonCash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:layout_weight="1"
android:text="@string/cash"
/>
<RadioButton
android:id="@+id/radioButtonCard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="false"
android:text="@string/card" />
</RadioGroup>
</androidx.constraintlayout.widget.ConstraintLayout>
2
Answers
so finally i have find the solution specifically according to the problem i faced. in my case within the fragment the radio group was not working properly i used if else statement but i was getting id only for one radio button at a time.
now i am using switch statement within "radioGroup.setOnCheckedChangeListener" and its working absolutely according.
this block of your code just run once and not checked selectedId value after oncheckedChange call:
try this :
udpdate 1 :
change your findviewbyId from :
to and again try my code :