android, How to set left padding of Radiobutton's button programmatically? - android

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

Related

Radio Button Set Margins and Alignment Programmatically

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();

AddRule of RelativeLayout is not working [duplicate]

I am trying to create a textview and a button programmatically in the existing relative layout. The idea is to put the textview in the left corner of the parentView(relativeLayout) and add then the button to the right of the textView. But in the app it looks like they are on the one place. The button is in front of textView, not on the right. Please, give me some advice.
the code:
TextView textView = new TextView(getActivity().getApplicationContext());
textView.setText("...");
textView.setTextColor(Color.GRAY);
int id = 0;
textView.setId(id);
final RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params.setMargins(16, 16, 0, 0);
textView.setLayoutParams(params);
notificationContentLayout.addView(textView, params);
Button customerButton = new Button(getActivity().getApplicationContext());
customerButton.setText("...");
customerButton.setTextColor(Color.parseColor("#00b3ff"));
customerButton.setBackgroundColor(Color.TRANSPARENT);
id = id + 1;
customerButton.setId(id);
final RelativeLayout.LayoutParams paramsForButton =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
paramsForButton.addRule(RelativeLayout.RIGHT_OF, textView.getId());
paramsForButton.addRule(RelativeLayout.ALIGN_PARENT_TOP); // with or without that rule everything is the same
// paramsForButton.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
paramsForButton.setMargins(10,0, 16, 0);
customerButton.setLayoutParams(paramsForButton);
notificationContentLayout.addView(customerButton, paramsForButton);
For RelativeLayout.LayoutParams rules, 0 means false, and only applies to rules that don't refer to sibling Views, such as CENTER_IN_PARENT. Since you've set your TextView's ID to 0, the RIGHT_OF rule you're adding is being ignored, as false doesn't make sense with that.
To remedy this, simply set the TextView's ID to any positive int value; e.g., 1.

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);

setmargin does not work in relative layout in android

I have created 2 buttons in a relative layout programmatically. I want no margin space between the buttons and tried to achieve this by using 'setmargin' but failed. Below is the code.
//creating the relative layout
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
//creating buttons
Button button1 = new Button(this);
button1.setId(R.id.button1);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
SCR_W/2,
RelativeLayout.LayoutParams.WRAP_CONTENT);//SCR_W is the device screenwidth
params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params1.setMargins(0, 0, 0, 0);
button1.setLayoutParams(params1);
relativeLayout.addView(button1);
Button button2 = new Button(this);
button2.setId(R.id.button2);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
SCR_W/2,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params2.addRule(RelativeLayout.BELOW, button1.getId());
params2.setMargins(0, 0, 0, 0);
button2.setLayoutParams(params2);
relativeLayout.addView(button2);
//setting the relative layout as the contentview
setContentView(relativeLayout, rlp);
Thanks in advance..
I tried your code, and works ok to me,
The two buttons are at distance 0. To check it, simply set different color to each button, and you will see that they are together.
button1.setBackgroundColor(Color.BLUE);
button2.setBackgroundColor(Color.RED);
Try setting the height of the relative layout to wrap_content..
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Just try something like this, it will work -:
params1.setMargins(0, 0, 0, -5);
params2.setMargins(0, -5, 0, 0);

How to add margin to a Button at run time?

I have to add more than one buttons on a LinearLayout. But I want to put those buttons say 5px apart. I could not find a way to set a margin of a Button. Any idea?
Use the layout_margin property of the button element. Does that not work?
<Button android:layout_margin="5px" (...)/>
Edit
When creating a button in java, LayoutParams is used to specify margins and so.
Button button = new Button();
(....)
params = new LinearLayout.LayoutParams();
params.leftMargin = 5;
(set params as you need)
parent.addView(button, params);
The LayoutParams you use must match the Layout you add your button in (see http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html). For a LinearLayout, use a LinearLayout.LayoutParams and set the *Margin fields accordingly.
Try this:
//Assuming your button is in a LinearLayout as stated
LinearLayout.LayoutParams params = myButton.getLayoutParams();
params.setMargins(0, 0, 0, 0) //left, top, right, bottom
myButton.setLayoutParams(params);
MarginLayoutParams params = (MarginLayoutParams) vector8.getLayoutParams();
params.width = 200; params.leftMargin = 100; params.topMargin = 200;
vector8.setLayoutParams(params);
Need use type of: MarginLayoutParams
Adding padding to the buttons would also solve your problem. Check this out
http://developer.android.com/resources/tutorials/views/hello-formstuff.html#CustomButton
use this code to set margin at runtime
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) btnContainer.getLayoutParams();
params.setMargins(5, 50, 10, 0); //left, top, right, bottom
view_whwre_set_margin.setLayoutParams(params);

Categories

Resources