n179911
n179911

Reputation: 20341

Using shape drawable as my background xml

I really appreciate if someone can help me with using how to use shape drawable as my background xml for my view.

This is what I tried: But I never get the color. Android always gives me black text on white background, regardless what color attribute I put.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <stroke android:width="1dip" android:color="#FBBB" />
            <solid android:color="#6000"/> 
</shape>

I tried , does not work

<shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle"
            android:color="#6000>

</shape>

I tried , does not work

<shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle"
            android:background="#6000>
</shape>

I google this is the limited result I found to try.

Upvotes: 27

Views: 92203

Answers (5)

Fakhriddin Abdullaev
Fakhriddin Abdullaev

Reputation: 4940

try this

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

<corners
    android:bottomLeftRadius="5dp"
    android:bottomRightRadius="5dp"
    android:radius="0.1dp"
    android:topLeftRadius="5dp"
    android:topRightRadius="5dp" />

<solid android:color="#Efffff" />

<stroke
    android:width="2dp"
    android:color="#25aaff" />

</shape>

Upvotes: 3

Lucas S.
Lucas S.

Reputation: 13551

You have wrong color settings, you must specify 4 byte colors, eg: #ffff8080

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#f0600000"/>
    <stroke android:width="3dp" android:color="#ffff8080"/>
    <corners android:radius="3dp" />
    <padding android:left="10dp" android:top="10dp"
        android:right="10dp" android:bottom="10dp" />
</shape>

Upvotes: 44

Muhammad Aamir Ali
Muhammad Aamir Ali

Reputation: 21117

You are giving a wrong hexa color attribue. It should be eight digits after #. e.g #00000000

Upvotes: 3

Melinda Green
Melinda Green

Reputation: 2140

It seems like there are a couple of problems here. The biggest one seems to be the idea that you can use a shape as a text color and that doesn't seem to make sense. You can use a color as a background of a shape, and you can set a shape as the background of view but you can't set a shape as text background or foreground.

The other thing that looks wrong is that in the alternative XML files that you tried, the end quotes around the color values are missing, so that shouldn't compile at all.

Upvotes: 3

Tenacious
Tenacious

Reputation: 594

OK - I'm pretty sure my problem is the same as what drove your question, and that I've found its cause.

The problem is conflicting resource definitions (specifically, resource filenames). Say, for example, for some reason you put a file named "drawable_bg.png" in /res/color/ in your project; and forgot that you did this (or it happened accidentally). If you then try to define a Shape Drawable in your project named "res/drawable/dialog_bg.xml" - the PNG (from 'MyLib') takes precedence. Since you can have many "res" folders for different DPI, form-factor, SDK, etc - it's fairly easy to wind up with a filename collision. This can also happen with Android Library projects. If your project has any dependencies on projects which themselves have resources, they can cause conflicts. As I just found today, Eclipse can either hide or fail to show a warning about this in many situations.

When this happens it can easily appear that the Shape Drawable is not applied. Since "dialog_bg.png" probably isn't designed for your view you get unexpected results and it's easy to be confused about what's really going on.

The easiest way to solve this is to rename the shape drawable in your project. If the problem is with a resource(s) in an Android Library Project, then there may be a better solution found by applying the recommended practice as described at http://tools.android.com/recent/buildchangesinrevision14.

Upvotes: 5

Related Questions