Change Gridview custom selector size - android

I have a custom selector for my gridview that appears on top of my gridview images when pressed. The issue is I don't know how to make the selector smaller. I currently use a rectangular shape, I dont know how to make this shape slightly smaller on each side.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:constantSize="true">
<item android:state_pressed="true"><shape android:shape="rectangle">
<solid android:color="#color/dark_red_title_transparent" />
</shape></item>
</selector>

You can use 9-patch drawable with transparent border as your selector.

I found the answer basically use layer-list to do something like the following that basically makes the selector slightly smaller.
<item android:state_pressed="true"><layer-list>
<item android:left="12dip" android:right="12dip" android:top="12dp" android:bottom="12dp"><shape android:shape="rectangle">
<solid android:color="#android:color/white" />
</shape></item>
</layer-list></item>

Related

Make button with drawable as background look "pressed" on Android?

Good evening everyone!
Is there a way to make my buttons look pressed without having to design two times each button?(one for pressed state, another for not pressed). I've designed the buttons' shapes using http://angrytools.com/android/button/, and using these shapes as the background for the buttons. I've heard about ImageButton but it doesnt seem to apply here as I need the buttons to have text.
Example of one of my background shapes:
Thanks very much in advance!
Try This Code, set this XML file as background to a button.
but_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="#9df20901" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#892379" />
</shape>
</item>
</selector>

How to make custom ripple borderless

Here is my custom ripple
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight">
<item android:drawable="#color/white" />
</ripple>
How do I make it borderless?
As per the RippleDrawable documentation, a ripple will be masked against the composite of its child layers.
In this case, your drawable will be masked by the only item in your ripple.
To have a ripple with no mask, you can define your ripple like so:
<ripple android:color="?android:colorControlHighlight" />
I had the same problem, while I stumbled upon this question. I have solved my problem easily and there is a code
Btw it works only for cases when height equals width. Else it will be not ?attr/selectableItemBackgroundBorderless, but ripple oval
<?xml version="1.0" encoding="utf-8"?>
<ripple android:color="#color/grayPrimary" xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/mask">
<shape android:shape="oval">
<solid android:color="#000000"/>
</shape>
</item>
</ripple>

Android ShapeDrawable stroke is blurred

I created a rather simple shapedrawable with semi-transparent borders, and used it as a background for two adjacent view.
Either if the srtoke color is partially transparent, I'm expecting a solid stroke (like the image on the right), but what I get is a blurred stroke (image on the left).
What I'm doing wrong?
(images are taken from the ADT preview, in the emulator the effect is even more visible)
This is the ShapeDrawable (theme_base_bg_framed.xml):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#color/base_frame_solid_color"
/>
<stroke
android:width="#dimen/frame_border_size"
android:color="#color/base_frame_border_color"
/>
<padding
android:left="#dimen/frame_padding_size"
android:top="#dimen/frame_padding_size"
android:right="#dimen/frame_padding_size"
android:bottom="#dimen/frame_padding_size"
/>
</shape>
It uses these color and dimen definitions:
<color name="base_frame_solid_color">#20ffffff</color>
<color name="base_frame_border_color">#40ffffff</color>
<dimen name="frame_border_size">2dp</dimen>
<dimen name="frame_padding_size">2dp</dimen>
Both drawables are assigned to the Views background
<style name="ViewWithBorder">
<item name="android:background">#drawable/theme_base_bg_framed</item>
</style>
Edit:
The colors used in the ShapeDrawable are alphaed for a reason. The view in the background is going to contain other views and/or images. This is a better example of what I get (left) and what I'm expecting to get (right).
I use this values to get the result you wanted:
colors.xml:
<color name="base_frame_solid_color">#20000000</color>
<color name="base_frame_border_color">#40ffffff</color>
shape:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#color/base_frame_solid_color"
/>
<stroke
android:width="#dimen/frame_border_size"
android:color="#color/base_frame_border_color"
/>
</shape>
Hope this helps!
Ok, I found what's happening and how to fix it.
It seems that Android, when filling a ShapeDrawable with a border, doesn't fill it to it's full size but just to the middle of the stroke. So, with a stroke of 2dp, it leave a space of 1dp all around. This can be noticed only when using alphaed borders, like I'm doing, because normally the border cover it.
Tho fix this behaviour, I used a LayerList drawable containing two ShapeDrawables, one for the solid color (with no borders) and one for the stroke (with no solid color):
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="rectangle">
<solid
android:color="#color/base_frame_solid_color"
/>
</shape>
</item>
<item>
<shape
android:shape="rectangle">
<stroke
android:width="#dimen/frame_border_size"
android:color="#color/base_frame_border_color"
/>
<padding
android:left="#dimen/frame_padding_size"
android:top="#dimen/frame_padding_size"
android:right="#dimen/frame_padding_size"
android:bottom="#dimen/frame_padding_size"
/>
</shape>
</item>
</layer-list>
It works, but being the border superimposed to the "solid" background, it's color need some adjustment.

