androidandroid-layoutbuttonbackground-drawable

Buttons text is not aligned any more after changing the BackgroundDrawable


i am changing my buttons' backgroundDrawable programaticaly:

myButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.card_button_blue));

After changing the background, the text in the button is not aligned any more like it was before changing the background.

buttons before and after changing its background
(source: dsliga.eu)

target SDK version is 10. Thanks.


Solution

  • Looks like the setText() method messes up the alignment. After changing the text, re-applying the gravity and padding did the job:

    myButton.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
    myButton.setPadding(12, 12, 12, 12);