Drawable xml issue: corner radius not working - android

Due to a bug in my app on android 4.4, I had to change some of my xml drawable files.
Now I have this xml file that I use to give a background color and to round the corners:
My xml file:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#android:color/white" android:state_pressed="false">
<shape>
<corners android:radius="5dp" />
</shape>
</item>
</selector>
The problem is that my corners are not rounded at all!
Note that if I remove this attribute of the "item" tag:
android:drawable="#android:color/white"
it works.
But obiouvsly the background color is gone. I also underline that I cannot use this method:
<solid android:color="#android:color/white" />
This because for some unknown reasons it crashes on android 4.4 (as I said on top).
Does anyone know why corners aren't rounded and how can I fix it?
Thanks in advance.

Related

Half overlay effect on CardView

I'm trying to replicate this half, semi-transparent overlay effect (obtained with css) in Android Studio using xml. However, I've got troubles in figuring out how to do that. Some indications would be very appreciated!
The Background is:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:angle="45"
android:startColor="#ff4800"
android:endColor="#ffee01"
android:type="linear" />
</shape>
</item>
</selector>
I think I should apply another xml file as Foreground, but I don't know how to code it to create that effect.

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 handle the fast-scroller on Lollipop 5.1?

Background
I've made a tiny library, which shows how to mimic the style of the stock contacts app of Android Lollipop, here.
The problem
It seems that on Android 5.1 , the fast scroller looks very different than the previous one, and it's too close to the right, so it's hard to use it.
screenshot of Android 4.4 :
Here's a screenshot on Android 5 :
and on Android 5.1 :
What I've found
I've tried to go over all of the "what's new" section of Android 5.1, and also in some related classes docs, but I didn't find anything special, except for "setFastScrollStyle" . However, I couldn't find any explanation of how to use it (plus it's from API 21 , so that might not be the reason).
The question
How can I make the fast scroller to be located a bit to the left, so that it will be easier to touch it?
How do you use setFastScrollStyle? Is there any tutorial for this?
I had the same problem and after a few hours of research I came up with a solution. This AOSP commit helped me the most, it shows the changes made to the scrollbar in Android 5.1 (SDK 22): Update scrollbars to match Material spec
There are two possibilities:
A: Use the new style and add padding
This will keep the same new rectangle from API 21 (Android 5.1), but add some padding left and right.
1: Copy the fastscroll_thumb_material.xml to your drawables folder
This should be the content (removed AOSP comments to save space):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:tint="?attr/colorControlActivated"
android:shape="rectangle">
<solid android:color="#android:color/white" />
<size android:width="8dp" android:height="48dp" />
</shape>
</item>
<item>
<shape android:tint="?attr/colorControlNormal"
android:shape="rectangle">
<solid android:color="#android:color/white" />
<size android:width="8dp" android:height="48dp" />
</shape>
</item>
</selector>
2: Edit your theme
Add the following item to your theme. I think you could put this in a style and use it with setFastScrollStyle, but I find this easier.
<item name="android:fastScrollThumbDrawable">#drawable/fastscroll_thumb_material</item>
3: Edit fastscroll_thumb_material.xml
You can edit it to your liking, but I did the following, which adds 8dp to the left and right of the scrollbar. I added a layer-list and item around each shape and added android:right="8dp" and android:left="8dp" to the new item. I tried adding that to the original items, but that didn't work, neither did adding padding to the shape.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<layer-list>
<item android:right="8dp" android:left="8dp">
<shape android:tint="?attr/colorControlActivated"
android:shape="rectangle">
<solid android:color="#android:color/white" />
<size android:width="8dp" android:height="48dp" />
</shape>
</item>
</layer-list>
</item>
<item>
<layer-list>
<item android:right="8dp" android:left="8dp">
<shape android:tint="?attr/colorControlNormal"
android:shape="rectangle">
<solid android:color="#android:color/white" />
<size android:width="8dp" android:height="48dp" />
</shape>
</item>
</layer-list>
</item>
</selector>
B: Change the scrollbar to the old style
This will use the API 21, Android 5.0 style
1: Add the old drawables to your project
You can download them from the commit above (fastscroll_thumb_mtrl_alpha.png and fastscroll_track_mtrl_alpha.9.png), but I also bundled them, you can also download them from my Google Drive.
2: Add fastscroll_thumb_material.xml to your drawables
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<bitmap android:src="#drawable/fastscroll_thumb_mtrl_alpha"
android:tint="?attr/colorControlActivated" />
</item>
<item>
<bitmap android:src="#drawable/fastscroll_thumb_mtrl_alpha"
android:tint="?attr/colorControlNormal" />
</item>
</selector>
3: Add fastscroll_track_material.xml to your drawables
<?xml version="1.0" encoding="utf-8"?>
<nine-patch xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/fastscroll_track_mtrl_alpha"
android:tint="?attr/colorControlNormal" />
4: Edit your theme
Add the following item to your theme. I think you could put this in a style and use it with setFastScrollStyle, but I find this easier.
<item name="android:fastScrollThumbDrawable">#drawable/fastscroll_thumb_material</item>
<item name="android:fastScrollTrackDrawable">#drawable/fastscroll_track_material</item>
I hope this helps to achieve your goal :)
Why not set padding on the right side of your layout containing the ListView? Google webpage # setPadding(). Personally I used android:paddingLeft for a similar issue like yours.
Another common trick is to add a (hidden) ImageView in the layout on the right side (horizontal orientation). The ImageView would be hidden and the (width) size can be set.
Glad to help a frequent SO user, hope I did. Regards, Tommy Kwee.

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).

Change Gridview custom selector size

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>

Categories

Resources