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();
Related
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);
I am able to control the height but somehow it does not make any difference to the button width.
// Table Row for Previous button
TableRow tableRow1 = new TableRow(this);
tableRow1.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
tableRow1.setPadding(5, 5, 5, 5);
// Button - Previous
Button btnPrev = new Button(this);
btnPrev.setText("< Previous");
btnPrev.setGravity(Gravity.CENTER);
btnPrev.setOnClickListener(prevListener);
tableRow1.addView(btnPrev);
android.view.ViewGroup.LayoutParams params = btnPrev.getLayoutParams();
params.height = 20;
params.width = 60;
btnPrev.setLayoutParams(params);
// TextView for Previous button
TextView textView11 = new TextView(this);
textView11.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
textView11.setGravity(Gravity.CENTER_VERTICAL);
textView11.setGravity(Gravity.RIGHT);
textView11.setPadding(0, 0, 20, 0);
textView11.setText("New Entry Form");
tableRow1.addView(textView11);
android.view.ViewGroup.LayoutParams params1 = textView11
.getLayoutParams();
params1.height = (int) dipHeight;
textView11.setLayoutParams(params1);
tableLayout.addView(tableRow1);
I tried changing few things here and there but no look.
Can someone from you take a time and help me on this?
// replace this code
tableRow1.addView(btnPrev);
android.view.ViewGroup.LayoutParams params = btnPrev.getLayoutParams();
params.height = 20;
params.width = 60;
btnPrev.setLayoutParams(params);
to
tableRow1.addView(btnPrev,new android.view.ViewGroup.LayoutParams(20,60));
to set button width & height use below code
btnPrev.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btnPrev.setwidth(60);
btnPrev.setheight(20);
You can set button width in two ways:
Via java :
Button.setWidth(int width);
Via XML properties :
<Button android:layout_height="wrap_content"
android:id="#+id/ButtonPrev"
android:layout_below="#id/output"
android:text="7"
android:minWidth="100dp">
</Button>
The property : android:minWidth will set the width of the button.
Reference : Set width of Button in Android
View.requestLayout() maybe what you want?
I use
appMusicTab.setButtonDrawable(R.drawable.all);
to set the RadioButton's button style,
But the drawable is always algin to the left of RadioGroup, How can I set a left padding to the 'button' in Code? Thanks!!
I've tried
appMusicTab.setPadding(20, 0, 0, 0);
And:
appMusicTab.setLeft(20);
But those do not worked.
This is my code:
RadioButton appMusicTab = new RadioButton(mContext);
appMusicTab.setText(tabInfo.name);
appMusicTab.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 25);
appMusicTab.setGravity(Gravity.CENTER);
appMusicTab.setId(tabInfo.id);
// appMusicTab.setButtonDrawable(android.R.color.transparent);
appMusicTab.setButtonDrawable(R.drawable.all);
appMusicTab.setBackgroundResource(R.drawable.app_tab_bg);
appMusicTab.setPadding(30, 0, 0, 0);
RadioGroup.LayoutParams lp =
new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.MATCH_PARENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
lp.bottomMargin =MASettings.MAIN_UI_APP_TAB_MARGIN_BOTTOM;
mAppsTab.addView(appMusicTab, lp); //mAppsTab is a RadioGroup
I used a icon with some transparent paddings to solve this problem. I checked RadioButton source code and found that the left padding of button Drawable has been set to 0 in code: buttonDrawable.setBounds(0, y, buttonDrawable.getIntrinsicWidth(), y + height);
For that you have to set the LayoutParams.
See below:
qButt = new Button(this);
qButt.setPadding(0, 0, 0, 0);
TableRow.LayoutParams params = new TableRow.LayoutParams(BtnHeight,BtnHeight); // You can set any Layout params in Place of TableRow
YourParentLayout.addView(qButt,params); // Your parent layout may be any Layout in xml
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);
I want to create multiple TextView boxes dynamically, If i enter 2 in the Edittext it wants to create 2 TextView, If i enter 5 in the EditText it wants to create 5 TextView and so on... can anyone help me...
Thanks in advance.,
first you need to get the parent layout.if it is ,say LinearLayout means,you can add the textview by using addView(view).
letterTextView = new TextView[length];
for (int Index = 0; Index < length; Index++)
{
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 0, 25, 0);
letterTextView[Index] = new TextView(FriendsPanel.this);
letterTextView[Index].setTextSize(20);
letterTextView[Index].setTextColor(Color.WHITE);
letterTextView[Index].setText("TextView"+letterIndex);
linearLayout.addView(letterTextView[Index], layoutParams);
}