I'm trying to set a margin to RadioButtons added programmatically to a RadioGroup, but it fails.: the RadioButtons are correctly added, but they have 0 margin...
Anybody can help?
layout
<RadioGroup android:id="#+id/rg_nav" android:orientation="vertical"
android:layout_width="wrap_content" android:layout_height="wrap_content">
</RadioGroup>
activity
float density = getResources().getDisplayMetrics().density;
rg_nav = (RadioGroup) findViewById(R.id.rg_nav);
LinearLayout.LayoutParams params_rb = new LinearLayout.LayoutParams(
(int)(8*density),
(int)(8*density));
int margin = (int)(6*density);
params_rb.setMargins(margin, margin, margin, margin);
for(String url : product.list_url_pic){
RadioButton radio_btn = new RadioButton(ProductHome.this);
radio_btn.setButtonDrawable(R.drawable.rb_nav);
radio_btn.setId(rb_id++);
rg_nav.addView(radio_btn, params_rb);
}
I had to replace
LinearLayout.LayoutParams params_rb = new LinearLayout.LayoutParams(
(int)(8*density),
(int)(8*density));
by
RadioGroup.LayoutParams params_rb = new RadioGroup.LayoutParams(
(int)(8*density),
(int)(8*density));
Try this:
TableRow.LayoutParams rg_params = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 3f);
RadioGroup radio_group=new RadioGroup(this);
radio_group.setOrientation(RadioGroup.HORIZONTAL);
radio_group.setLayoutParams(rg_params);
RadioButton dry=new RadioButton(this);
RadioGroup.LayoutParams params_soiled = new RadioGroup.LayoutParams(getBaseContext(), null);
params_soiled.setMargins(10, 0, 10, 0);
dry.setLayoutParams(button_params);
Your code seems to be correct.
Try applying LayoutParams directly to your buttons
radio_btn.setButtonDrawable(R.drawable.rb_nav);
radio_btn.setId(rb_id++);
rg_nav.addView(radio_btn);
radio_btn.setLayoutParams(params_rb);
Related
I am setting up a RadioGroup with RadioButtons, and they are all populated dynamically based on data in Firebase. I set the parameters as follows:
mParams = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
mParams.setMargins(0,0,4,6);
I then instantiate my RadioButton objects:
public void addRadioButtonsWithFirebaseAnswers(List<DocumentSnapshot> answers) {
mPollAnswerArrayList = new ArrayList<RadioButton>();
int indexCreated = 0;
for (DocumentSnapshot answerSnapshot : answers) {
Answer answer = answerSnapshot.toObject(Answer.class);
mPollAnswerArrayList.add((indexCreated), new RadioButton(mContext));
RadioButton radioButton = mPollAnswerArrayList.get(indexCreated);
radioButton.setTag(indexCreated);
radioButton.setText(answer.getAnswer().toString());
radioButton.setTextColor(getResources().getColor(R.color.black));
radioButton.setButtonDrawable(R.drawable.custom_btn_radio);
//TODO: Investigate if this line is necessary
radioButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.radio_button_answer_text_size));
mPollQuestionRadioGroup.addView(radioButton, mParams);
indexCreated++;
}
}
Below is how it is appearing. I want to add space between the radio button and the text. I also want space between each button. Finally, I would like the text centered with the button if possible.
this is an easy for how you can add a space between the button and text:
radioButton.setText(" " + answer.getAnswer().toString());
To change the distance between buttons, you can do:
LayoutParams params = new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
);
params.setMargins(leftMargin, topMargin, rightMargin, 15);
radioButton.setLayoutParams(params);
The 15 is the bottomMargin which you can change as you wish. I hope this helps.
Try this.
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(10, 10, 10, 10); // leftMargin, topMargin, rightMargin, buttomMargin
RadioGroup radioGroup = new RadioGroup(getContext());
RadioButton radioButton = new RadioButton(getContext());
radioButton.setLayoutParams(params1);
radioButton.setId(1);
radioButton.setText("text");
radioButton.setPadding(0, 5, 0, 5); // leftMargin, topMargin, rightMargin, buttomMargin
radioGroup.addView(radioButton);
use this for check/select the radio button,
radioGroup.check(3);
use this for unchecked the radio button,
radioGroup.clearCheck();
I'm having a linear layout in which I want to insert a checkbox on left and a button on its right.
Also, it should be in for loop so as to create many number of such combination in that single linear layout.
final LinearLayout LayoutRight = (LinearLayout) findViewById(R.id.linearlayoutbars_register);
LayoutRight.setOrientation(LinearLayout.VERTICAL);
LayoutRight.setWeightSum(1f);
final LinearLayout.LayoutParams pR1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.MATCH_PARENT);
pR1.weight = 0.3f;
final LinearLayout.LayoutParams pR2 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.MATCH_PARENT);
pR2.weight = 0.7f;
for (int i = l; i <= lastCounter; i++) {
tick = new CheckBox(RegisterActivity.this);
tick.setId(10000 + l);
tick.setOnClickListener(tick_Click);
LayoutRight.addView(tick, pR1);
pdR = new Button(RegisterActivity.this);
pdR.setText(l + ". ");
pdR.setId(l);
pdR.setWidth(800);
pdR.setTextSize(17);
pdR.setGravity(Gravity.START);
pdR.setOnClickListener(pdRclass);
LayoutRight.addView(pdR, pR2);
l++;
}
I'm setting 1f as the setWeightSum; 0.3f and 0.7f and childs weight.
With this code I'm getting output as this
But I want my output exactly like this.
You need to set Layout orientation Like this LayoutRight.setOrientation(LinearLayout.HORIZONTAL);
not VERTICAL
LayoutRight.setOrientation(LinearLayout.HORIZONTAL)?
I'm not cool enough to comment so...
Android weight doc says the effected attribute needs to be set to 0dp so try this.
final LinearLayout.LayoutParams pR1 = new LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.WRAP_CONTENT);
pR1.weight = 0.3f;
final LinearLayout.LayoutParams pR2 = new LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.WRAP_CONTENT);
pR2.weight = 0.7f;
don't forget to keep your orientation horizontal!
I want to add three linear layouts to an activity programatically each of same width. the problem is i am not able to set the weights of these layouts programmatically. I could do this within xml, but I want to do this in program.
here is what I want:
Here its the solution
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, 100);
lp.weight = 1;
See Full Solution
LinearLayout ll1, ll2, ll3;
/* Find these LinearLayout by ID
i.e ll1=(LinearLayout)findViewById(R.id.ll1);
*/
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, 100);
lp.weight = 1;
ll1.setLayoutParams(lp);
ll2.setLayoutParams(lp);
ll3.setLayoutParams(lp);
Use new LinearLayout.LayoutParams(int width, int height, float weight) to set weights when setting layout params to the subviews
Do this way..
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtNote = (LinedEditText) findViewById(R.id.txtNote);
lnr = (LinearLayout) findViewById(R.id.lnr);
LinearLayout l1 = new LinearLayout(this);
LinearLayout l2 = new LinearLayout(this);
LinearLayout l3 = new LinearLayout(this);
l1.setBackgroundResource(android.R.color.holo_green_light);
l2.setBackgroundResource(android.R.color.holo_orange_dark);
l3.setBackgroundResource(android.R.color.holo_blue_bright);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT, 1);
lnr.addView(l1, param);
lnr.addView(l2, param);
lnr.addView(l3, param);
}
You can do it by setting layout weight property for your individual linear layouts, pass it in LinearLayout - LayoutParams constructor:
LinearLayout.LayoutParams param = new LinearLayout.LayoutParam(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1);
or
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
0,
LayoutParams.MATCH_PARENT, 1);
Hope it may help you !
Every time I set the layout param of TextView to wrap content or fill parent it's not showing in the view, but when I set it to pixels it's working why is that?
Here is the relevant code:
items = (LinearLayout) findViewById(R.id.llmain);
tb = new TableLayout(MainActivity.this);
tb.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tb.setGravity(Gravity.CENTER);
TableRow tr = new TableRow(MainActivity.this);
ImageButton ima = new ImageButton(MainActivity.this);
ImageButton imr = new ImageButton(MainActivity.this);
TextView tvin = new TextView(this);
TableRow.LayoutParams trp = new TableRow.LayoutParams();
trp.setMargins(0, 0, 0, 10);
tr.setLayoutParams(trp);
tr.setGravity(Gravity.CENTER);
ima.setBackgroundColor(Color.TRANSPARENT);
ima.setImageResource(R.drawable.add);
imr.setBackgroundColor(Color.TRANSPARENT);
imr.setImageResource(R.drawable.remove);
tvin.setWidth(100);
tvin.setHeight(45);
tvin.setGravity(Gravity.CENTER);
tvin.setText(sil[i]);
tvin.setBackgroundColor(Color.rgb(200, 0, 255));
tr.addView(ima);
tr.addView(tvin);
tr.addView(imr);
tb.addView(tr);
items.addView(tb);
I tried using this
tvin.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
and this
tvin.setWidth(LayoutParams.FILL_PARENT);
tvin.setHeight(LayoutParams.FILL_PARENT);
but none of these work.
Try:
tvin.setLayoutParams(new TableRow.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
and remove the tvin.setWidth and height.
You should always set the layout params of view's parent, TableRow in this case.
MATCH_PARENT makes no difference but it's a new name and you should be using it instead of FILL_PARENT.
In this Line u have to use height and width .. TableRow.LayoutParams trp = new TableRow.LayoutParams(); Like this
TableRow row = new TableRow(context);
row.setLayoutParams(new android.widget.TableRow.LayoutParams(android.widget.TableRow.LayoutParams.FILL_PARENT,
android.widget.TableRow.LayoutParams.WRAP_CONTENT));
Try to use the LinearLayout.LayoutParams
TextView tvin = new TextView(this);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
tvin.setLayoutParams(llp);
I want that button(child) will have 25% of full width of horizontal LinearLayout (its parent). The code:
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
ll.setWeightSum (1.0f);
Button b = new Button (this);
ll.addView(b);
How can I do it? Thanks.
Try this:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENTS,LayoutParams.FILL_PARENT));
lp.weight = 0.25;
b.setLayoutParams(lp);
You can pass it in as part of the LinearLayout.LayoutParams constructor:
LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1.0f);