0

I've created a simple custom view that is a rectangle. I can add it to my layout, but can't change its color that is defined in the class.

my class:

package com.example.customview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;

public class CustomRect extends View {

    Rect rect;
    Paint blue;

    public void init() {

        rect = new Rect(0, 0, 200, 200);
        blue = new Paint();
        blue.setColor(Color.BLUE);
        blue.setStyle(Paint.Style.FILL);
    }

    public CustomRect(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        init();
    }

    public CustomRect(Context context, AttributeSet attrs) {
        super(context, attrs);

        init();
    }

    public CustomRect(Context context) {
        super(context);

        init();
    }

    @Override
    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);

        canvas.drawRect(rect, blue);

        invalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // TODO Auto-generated method stub
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        setMeasuredDimension(200, 200);
    }   
}

my activity:

package com.example.customview;
import android.app.Activity;
import android.os.Bundle;

public class RectActivity extends Activity{

    CustomRect rect;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        rect = new CustomRect(this);
        setContentView(rect);
    }
}

my layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <view
        android:id="@+id/thisId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        class="com.example.customview.CustomRect" />

</LinearLayout>

If anybody has any idea to do that. Thanks.

1 Answer 1

1

Remove invalidate() from your onDraw(). Other classes should use invalidate() to indicate to Android that your custom view should be redrawn.

[EDIT]

To change the colour you use from XML, add a custom attribute. See the answer in this question:

Declaring a custom android UI element using XML

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, it seems there is a misunderstanding. It's working well for blue color. But how to change it from xml whenever I want. Thanks.
I can't see anything wrong with your namespace include. Did you try to rebuild you app then try again?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.