Reputation: 11093
I need to add a border to my button when it's clicked, and i need the background from the button (which i get remotely, so i cant use R.drawable.picture
etc) to remain the way it is.
In short, when clicked, i want to add a border to my (custom backgrounded) button.
How do i do this? I've read many many pages and questions on borders, buttons, shapes, selectors, etc, but the majority of them involve xml, but because i don't have the background from the buttons before starting the applications, I think i'm bound to programmatically adding and removing the border. Again, i need to retain the custom button background, but add a small onclick border.
NOTE: I use this on all buttons, with different sizes. So applying a drawable is not a real option. I think i'm bound to some sort of drawing a rectangle around the clicked button.
NOTE2: again, I have a background which must be applied. the border is merely a small indicator for onclick events. So i cant put in the XML that the background is some sort of selector
Upvotes: 3
Views: 18051
Reputation: 21201
Btn.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<solid
android:color="#343434" />
<stroke
android:width="1dp"
android:color="#171717" />
<corners
android:radius="3dp" />
<padding
android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#343434"
android:endColor="#275296"
android:angle="270" />
<stroke
android:width="0dp"
android:color="#171717" />
<corners
android:radius="4dp" />
<padding
android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp" />
</shape>
</item>
</selector>
Here stroke attribute will appear as border to the button when clicked.
save this in drawable folder. and use as like below
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/btn"
android:textColor="@color/white"
android:text="Button"/>
Upvotes: 5
Reputation: 48272
You can define layer_list in xml with 2 layers: one for your image, the other one for your border and use this layer list as a drawable for your button's pressed state
Upvotes: 0
Reputation: 5278
You can try having a 9.png
with just a black border around a transparent area. This you could apply in the "pressed" state of the button, using a selector.
Upvotes: 0