Search code examples
androidandroid-layoutradio-buttonandroid-linearlayoutcenter

Center a programmatically created radio button in its linear layout vertical parent Android


I am creating a linear layout programmatically with orientation = vertical. In that linear layout I am also creating 2 or 3 objects programmatically:

1-Image view

2-Text View (I create this object if it is needed)

3-Radio button

By problem is how I can center the radio button in its parent ?


edit: My code is in Xamarin.Android C# but I think it is easy to covert it to Java Android

                    LinearLayout mainLinearLayout = new LinearLayout(this)
                    {
                        Orientation = Orientation.Vertical

                    };


                    LinearLayout sub1LinearLayout = new LinearLayout(this)
                    {
                        //Set orientation = horizontal
                        Orientation = Orientation.Horizontal

                    };

                    //Add the sub1LinearLayout to the mainLinearLayout
                    mainLinearLayout.AddView(sub1LinearLayout);


                    for (int j = 0; j < count; j++)
                    {


                        LinearLayout sub2LinearLayout = new LinearLayout(this)
                        {
                            Orientation = Orientation.Vertical
                        };


                        LinearLayout.LayoutParams layoutParams =
                            new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent,
                                ViewGroup.LayoutParams.WrapContent);



                        RadioButton Box = new RadioButton(this){
                           //Set gravity = center
                              Gravity = GravityFlags.Center
                         };



                            switch (SomeMode)
                            {


                                //Show radio button + image
                                case Mode.ShowBoxAndImage:
                                {

                                    ImageView image = new ImageView(this);


                                    Bitmap imageBitmap = BitmapFactory.DecodeFile(path);


                                    image.SetImageBitmap(imageBitmap);


                                    sub2LinearLayout.AddView(image);


                                    layoutParams.SetMargins(padding, 0, 2 * padding, 0);

                                   break;
                                }
                                //Show radio button + its text + image
                                case Mode.ShowBoxAndImageAndText:
                                {
                                    ImageView image = new ImageView(this);


                                    Bitmap imageBitmap = BitmapFactory.DecodeFile(path);


                                    image.SetImageBitmap(imageBitmap);


                                    sub2LinearLayout.AddView(image);


                                    TextView Name = new TextView(this)
                                    {

                                        Text = name,


                                        TextSize = nameTextSize,

                                        //Set gravity = center
                                        Gravity = GravityFlags.Center
                                    };


                                    Name.SetTextColor(new Color(ContextCompat.GetColor(this, Resource.Color.colorBlack)));

                                    sub2LinearLayout.AddView(Name);


                                    layoutParams.SetMargins(padding, 0, 2 * padding, 0);

                                    break;
                                }

                            }
                    }


                            sub2LinearLayout.AddView(Box);


                            sub2LinearLayout.LayoutParameters = layoutParams;


                            sub1LinearLayout.AddView(sub2LinearLayout);

                            //Set sub2LinearLayout gravity = bottom
                            sub2LinearLayout.SetGravity(GravityFlags.Bottom);

enter image description here


Solution

  • I fixed it by creating LayoutParams for every view and add it with its view to the parent linear layout.

    LinearLayout.LayoutParams center = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent)
                                        {
                                            Gravity = GravityFlags.Center
                                        };