Blundell
Blundell

Reputation: 76506

Android Refactoring - Best Practice - drawable shape and colors.xml

This may sound stupid, but I can think of reasons for and against.

Should I refactor the two color's out my drawable/shape.xml gradient into colors.xml?

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient 
        android:startColor="#FFFFFF" 
        android:endColor="#CCCCCC"
        android:angle="270" />
    <stroke 
        android:width="3dp" 
        color="@color/transparent" />
    <corners 
        android:radius="13dp" />
    <padding 
        android:left="10dp" 
        android:top="10dp"
        android:right="10dp" 
        android:bottom="10dp" />
</shape>

Keeping it in drawable/shape.xml show's that it is one entity ( a button ) and allows it to stay safe in one place. Moving it to colors.xml allows these color's to be shared with other resource perhaps if I was creating a theme or style? But then when looking at colors.xml how would I know they combine to make a gradient color?

Does anyone have an opinion, I hope I've added enough of my though process to spark some debate.

I tried looking at the Source for the IO Sched app: linky to see what Google Employee's did, but I can't find them creating any shapes.

Upvotes: 2

Views: 1210

Answers (2)

Roman Nurik
Roman Nurik

Reputation: 29745

I'd say that in general you should extract color resources, but make sure to name them logically.

For example, if you have something like accent_color_1 and accent_color_1_darker, one should assume there could be a color ramp between these two colors used in the app. It would make less sense, however, for you to color ramp from something like accent_color_1 and accent_color_2.

At the end of the day, though, it's really just a matter of personal or team preference.

Upvotes: 1

mgv
mgv

Reputation: 8484

I prefer defining all the colors in the separate xml file.

Apart from being able to use them in other resources you can give each color a name that describes what is it used for. So when you look at your shape code you will be reading something that makes sense and not some arcane hexadecimal characters.

If you can ensure that you are going to use that specific color just in one place, I guess it's just a matter of taste.

Upvotes: 1

Related Questions