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.

Hamzeh Soboh
  • 7,070
  • 5
  • 37
  • 54

1 Answers1

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

Community
  • 1
  • 1
Simon
  • 14,029
  • 6
  • 43
  • 60
  • 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. – Hamzeh Soboh Oct 14 '12 at 11:41
  • 1
    I can't see anything wrong with your namespace include. Did you try to rebuild you app then try again? – Simon Oct 15 '12 at 10:59