John T
John T

Reputation: 1082

setPadding() on Button programmatically not working

When I set padding on a button using XML it works without problems.

XML for the button:

<Button
    android:id="@+id/comment_video"
    android:layout_below="@+id/input_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/comment_video"
    android:textColor="@android:color/white"
    android:backgroundTint="@color/colorAccent"
    android:layout_alignParentEnd="true"
    android:textSize="14sp"
    android:padding="10dp"
    android:layout_marginTop="0dp"
    android:layout_marginEnd="10dp"
    android:visibility="gone"/>

Screenshot of button:

enter image description here

Then when I do it programmatically it's not working.

Code for the button:

// Add reply button
final Button replyButton = new Button(mContext);
replyButton.setId(170000 + i2);
replyButton.setText("REPLY");
replyButton.setTextColor(Color.parseColor("#FFFFFF"));
replyButton.setBackgroundColor(Color.parseColor("#C70000"));
replyButton.setPadding(dp10_in_px, dp10_in_px, dp10_in_px, dp10_in_px);
replyButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
replyButton.setVisibility(View.GONE);

RelativeLayout.LayoutParams lp8 = new RelativeLayout.LayoutParams(
        ViewGroup.LayoutParams.WRAP_CONTENT,
        ViewGroup.LayoutParams.WRAP_CONTENT);
lp8.addRule(RelativeLayout.BELOW, textInputLayout.getId());
lp8.addRule(RelativeLayout.ALIGN_PARENT_END, relativeLayout3.getId());
lp8.setMargins(0, dp4_in_px, dp10_in_px, dp10_in_px);
replyButton.setLayoutParams(lp8);

Screenshot of button:

enter image description here

I set thousands of views dynamically based on YouTube data and must do it programmatically, at least as far as I know.

The formula used for variables in setPadding and setMargin are as follows:

int dp4 = 4;  // 4 dps
final float scale = getResources().getDisplayMetrics().density;
int dp4_in_px = (int) (dp4 * scale + 0.5f);
int dp10 = 10;  // 10 dps
int dp10_in_px = (int) (dp10 * scale + 0.5f);

Any help is appreciated.

Update 1 - Screenshot of layout bounds

enter image description here

Update 2 - Sharp corners

Another small detail that might be a clue. The corners on the programmatically made button is sharp. Normally they're a bit rounded.

Upvotes: 2

Views: 6739

Answers (2)

Android Geek
Android Geek

Reputation: 9225

Try this:

replyButton.setPadding(dp10_in_px, dp10_in_px, dp10_in_px, dp10_in_px);

you should replace line something like this

 replyButton.setPadding(0,padding,0,0);

Upvotes: 0

John T
John T

Reputation: 1082

OK, with help from war_Hero in comments I've found the solution.

setBackgroundColor() don't work. It sets the entire background, including the padding, to the same color.

This:

replyButton.setBackgroundColor(Color.parseColor("#C70000"));

Should be replaced with something like this:

replyButton.getBackground().setColorFilter(ContextCompat.getColor(mContext, R.color.colorAccent), PorterDuff.Mode.MULTIPLY);

Upvotes: 2

Related Questions