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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With