I tried to implement a RadioGroup item to my XML layout. The code compiled just fine, however, in the App the RadioGroup (last quarter of the XML-file) is not showing. I read that a reason could be the orientation of the Linearlayout, but in my case, that looks right for me. As you can see in my code I already implemented some CardViews (they all work perfectly) what leads me to the idea that something with the RadioGroup itself is wrong. I will also post parts of my Java code because it's also likely to affect the size of the RadioGroup. Thanks for your help.
XML-Layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="@color/colorBlack">
<androidx.core.widget.NestedScrollView
android:id="@+id/nestedScrollView"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:clipToPadding="false"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="5dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/food_name"
android:layout_marginTop="8dp"
android:padding="12dp"
android:text="Food Name"
android:textSize="18sp"
android:textColor="@color/colorAccent"
android:textStyle="bold"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/layout_price"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/ic_attach_money_black_24dp"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/food_price"
android:layout_weight="9"
android:text="1000"
android:textColor="@color/colorAccent"
android:textSize="16sp"
android:textStyle="bold"/>
</LinearLayout>
<com.cepheuen.elegantnumberbutton.view.ElegantNumberButton
android:id="@+id/number_button"
android:layout_width="80dp"
android:layout_height="25dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="18dp"
app:backGroundColor="@color/colorAccent"
app:finalNumber="20"
app:initialNumber="1"
app:textSize="6sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="5dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ratingBar"
style="?android:attr/ratingBarStyleIndicator"
android:numStars="5"
android:stepSize="0.5"
android:isIndicator="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/food_description"
android:layout_marginTop="12dp"
android:lineSpacingMultiplier="1.5"
android:padding="12dp"
android:text="Description"
android:textColor="@android:color/black"
android:textSize="14sp"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
app:cardElevation="5dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/size"
android:textSize="22sp"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3"
android:id="@+id/rdi_group_size"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnShowComment"
android:layout_alignParentBottom="true"
android:layout_margin="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:text="Show Comment"
android:background="@drawable/roundbtn"
android:textColor="@color/colorWhite"
app:cornerRadius="4dp"/>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
<com.andremion.counterfab.CounterFab
android:id="@+id/btnCart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_shopping_cart_black_24dp"
app:backgroundTint="@android:color/white"
app:elevation="6dp"
app:layout_anchor="@id/app_bar_layout"
app:layout_anchorGravity="bottom|right|end"
app:pressedTranslationZ="12dp"
app:useCompatPadding="true"/>
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/collapsing"
android:layout_height="350dp"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="#0e0d0e"
app:expandedTitleTextAppearance="@android:color/transparent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/img_food"
android:contentDescription="@null"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax"/>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/btn_rating"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:backgroundTint="@android:color/white"
android:src="@drawable/ic_star_black_24dp"
app:elevation="6dp"
app:layout_anchor="@id/app_bar_layout"
app:layout_anchorGravity="bottom|right|start"
app:pressedTranslationZ="12dp"
app:useCompatPadding="true"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Java-Fragment and BindView
@BindView(R.id.rdi_group_size)
RadioGroup rdi_group_size;
for(SizeModel sizeModel: Common.selectedFood.getSize())
{
RadioButton radioButton=new RadioButton(getContext());
radioButton.setOnCheckedChangeListener((compoundButton, b) -> {
if(b)
Common.selectedFood.setUserSelectedSize(sizeModel);
calculateTotalPrice();
});
LinearLayout.LayoutParams params =new LinearLayout.LayoutParams(0,
LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
radioButton.setLayoutParams(params);
radioButton.setText(sizeModel.getName());
radioButton.setTag(sizeModel.getPrice());
rdi_group_size.addView(radioButton);
}
if(rdi_group_size.getChildCount()>0)
{
RadioButton radioButton =(RadioButton)rdi_group_size.getChildAt(0);
radioButton.setChecked(true);
}
calculateTotalPrice();
The second one <LinearLayout android:layout_width="match_parent" />
is non-sense.
Better set android:layout_width="0dp"
together with android:layout_weight="1.00"
... in order not to have the trailing one node pushed outside of the screen boundaries.
I think you should put the RadioButtons in RadioGroup tag, for example:
<RadioGroup
android:id="@+id/myRadioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkedButton="@+id/btn1" >
<RadioButton
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="btn1" />
<RadioButton
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="btn2" />
<RadioButton
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="btn3" />
</RadioGroup>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.