Reputation: 3814
I was wondering if is possible to change the compound drawables of a button (inherited from its TextView) when it is pressed. I have an style file in which I define the button style of default, pressed, and disabled state, but I do have compound drawables in that button and I don't know how to change them depending on the button state. Is there any way to achieve this?
Thank you so much,
Upvotes: 0
Views: 888
Reputation: 12711
You can use a StateListDrawable to make it so that your different compound drawables are associated with the pressed and normal states. The docs for them are here.
Upvotes: 1
Reputation: 5295
You cannot do them in xml. However, you can do them programmatically. You just have to put this code in click listener or something and apply if conditions for example whether the button is pressed or clicked. There are pre defined boolean methods for that.
Here is the code to change compund drawable :-
You can use the setCompoundDrawable method to do this. I used this without using the setBounds and it worked.
Drawable image = getContext().getResources().getDrawable( R.drawable.ic_launcher );
img.setBounds( 0, 0, 60, 60 );
txtVw.setCompoundDrawables( img, null, null, null );
or, you can use :-
Drawable image = getContext().getResources().getDrawable( R.drawable.ic_launcher );
image.setBounds( 0, 20, 50, 50 );
or
txtview1.setCompoundDrawables( imageg, null, null, null );
Upvotes: 1