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>
The parameter to onDraw() is a Canvas object that the view can use to draw itself. The Canvas class defines methods for drawing text, lines, bitmaps, and many other graphics primitives. You can use these methods in onDraw() to create your custom user interface (UI).
What you need to do is set the color between drawing. paint. setColor(color. RED); // Will apply to first path.
A well-designed custom view is much like any other well-designed class. It encapsulates a specific set of functionality with an easy to use interface, it uses CPU and memory efficiently, and so on. In addition to being a well-designed class, though, a custom view should: Conform to Android standards.
The draw() method is called by the framework when the view need to be re-drawn and the draw() method then calls the onDraw() to draw the view's content.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With