Align RadioButtons in RadioGroup (created by code) in android - android

In my app I need to create a RadioGroup with 2 RadioButtons by code. I need to align the RadioButtons inside the RadioGroup, one to the left, and the other to the right part of RadioGruop. I know that if I add the radio-buttons to a LinearLayout these will solve this problem but in this case the property of RadioGroup, that only one RadioButton is checked at a time is not anymore available.
Here is my code :
TableRow.LayoutParams lp_radio1 = new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams lp_radio2 = new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams lp_radiogr = new TableRow.LayoutParams(
width, height);
lp_radio1.setMargins(left, left, top2, 0);
lp_radio1.gravity=Gravity.LEFT;
lp_radio1.weight=1;
lp_radio2.setMargins(left, left, top2, 0);
lp_radio2.gravity=Gravity.RIGHT;
lp_radio2.weight=1;
product_radiogroup = new RadioGroup(viewToLoad.getContext());
product_radiogroup.setLayoutParams(lp_radiogr); product_radiogroup.setOrientation(RadioGroup.HORIZONTAL); product_radiogroup.setBackgroundResource(R.drawable.radio_group_background);
product_radiobuttonYES = new RadioButton(viewToLoad.getContext());
product_radiobuttonYES.setLayoutParams(lp_radio1);
product_radiobuttonYES.setTextColor(R.color.medium_gray);
product_radiobuttonNO = new RadioButton(viewToLoad.getContext());
product_radiobuttonNO.setLayoutParams(lp_radio2);
product_radiobuttonNO.setTextColor(R.color.medium_gray);
product_radiogroup.addView(product_radiobuttonYES);
product_radiogroup.addView(product_radiobuttonNO);
and here is my result :
Has anyone any idea how to solve this ? Thanks in advance.

i don't think this solution is a nice one, but since you know the exact width and height of your radiogroup, just set the width of your radiobutton to width/2 instead of wrap_content and other attributes, such as weight and gravity on lp_radio1 and lp_radio2 can be removed.

Related

Not able to add margins on buttons through code

Here is my code, I am trying to add few buttons in a row. Its working fine, But these buttons appear too close to each other, I want to apply horizontal margins to buttons. But not able to add. To achieve this I tried to keep button inside a Linearlayout and applied margins to it. But it somehow don't show any buttons, And when I comment out this line- ll.setLayoutParams(lp); Buttons can be seen again, But without any margin. Please let me know how can I make buttons at some distance from each other.
maintable = (TableLayout) findViewById(R.id.maintable);
TableRow tr = new TableRow(this);
for (int z = 0; z < 3; z++) {
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(5, 5, 5, 5);
LinearLayout ll = new LinearLayout(this);
//ll.setLayoutParams(lp);
Button b = new Button(savedLists.this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(150, 150);
params.setMargins(0, 20, 0, 20);
b.setBackgroundResource(R.drawable.circular);
b.setText(Integer.toString(c));
b.setPadding(10,10,10,10);
ll.addView(b,params);
tr.addView(ll);
c++;
}
maintable.addView(tr);
R.drawable.circular is just creating a simple circular button. Please let me know, if I should post that too.
You haven't set any margin for the button here. You are setting it's padding! Here is how you can set the margin for a button!
Button b = new Button(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);
params.setMargins(top, left, bottom, right);
yourLinearLayout.addView(b,params);

Android: Height of table cell should get height of previous cell

I'm creating a TableLayout programmatically. A table row can consist of an amount + unit (cell 1), and ingredient (cell 2) and a delete button (cell 3).
The ingredients can be longer than the available width, so I used the weight attribute and set it to 1 to enable a line break:
setLayoutParams(new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 1f));
This works.
The problem is that the delete button prevents the table row to increase the height, so it is partly hidden which looks like this:
This is the important part of the code that produces one table row:
final TableRow tableRow = new TableRow(getApplicationContext());
tableRow.setTag(INGREDIENT_ENTRY);
tableRow.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
// Amount and unit
int dp6InPixel = PixelCalculator.convertDpToPixel(getApplicationContext(), 6);
TextView tvAmountAndUnitText = new TextView(getApplicationContext());
tvAmountAndUnitText.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
tvAmountAndUnitText.setText(strAmount + " " + strUnit);
tvAmountAndUnitText.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
tvAmountAndUnitText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16f);
tvAmountAndUnitText.setGravity(Gravity.RIGHT);
tvAmountAndUnitText.setTypeface(tvAmountAndUnitText.getTypeface(), Typeface.BOLD);
tvAmountAndUnitText.setPadding(dp6InPixel, 0, dp6InPixel, 0);
tableRow.addView(tvAmountAndUnitText);
// Ingredient
TextView tvIngredientText = new TextView(getApplicationContext());
tvIngredientText.setLayoutParams(new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 1f));
tvIngredientText.setText(strIngredient);
tvIngredientText.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
tvIngredientText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16f);
tableRow.addView(tvIngredientText);
// Button
int dp10InPixel = PixelCalculator.convertDpToPixel(getApplicationContext(), 10);
TextView tvIngredientDeleteButton = new TextView(getApplicationContext());
LayoutParams buttonParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
buttonParams.setMargins(dp10InPixel, 0, 0, 0);
tvIngredientDeleteButton.setLayoutParams(buttonParams);
tvIngredientDeleteButton.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
tvIngredientDeleteButton.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.lightred));
tvIngredientDeleteButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20f);
tvIngredientDeleteButton.setPadding(dp6InPixel, dp6InPixel, dp6InPixel, dp6InPixel);
tvIngredientDeleteButton.setText("x");
//more code
tableRow.addView(tvIngredientDeleteButton);
ingredientTable.addView(tableRow);
When I set tvIngredientDeleteButton.setMinLines(2);, then I can see the full ingredient cell. Unfortunately all rows have a min height of 2 then which looks ugly. I need some way to recognize if the ingredient cell has a line break and set minLines for that case or any other good solution (but I will not count ingredient characters or something. I guess this can be solved with some table attributes or similar). Any ideas how to solve this problem?
TableRow is direct subclass of LinearLayout. And inorder to set how child views are positioned in it, you need to define it's gravity. Default value is TOP, and I've tried with FILL, CENTER_VERTICAL etc. So any value of setGravity() other than TOP will render TextView with full content. Please refer official document, for more detail.
So you just need to add one statement at a time of declaration of TableRow to achieve your requirement.
tableRow.setGravity(Gravity.FILL);

