4

I have a nine-patched image, which I set the background for my textview during activity.onCreate(). Textview has WRAP_CONTENT layout for both height+width. It's created from code, not from XML.

_btnCurrent.setBackgroundResource(R.drawable.button_menu_off);

This works fine. I have an other nine-patched image, same size, only difference is it's color. Now if I call setBackgroundResource later with the other image, it will increase the height of the textview, almost to double. This other call happens from the textview.onClick()

_btnCurrent.setBackgroundResource(R.drawable.button_menu_on);

If I call it again (clicking again in the textview), no effect. Which is good, and I suppose it to happen for the first click as well. So:

Activity.onCreate()
{
    btnCurrent= new TextView(this);
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    btnCurrent.setBackgroundResource(R.drawable.button_menu_hs_off);

    btnCurrent.setPadding(15, 5, 15, 5);
    btnCurrent.setGravity(Gravity.CENTER);

    btnCurrent.setText( GetString(id) );        
    btnCurrent.setOnClickListener(this);

}

onClick()
{
    _btnCurrent.setBackgroundResource(R.drawable.button_menu_on);
}


onCreate();  // -> called by Android during startup -> OK
....
onClick();   // called when user clicks on it -> btnCurrent has double height
....
onClick();   // called when user clicks again -> btnCurrent has same double height, no change in size
....
onClick();   // called when user clicks again -> btnCurrent has same double height, no change in size

I tried to set the _btnHSCurrent.setBackgroundResource(0); before setting the new image, but does not help.

Why does this happen?

@Edit

I did: _btnCurrent.setBackgroundResource(R.drawable.button_menu_off); during the Activity.onCreate().

Then in the textview.onClick(), I call:

_btnCurrent.setBackgroundResource(R.drawable.button_menu_on);

and this will double the height. Clicking again, again, again, does not change the size anymore, only at first click.

enter image description here enter image description here

Zoli
  • 841
  • 8
  • 31
  • do you mind to put here to two 9-patch images? – Emil Adz Apr 07 '13 at 11:16
  • seems your problem with the 2nd image (button_menu_on) – Mohammad Ersan Apr 07 '13 at 11:24
  • Can you show the xml part of your textview ? – Arnaldo Ignacio Gaspar Véjar Apr 07 '13 at 11:28
  • @ArnaldoGaspar: I do not have it in XML, I create the TextView from code in the onCreate().`TextView retval = new TextView(this); `LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); `retval.setBackgroundResource(R.drawable.button_menu_hs_off); `retval.setTextColor(0xff000000); `retval.setPadding(15, 5, 15, 5); `retval.setGravity(Gravity.CENTER); `retval.setText( GetString(id) ); `retval.setOnClickListener(this); – Zoli Apr 07 '13 at 11:31
  • @MoshErsan: what problem can you see with the 2nd image (white)? – Zoli Apr 07 '13 at 11:34
  • retval.setIncludeFontPadding(false) works? – Arnaldo Ignacio Gaspar Véjar Apr 07 '13 at 11:39
  • @ArnaldoGaspar: setIncludeFontPadding(false) -> no change – Zoli Apr 07 '13 at 11:45
  • Check *all* your drawables folders. You may have a differently-sized variant of *one* of the button background images in drawable-xhdpi, drawable-hdpi, drawable-nodpi, etc. – Reuben Scratton Apr 07 '13 at 11:49
  • @ReubenScratton, nope these 2 png exist only in the res/drawable. – Zoli Apr 07 '13 at 11:51
  • What happens if you swap the images around? Does the button start big and then get small when clicked? – Reuben Scratton Apr 07 '13 at 11:52
  • @ReubenScratton: same behavior with swapped images – Zoli Apr 07 '13 at 11:58
  • I FOUND IT, FINALLY :) The problem is, calling `setBackgroundResource()` again seems to erase the padding. During the creation of the textview, I call `setPadding(15, 5, 15, 5)` after the `setBackgroundResource()`. If I call `setPadding()` after the second `setBackgroundResource()` in the `onClick()` (just like during the creation of the textview), it's fine again. Works, no height change. THANKS all your hints, helps. – Zoli Apr 07 '13 at 12:08
  • http://stackoverflow.com/questions/2886140/does-changing-the-background-also-change-the-padding-of-a-linearlayout Consider this – Zaartha May 16 '15 at 11:09

1 Answers1

7

This looks like a similar issue I had with padding. After calling setBackgroundResource the padding seems to change, not using the values I configured in styles.xml. The effect looks the same as if the size has changed, but in fact it is the padding.

My workaround is to call setPadding on the button after changing the background, for example:

button.setBackgroundResource(R.drawable.btn_add);
button.setPadding(10, 15, 10, 15);

It's not great but it works.

I hope this helps!

janos
  • 120,954
  • 29
  • 226
  • 236
  • I just found the same solution, see my last comment under the question. Yes, this was the problem, `setBackgroundResource` ruins the padding. Marking this as the solution – Zoli Apr 07 '13 at 12:05
  • But you're using ninepatches, so the padding should be specified as part of the image. You should only specify padding in XML or via setPadding() when *not* using a ninepatch... – Reuben Scratton Apr 07 '13 at 12:39
  • @ReubenScratton: That's also something to consider...however being lazy it is simpler to play with `setPadding()` than editing images – Zoli Apr 07 '13 at 12:46
  • @ReubenScratton the padding inherent in the nine-patch is one thing, the padding you can add around your *content* is another. Ideally the nine-patch should have minimal padding and let the user control the padding in xml or Java code. – janos Apr 07 '13 at 13:20
  • That's crazy, must be a bug, it's hardly intentional. Anyway, I had the same problem but it was when I set the background resource for a `RelativeLayout`, which is weird because I thought those things couldn't have any padding. In any case, calling `setPadding(0,0,0,0)` after setting the resource fixed it! – RTF Mar 23 '16 at 16:47