Android borders color with selector listview

I had this working perfectly, although I need to change my code to listSelector, and now I don't know how to do it in order to maintain the border on left side.
In all listview lines I had one border:
border.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#E30E0E"/>
</shape>
</item>
<item android:left="5dip">
<shape android:shape="rectangle">
<solid android:color="#E3E3E3"/>
</shape>
</item>
</layer-list>
This sets the left side of each line, a border with color. But, as you can see, it has a default Solid color for background. With this code, whenever the user click on the item of the listview he doesn't understand if he has already clicked or not, because it has this solid color which doesn't change onState().
To make this working, I needed to create a listSelector with background gradients on state select plus state pressed.
selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="false"
android:state_pressed="false"
android:drawable="#drawable/listview_gradient_bg"/>
<item android:state_pressed="true"
android:drawable="#drawable/listview_gradient_bg_hover" />
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="#drawable/listview_gradient_bg_hover" />
</selector>
So, my question is: there's any way to "link" this two different piece of code?
Try to put this
<item android:left="5dip">
<shape android:shape="rectangle">
<solid android:color="#E3E3E3"/>
</shape>
</item>
into the listview_gradient_bg and listview_gradient_bg_hover drawables, then link the selector to each line in the adapter.

Button text disappears after setting text colors

My buttons use two XML files to do some fancy color switching when pressed, but I have a problem with the color drawable (dunno what to call it)...
Here's a button
<Button
android:background="#drawable/main_loginbtn"
android:textColor="#color/main_loginbtn"
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:onClick="login"
android:text="Login" />
(I know I shouldn't use hardcoded strings, but I will change this later :)
Here's #drawable/main_loginbtn
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle" >
<solid android:color="#FFFFFF"/>
<stroke android:width="2dp"
android:color="#00BFFF"/>
</shape>
</item>
<item>
<shape android:shape="rectangle" >
<solid android:color="#00BFFF"/>
<stroke android:width="2dp"
android:color="#FFFFFF"/>
</shape>
</item>
</selector>
And finally, here's the #color/main_loginbtn file
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#00BFFF"/>
<item android:color="#FFFFFF"/>
</selector>
If I remove the textColor reference to #color/main_loginbtn, it will fix the problem and the text will reappear on the button (in black of course). So i'm pretty sure the problem is in the color drawable.
Normally, I wouldn't care about this, but it messes up the scaling on some buttons because of wrap_content, when there's no text inside the buttons.
Thanks for your time!
UPDATE
I tried creating a second random color drawable, and tested it on a TextView's textColor attribute, and the same problem arose... The entire TextView text disappeared.
So i'm thinking it's a problem with the selector?
Oh, and I also messed up in this question: Nothing "disappears" per say, but rather I can't see the text in the eclipse layout UI. When I run it in the emulator, everything works fine...
You need to set the transparency of your color.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#FF00BFFF"/>
<item android:color="#FFFFFFFF"/>
</selector>
The UI editor is assuming your colors are transparent if you don't (in other words it's defaulting to alpha = 00 if you don't set it).

Categories

Resources