Add Buttons to Relative Layout Dynamically or Extended Linear Layout android

I want to add buttons to relative layout dynamically as per button and screen width as shown in image. Number of buttons is not fixed and width of button depends on the text which is set to the button.
I tried to achieve this using below code but that to not working fine as per my requirement.
Can anyone please help me ? Please help me through this.
RelativeLayout layout = (RelativeLayout) findViewById(R.id.genre_layout);
for(int i=0; i < genreList.size(); i++){
Button btn = new Button(MovieDetailsActivity.this);
btn.setText(genreList.get(i).getGenreName());
btn.setPadding(15, 5, 15, 5);
btn.setBackgroundColor(Color.parseColor("#FFFFFF"));
btn.setBackgroundColor(Color.parseColor("#80333333"));
LayoutParams param = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
param.setMargins(5, 5, 5, 5);
if (i != 0){
int prevId = genreList.get(i).getGenreId();
param.addRule(RelativeLayout.RIGHT_OF, prevId);
}
btn.setLayoutParams(param);
btn.setId(genreList.get(i).getGenreId());
layout.addView(btn);
}
Try this
Display display=getWindowManager().getDefaultDisplay();
int width=display.getWidth();
btn.setWidth(width);
or if you have two buttons, do
Display display=getWindowManager().getDefaultDisplay();
int width=display.getWidth();
btn1.setWidth(width/2);
btn2.seTwidth(width/2);
Update
LinearLayout.LayoutParams paramz = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
0dp, 1.0f);
then btn.setLayoutParams(paramz);
I have the code to add buttons dynamically to linear layout. Just check out whether it helps you https://docs.google.com/document/d/1QA1tAIe601fZT2Dlp1A0qDuLD0U4IqKL3f_Crx1rtkE/edit?usp=sharing
Try this for adding dynamic buttons in relativelayout
RelativeLayout layout = (RelativeLayout)findViewById(R.id.slidingDrawerContent);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT );
for (int i=0; i<4; i++) {
Button btn = new Button(this);
btn.setId(i);
btn.setText("some_text");
// lp.addRule(RelativeLayout.RIGHT_OF, <Id>);
layout.addView(tv2, lp);
}
I would like to suggest use LinearLayout as you asked in your question and give a Weight.
LinearLayout
I Solved my problem by using FlowLayout.
Sample code and references are available here
Its simple to use. Thank you all for help.

How do I get the `layout_weight` of a TextView?

I have a TableLayout and a row with a couple TextViews. I want to find the layout_weight of the TextView but I cant figure out how. I've tried the following:
TableRow.LayoutParams lp = tv1.getLayoutParams();
and:
ViewGroup.LayoutParams lp = tv1.getLayoutParams();
In the first case I get a type mismatch: "Cannot convert from ViewGroup.LayoutParams to TableRow.LayoutParams", and the second case works fine, however ViewGroup.LayoutParams doesn't have a weight field.
All the different types of LayoutParams are confusing, I'm never sure which to use where.
Try this
TextView text = new TextView(v.getContext());
text.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
Try this link
You almost had it with your first try. You just need to cast to the correct type:
TableRow.LayoutParams lp = (TableRow.LayoutParams) tv1.getLayoutParams();
Any subclass of LinearLayout.LayoutParams (including TableRow.LayoutParams) will have a weight field.

android position TextViews programmatically

how do I position two programmatically created TextViews in a LinearLayout BESIDE each other? I tried the code below, but that way the "number" TextView is placed one line deeper compared to the "value" TextView, so the height of nlap LinearLayout changes. I need both TextViews to be at the same height, the "number" TextView should be on the left side an centered vertically. Any help is appreciated.
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(10, 1, 10, 1);
LinearLayout nlap = new LinearLayout(this);
nlap.setOrientation(LinearLayout.VERTICAL);
nlap.setLayoutParams(layoutParams);
TextView value = new TextView(this);
value.setText("Test");
value.setTextColor(Color.parseColor("#A60101"));
value.setTextSize(23);
value.setGravity(Gravity.CENTER);
value.setTypeface(font);
TextView number = new TextView(this);
number.setTextColor(Color.parseColor("#FFFFFF"));
number.setText("01");
nlap.addView(value);
nlap.addView(number);
You should use nlap.setOrientation(LinearLayout.HORIZONTAL); to achieve having textViews beside each other, if you use VERTICAL as you do, second one will always be below the first.

Categories

Resources