so I'm pretty new to Android Development, and I have the following situation:
<LinearLayout
android:id="#id/child_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="">
<Button
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="">
</LinearLayout>
The problem is that I have to repeat this Horizontal LinearLayout an unknown amount of times, dynamically adding the text fields of the TextView and Button for each new instance. Each new instance of the Horizontal LinearLayout has to be added to an existing Vertical LinearLayout.
So the initial Vertical LinearLayout:
<LinearLayout
android:id="#id/parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
Then based on the dynamic implementation:
<LinearLayout
android:id="#id/parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#id/child_1">
</LinearLayout>
<LinearLayout
android:id="#id/child_2">
</LinearLayout>
</LinearLayout>
Is there any way that this can be done as I described? The XML elements that I used in the example are base case. The ones I am actually using have styling attributes associated to them, so I would just like to create the first Horizontal LinearLayout in XML then programmatically access it, add the required fields, then set it as a child of the Vertical LinearLayout.
You can create the childs dynamically and add them to the parent vertical LinearLayout.
Please check the following code: I used a TextView and a Spinner for each created row:
//--- Row Layout ---
LinearLayout rowLayout = null;
LinearLayout.LayoutParams rowLayoutparams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
/** Create the TextViews and Spinners */
for (int j=0; j<gameArray.length; j++) {
//Create new row
rowLayout = new LinearLayout(getActivity().getApplicationContext());
rowLayout.setLayoutParams(rowLayoutparams);
rowLayout.setGravity(Gravity.CENTER_HORIZONTAL);
//--- TEXT VIEW ---
TextView myTextView = new TextView(getActivity().getApplicationContext());
myTextView.setId(j);
myTextView.setText("CONTENT");
myTextView.setGravity(Gravity.CENTER);
//--- SPINNER ---
spinnersArray[j] = new Spinner(getActivity().getApplicationContext());
spinnersArray[j].setId(j+100); //set a different ID
//Populate the spinner here ...
//--- LAYOUT PARAMS ---
LinearLayout.LayoutParams itemParams = new LinearLayout.LayoutParams(100, 100); //first and last item
//edit the item params here ...
//Add the params to the Views
myTextView.setLayoutParams(itemParams);
spinnersArray[j].setLayoutParams(itemParams);
//--- Add the view to the Layout ---
rowLayout.addView(myTextView);
rowLayout.addView(spinnersArray[j]);
//--- Add the row to the layout only if (NOT even) OR the chars are finished ---
((LinearLayout)getView().findViewById(R.id.parent)).addView(rowLayout);
}
Related
I am programmatically inflating combination of ImageView and TextView inside my LinearLayout.My linear layout is under a horizontal scroll view. But when I set layout_gravity="center_horizontal" to the parent of my linear layout, the content inside linear layout going out of the screen.
See what is happening:
I am using layout_gravity to achieve this
Without layout_gravity it appears like this
But if I use layout_gravity and there are many items inside that HorizontalScrollView then 1 or 2 items don't show. You can see the first uploaded image for understanding the scenario. In the first image I can't scroll more towards right and I can only see see of the ImageView and TextView combination and moreover, one more such combination is out of the screen completely.
Here's the xml structure
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbarAlwaysDrawHorizontalTrack="false"
android:scrollbars="none"
android:id="#+id/hotel_detail_services">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/imageViewLayout"
>
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
Java manipulation
#Override
protected void onPostExecute(List<hotel_detail_model> hotel_detail_models) {
super.onPostExecute(hotel_detail_models);
LinearLayout layout = findViewById(R.id.imageViewLayout);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setGravity(Gravity.CENTER_HORIZONTAL);
if (hotel_detail_models.get(0).getTv()==1)
addHotelSpeciality(layout,"TV",R.drawable.tv);
if (hotel_detail_models.get(0).getAc()==1)
addHotelSpeciality(layout,"AC",R.drawable.ac);
if (hotel_detail_models.get(0).getGeyser()==1)
addHotelSpeciality(layout,"Geyser",R.drawable.geyser);
if (hotel_detail_models.get(0).getBus()==1)
addHotelSpeciality(layout,"BUS",R.drawable.bus);
if (hotel_detail_models.get(0).getCab()==1)
addHotelSpeciality(layout,"CAB",R.drawable.cab);
if (hotel_detail_models.get(0).getFood()==1)
addHotelSpeciality(layout,"FOOD",R.drawable.food);
if (hotel_detail_models.get(0).getRailway()==1)
addHotelSpeciality(layout,"Train",R.drawable.train);
if (hotel_detail_models.get(0).getAirport()==1)
addHotelSpeciality(layout,"Airport",R.drawable.airport);
if (hotel_detail_models.get(0).getMedical()==1)
addHotelSpeciality(layout,"Medical",R.drawable.medical);
progressDialog.dismiss();
}
//Add Image Dynamically
private void addHotelSpeciality(LinearLayout layout, String image_name, int image_id) {
LinearLayout linearLayout = new LinearLayout(hotel_details.this);
ImageView image = new ImageView(hotel_details.this,null,R.style.dynamicImageHotel);
TextView textView = new TextView(hotel_details.this,null,R.style.dynamicTextHotel);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(getResources().getDimensionPixelSize(R.dimen._25sdp),
getResources().getDimensionPixelSize(R.dimen._25sdp));
params.weight = 1.0f;
params.gravity = Gravity.CENTER_HORIZONTAL;
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params2.weight = 1.0f;
params2.gravity = Gravity.CENTER_HORIZONTAL;
LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
params3.weight = 1.0f;
params2.gravity = Gravity.CENTER_HORIZONTAL;
image.setLayoutParams(params);
image.setScaleType(ImageView.ScaleType.FIT_CENTER);
textView.setLayoutParams(params2);
image.setImageDrawable( getResources().getDrawable(image_id));
textView.setText(image_name);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
textView.setTextAppearance(R.style.dynamicTextHotel);
}else {
textView.setTextAppearance(hotel_details.this,R.style.dynamicTextHotel);
}
// Adds the view to the layout
linearLayout.addView(image);
linearLayout.addView(textView);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(params3); linearLayout.setPadding(getResources().getDimensionPixelSize(R.dimen._15sdp),0,0,0);
layout.addView(linearLayout);
}
Isn't that what you are trying to achieve by using HorizontalScrollView? ScrollViews allow content to be larger than the physical display so when you add enough "hotelSpecialities" it goes out of the screen and you can scroll through it. If you are trying to fit all the content on the screen then you would not use a HorizontalScrollView.
Okay now that I know what you are trying to achieve, putting your ScrollView inside a RelativeLayout will give you that.
This is what worked out for me
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<HorizontalScrollView
android:id="#+id/hotel_detail_services"
android:layout_centerHorizontal="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:scrollbarAlwaysDrawHorizontalTrack="false"
android:scrollbars="none">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/imageViewLayout"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
You can remove the parent LinearLayout for imageViewLayout too if you don't need it. Let me know if you need anymore help!
I am creating a TextView dynamically. Here, I am creating a mainLayout in which I have two child layout and I wan gave them weight.
Here, is my Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:baselineAligned="false"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/Layout_second_overs"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="6"
android:orientation="vertical">
<!--Second textview overs-->
</LinearLayout>
<LinearLayout
android:id="#+id/Layout_second_balls"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal">
<!--Second textview balls--> //here i inflate 12 textview dynamically but not 12 seen on the device while i run the app.
</LinearLayout>
</LinearLayout>
</LinearLayout>
Code to create a 12 TextViews and put into layout.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
for(int a = 0; a <= 12; a++)
{
TextView first = new TextView(getApplicationContext());
first.setLayoutParams(params);
first.setTextColor(Color.parseColor("#000000"));
first.setTextSize(12);
first.setGravity(Gravity.LEFT);
first.setPadding(5, 0, 5, 0);
first.setText("12");
layout_second_balls.addView(first); //This is my linear layout which id is Layout_second_balls
}
Problem is my all data are print into log but not visible on the device.
Please, help me to solve out this problem.
There are two issues
One is for the Layout Design related
Second based on the layout design and property you also need to
handle is while you are adding dynamically view into linear layout
Here what i have changed in Your xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:baselineAligned="false"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/Layout_second_overs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--Second textview overs-->
</LinearLayout>
<LinearLayout
android:id="#+id/Layout_second_balls"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" android:background="#FF13C7FF"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
</LinearLayout>
In Layout_second_overs linearlayout height , Width & weight is the issue.
& in Layout_second_balls linear layout you have used weight as 1 so you also required layout_height as 0dp if orientation is horizontal
Now below things i have changed in code
LinearLayout.LayoutParams params = new
LinearLayout.LayoutParams
(0, LinearLayout.LayoutParams.WRAP_CONTENT);
for(int a = 0;a<=12;a++)
{
TextView first = new TextView(getActivity());
first.setLayoutParams(params);
first.setTextColor(Color.parseColor("#000000"));
first.setTextSize(12);
params.weight = 1; /// Need to give weight if you want equal size of textview inside Linear
first.setGravity(Gravity.LEFT);
first.setPadding(5, 0, 5, 0);
first.setText("12");
Layout_second_balls.addView(first); //This is my linear layout which id is Layout_second_balls
}
above things are based on the question which you ask
Suggestion
Avoid adding dynamic view like you asked . prefer to use ListView, GridView, RecyclerView for this type of functionality.
Your layout_second_overs requires all of your screen space - so your layout_second_balls is moved out of your screen.
If you want to see your two LinearLayouts beside each other, you need to set the layout_width to "0dp":
//pseudo, simply change width to 0dp
<LinearLayout
android:layout_height="40dp"
.... >
<LinearLayout
android:layout_width="0dp"
android:layout_weight="6"/>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"/>
</LinearLayout>
To replace this kind of loops you are supposed to use a RecyclerView. It is easy to use and responsive (since your list will be scrollable).
The things you have to remember while implementing a RecyclerView are to add an adapter and a LayoutManager to it.
For your example :
LayoutManager manager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, true);
YourAdapter adapter = new YourAdapter([...]);
RecyclerView recyclerView = findViewById(R.id.your_recyclerview_here);
recyclerView.setLayoutManager(manager);
recyclerView.setAdapter(adapter);
If you need any advices for creating your adapter just ask.
You can use this line of code.
LinearLayout layout_second_balls=(LinearLayout) findViewById(R.id.Layout_second_balls);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
for(int a = 0;a<=12;a++)
{
TextView first = new TextView(getApplicationContext());
first.setLayoutParams(params);
first.setTextColor(Color.parseColor("#000000"));
first.setTextSize(12);
first.setGravity(Gravity.LEFT);
first.setPadding(5, 0, 5, 0);
first.setText("12");
layout_second_balls.addView(first);
}
i run this code in my emulator and i got this out put.
You have to assign index for a view while performing addview(view,index) set index for each textview created in for loop like layout_second_balls.addView(first,index);
Code should look like :
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
for(int a = 0; a <= 12; a++)
{
TextView first = new TextView(getApplicationContext());
first.setLayoutParams(params);
first.setTextColor(Color.parseColor("#000000"));
first.setTextSize(12);
first.setGravity(Gravity.LEFT);
first.setPadding(5, 0, 5, 0);
first.setText("12");
layout_second_balls.addView(first,a);
}
Hope it will work.
Here is a cut-down version of the table layout im using. I'm trying to simulate a HTML percentage width by using a weightSum of 100 on the table and applying layout_weight on each view appropriately.
From my C# code I'm then looking to add each line item dynamically as new TableRow to the table layout. I can't seem to figure out how to ensure that the columns line up in the new rows. This is different to HTML, where columns are always the same width per row.
I've been messing around LayoutParameters to try and set a weight, but doesn't seem possible on a textview.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="#style/pagetemplate">
<TableLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px"
android:weightSum="100"
android:id="#+id/idBasketTable"
style="#style/pagetemplate">
<TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="10"
android:text="Options"
android:visibility="invisible" />
<TextView
android:text="Product Name"
style="#style/textViewHeader"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_weight="30"
android:layout_height="wrap_content" />
<TextView
android:text="Qty"
style="#style/textViewHeader"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_weight="30"
android:layout_height="wrap_content" />
<TextView
android:text="Price"
style="#style/textViewHeader"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_weight="30"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
<include
layout="#layout/Menu"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
Then from my code I'm attempting something similar to this (cut down for simplicity). You'll notice my attempted LayoutParameter code commented out on each View as this was making them disappear all together from the layout. Not sure whether I should be concentrating on the layout styles of the row itself or the individual views?
Secondly, in the first row, I've tried to create a column that has no title. The line item rows will include a small delete button here, which is why I've tried to make an empty column. Is there a better way to do this rather than creating a textView (used for nothing) and setting to invisible?
TableLayout tl = (TableLayout)FindViewById(Resource.Id.idBasketTable);
TableRow tr = new TableRow(this)
{
LayoutParameters = new TableRow.LayoutParams(TableRow.LayoutParams.MatchParent, TableRow.LayoutParams.MatchParent, 100)
};
TextView tv_ProductName = new TextView(this)
{
Text = item.ItemName//,
// LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent,ViewGroup.LayoutParams.WrapContent)
};
tv_ProductName.SetTextAppearance(this, Resource.Style.textViewDetail);
TextView tv_Qty = new TextView(this)
{
Text = item.ItemQty.ToString()//,
// LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent)
};
tv_Qty.SetTextAppearance(this, Resource.Style.textViewDetail);
TextView tv_Price = new TextView(this)
{
Text = item.ItemPrice.ToString()//,
// LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent)
};
tv_Price.SetTextAppearance(this, Resource.Style.textViewDetail);
ImageButton ib_Delete = new ImageButton(this)
{
// LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent,ViewGroup.LayoutParams.WrapContent)
};
//set the view values for the current row.
ib_Delete.SetImageResource(Resource.Drawable.deleteitem);
Guid itemID = item.ItemID;
//assign the delete onclick event.
ib_Delete.Click += delegate
{
deleteItem_OnClick(ib_Delete, itemID);
};
//add views to the row.
tr.AddView(ib_Delete);
tr.AddView(tv_ProductName);
tr.AddView(tv_Qty);
tr.AddView(tv_Price);
//add the row to the table layout.
tl.AddView(tr);
Hope this all makes sense, appreciate the feedback :)
I am adding 7 linear layouts to a vertical linear layout, and in each one, I am looping through a cursor and adding a view that has two textviews vertically, for each row of the cursor.
Everything is working fine, except the linear layout is not wrapping to the content of the child view.
I had a feeling it's because I was setting wrap_content in the layoutparams before adding the child views, but I updated the linearlayouts layoutparams just before adding it to the main view and it still doesn't wrap.
Dynamically adding code
while(c.moveToNext()) {
if(!(c.getString(c.getColumnIndex("date")).equals(startDate))) {
if(thisLin != null) {
LayoutParams thisLinParams = new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
thisLin.setLayoutParams(thisLinParams);
seven_wrapper.addView(thisLin);
}
thisLin = new LinearLayout(getActivity());
// originally I was setting layoutparams here but I removed this
// when I thought it wasn't wrapping properly as there were no child views
// at this stage
//LayoutParams thisLinParams = new LayoutParams(
// LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
//thisLin.setLayoutParams(thisLinParams);
TextView t = new TextView(getActivity());
t.setText(c.getString(c.getColumnIndex("date")));
LayoutParams lparams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
t.setLayoutParams(lparams);
thisLin.addView(t);
}
sevenItem = inflater.inflate(R.layout.seven_item, container, false);
TextView time = (TextView) sevenItem.findViewById(R.id.seven_item_time);
TextView name = (TextView) sevenItem.findViewById(R.id.seven_item_name);
time.setText(c.getString(c.getColumnIndex("time")));
name.setText(c.getString(c.getColumnIndex("name")));
thisLin.addView(sevenItem);
}
seven_wrapper.addView(thisLin);
c.close();
Fragment View
<HorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/seven_day_rows_wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"/>
</HorizontalScrollView>
Child view
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="#+id/seven_item_time"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"/>
<TextView android:id="#+id/seven_item_name"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#id/seven_item_time"/>
</RelativeLayout>
I was being an idiot.
My child view wrapped to parent, and the parent wrapped to the child, so it still remained the original size.
Changed the child to wrap_content on the height and it all worked.
I'm trying to create a RadioGroup within an Android layout where the child RadioButtons are stretched to evenly fill the entire width of the RadioGroup. However, I've encountered some unexpected behaviour when trying to do this with RadioButtons which have been added programmatically from code. First some background...
What does work
I started with a simple layout based on a RelativeLayout which contains a large TextView and a RadioGroup at the bottom.
The main.xml layout file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView android:text="Some text"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentTop="true"
android:layout_above="#+id/radio_group"
android:gravity="center"
android:background="#android:color/holo_green_dark"
/>
<RadioGroup android:id="#+id/radio_group"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:background="#android:color/holo_blue_dark">
<RadioButton android:text="Option 1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
android:background="#android:color/darker_gray"
android:button="#android:color/transparent"
android:padding="10dp"
android:gravity="center"
android:layout_margin="2dp"/>
<RadioButton android:text="Option 2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
android:background="#android:color/darker_gray"
android:button="#android:color/transparent"
android:padding="10dp"
android:gravity="center"
android:layout_margin="2dp"/>
</RadioGroup>
</RelativeLayout>
which produces the following layout at runtime:
You can see that the use of android:layout_width="wrap_content" and android:layout_weight="1" in both RadioButtons stretches them to evenly fill half of the enclosing RadioGroup each. So far so good.
What doesn't work
However, the requirement I have is to dynamically create RadioButtons within this layout at runtime based on business logic rather than always using the the two statically included in the layout - sometimes I might need two buttons, sometimes four etc.
To implement this I removed the RadioButtons from my main.xml layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView android:text="Some text"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentTop="true"
android:layout_above="#+id/radio_group"
android:gravity="center"
android:background="#android:color/holo_green_dark"
/>
<RadioGroup android:id="#+id/radio_group"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:background="#android:color/holo_blue_dark"/>
</RelativeLayout>
...and created a separate _radio_button.xml_ layout for my RadioButton:
<?xml version="1.0" encoding="utf-8"?>
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="#android:color/darker_gray"
android:button="#android:color/transparent"
android:gravity="center"
android:padding="10dp" />
In my activity I now add the RadioButtons programmatically:
public class TestRadioActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create an inflater to inflate our buttons
LayoutInflater inflater =
(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Create the layout params for our buttons
LinearLayout.LayoutParams layoutParams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f);
RadioGroup group = (RadioGroup) findViewById(R.id.radio_group);
// Add button one
RadioButton button = (RadioButton) inflater.inflate(R.layout.radio_button, null);
button.setText("Option 1");
group.addView(button, layoutParams);
// Add button two
button = (RadioButton) inflater.inflate(R.layout.radio_button, null);
button.setText("Option 2");
group.addView(button, layoutParams);
}
}
Note how both the _radio_button.xml_ file and the activity specify a layout width of WRAP_CONTENT and a layout weight of 1 to evenly distribute the buttons as in the original main.xml.
However, the layout seems to get rendered ignoring the layout weight with the buttons butted up on the left of the radio group:
As has been suggested elsewhere, I also tried setting the width of the RadioButtons to 0 in the LayoutParams (apparently this can cause the layout weight to be interpreted slightly differently), but this causes the RadioButtons not even to be rendered:
Can any advise how to get RadioButtons to evenly fill the entire width of the containing RadioGroup when added programmatically? Is there anything obvious I'm missing?
When you set a layout weight, you should use fill_parent as layout width. Then you shouldn't use LinearLayout.LayoutParams but RadioGroup.LayoutParams, as you're adding radio buttons to a RadioGroup, not to a simple LinearLayout.
Finally, as you use the inflater to "build" the radio button, the XML file of the radio button already has the layout params picked from the XML file, so I think you should just call the addView method that takes only the view to add as parameter (that is addView(View v)) and change the layout_width to fill_parent.
Note that, if you'll need to reference the variable "button" in the code, i.e. add a click listener, you'll add the listener only to the last created button. You'll have to create a RadioButton object for each RadioButton you will add to the RadioGroup (button, button1, button2, etc).
FYI, to do it without any xml at all
RadioGroup rgrp = new RadioGroup(context);
rgrp.setLayoutParams(new RadioGroup.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
rgrp.setOrientation(LinearLayout.HORIZONTAL);
mAccent = new RadioButton(context);
mAccent.setText("Accent");
mAccent.setLayoutParams(new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
rgrp.addView(mAccent);
mGhost = new RadioButton(context);
mGhost.setText( "Ghost");
mGhost.setLayoutParams(new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
rgrp.addView(mGhost);
mFlam = new RadioButton(context);
mFlam.setText( "Flam");
mFlam.setLayoutParams(new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
rgrp.addView(mFlam);
layout.addView(rgrp);
I ran into this issue too, I used the RadioGroup.LayoutParams with weight defined. However I also found once I'd created programatically the buttons weren't responding to touch so set clickable and enabled to true and that fixed things.
private RadioButton createMyTypeRadioButton(MyType type){
//create using this constructor to use some of the style definitions
RadioButton radio = new RadioButton(this, null, R.style.MyRadioStyle);
RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1f);
radio.setLayoutParams(layoutParams);
radio.setGravity(Gravity.CENTER);
//tag used by the setOnCheckedChangeListener to link the radio button with mytype object
radio.setTag(type.getId());
//enforce enabled and clickable status otherwise they ignore clicks
radio.setClickable(true);
radio.setEnabled(true);
radio.setText(type.getTitle());
return radio;
}
private void updateMyTypesUi() {
//populate RadioGroup with permitted my types
for (int i = 0; i < myTypes.size(); i++) {
MyType type = myTypes.get(i);
RadioButton radioButton = createSwapTypeRadioButton(type);
myRadioGrp.addView(radioButton);
}
myRadioGrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton checkedType = (RadioButton) group.findViewById(checkedId);
String idOfMyTypeChecked = (String) checkedType.getTag();
//do something with idOfMyTypeChecked
}
});
}