8

I am trying to make a custom view and have declared the styled attributes like the below:-

  <resources>
 <declare-styleable name="NewCircleView">
    <attr name="radius" format="integer"/>
    <attr name="circlecolor" format="color"/>
</declare-styleable>

 </resources> 

in the constructor of the customview , these values are obtained like below:-

    circleradius=a.getInt(R.styleable.NewCircleView_radius, 0);//global var
    circlecolor=a.getColor(R.styleable.NewCircleView_circlecolor, 0);//global var and a is the typed array

The view is used by declaring the xml as below:-

 <com.customviews.NewCircleView
        android:layout_below="@id/thetext"
        android:layout_width="match_parent"
        android:layout_height="fill_parent" 
        app:radius="10000"
        app:circlecolor="@color/black"<!--this is defined in colors.xml
      />

In the custom view when i set the paint object as :-

thePaintObj.setColor(circlecolor);//circlecolor logs to an integer as expected

I dont get the color-"black" defined in the xml

however when i set the color as

thePaintObj.setColor(Color.GRAY)

I get the color in the view

Can someone tell me what would I be doing wrong ?

(N.B:-If you want me to post more code , please let me know)

EDIT1:- Posting my colors.xml. Looks like it is not clear in my code comments:-

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="red">#7f00</color>
<color name="blue">#770000ff</color>
<color name="green">#7700ff00</color>
<color name="yellow">#77ffff00</color>
<color name="black">#000000</color>
 </resources>
Rasmus
  • 8,248
  • 12
  • 48
  • 72

2 Answers2

13

In colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="black_color">#000000</color>
</resources>

To retrieve

Resources res = getResources();
int color = res.getColor(R.color.black_color);

Then set color to paint

thePaintObj.setColor(color);

More info @

http://developer.android.com/guide/topics/resources/more-resources.html#Color

Edit:

MyCustomView

public class CustomView extends View{

    Paint p;
    int color ;
    public CustomView(Context context) {
        this(context, null);
    }

    public CustomView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // real work here
        TypedArray a = context.getTheme().obtainStyledAttributes(
                attrs,
                R.styleable.NewCircleView,
                0, 0
        );

        try {

         color = a.getColor(R.styleable.NewCircleView_circlecolor, 0xff000000);
        } finally {
            // release the TypedArray so that it can be reused.
            a.recycle();
        }
        init();
    }

public void init()
{
      p = new Paint();
      p.setColor(color);
}

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        if(canvas!=null)
        {
        canvas.drawCircle(100, 100,30,p );
        }
    }

}

attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <declare-styleable name="NewCircleView">
    <attr name="radius" format="integer"/>
    <attr name="circlecolor" format="color" />
</declare-styleable>
</resources>

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="black_color">#000000</color>
</resources>

MyCustomView in xml

<com.example.circleview.CustomView
       xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:app="http://schemas.android.com/apk/res/com.example.circleview"
        android:id="@+id/cv"
        android:layout_width="match_parent"
        android:layout_height="fill_parent" 
        app:radius="30"
        app:circlecolor="@color/black_color"
      />

Snap Shot

enter image description here

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • I have already declared colors in colors.xml as mentioned in my comments against the code and I am retrieving it via xml where the custom view is used as app:circlecolor="@color/black" which I guess is the same as above. Please let me know if i am wrong – Rasmus Sep 08 '13 at 08:52
  • @itamecodes do check this http://stackoverflow.com/questions/3441396/defining-custom-attrs – Raghunandan Sep 08 '13 at 09:04
  • @itamecodes i just tried and it works for me. I will post the same. – Raghunandan Sep 08 '13 at 10:17
  • thanks for your effort on this but does it still work the same if you replace color = a.getColor(R.styleable.NewCircleView_circlecolor, 0xff000000); with color = a.getColor(R.styleable.NewCircleView_circlecolor); – Rasmus Sep 08 '13 at 22:53
  • I know that it is the default color and that is what is happening in your code above. ..it is just taking the default color rather than reading from the xml – Rasmus Sep 09 '13 at 13:20
  • @itamecodes no i can recheck with a different color if you want. the one i use is black and the default one is `0xff000000`. I will change it to a different one and check but i don't think that is an issue – Raghunandan Sep 09 '13 at 13:24
  • @itamecodes cross checked i changed to `#5FDE00` green and i get the green circle. So i don't think that is an issue. Do try using the above code as reference. – Raghunandan Sep 09 '13 at 13:34
  • ok accepted based on what you say ..but i still dont understand the error in my code which happens to be the same as yours – Rasmus Sep 09 '13 at 13:58
0

If I understand correctly, the constant 0x000000 results in transparent black since there is no Alpha component specified. The Alpha value is the first byte of a four byte color value. The constant for opaque (solid) black is 0xff000000. In other words, the color 0x000000, which is the same as 0x00000000, results in you drawing completely transparently. The constant for Red also looks wrong, resulting in transparent green.

Ashley
  • 1