Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using shape drawable as my background xml

Tags:

java

android

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.

like image 262
n179911 Avatar asked Aug 14 '09 06:08

n179911


People also ask

Is a drawable to use as the background?

NinePatch drawables. A NinePatchDrawable graphic is a stretchable bitmap image that you can use as the background of a view. Android automatically resizes the graphic to accommodate the contents of the view.


3 Answers

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>
like image 101
Lucas S. Avatar answered Oct 19 '22 05:10

Lucas S.


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.

like image 45
Tenacious Avatar answered Oct 19 '22 07:10

Tenacious


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.

like image 42
Melinda Green Avatar answered Oct 19 '22 06:10

Melinda Green