coder
coder

Reputation: 10520

draw custom view then add to xml

I have LinearLayout in my xml file that I am trying to add a custom view to. Here is my xml code:

        <LinearLayout android:id="@+id/amenitycontent"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/background_color">

            <com.mypackage.util.AmentiesView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

        </LinearLayout>

This code is inside a TabHost (not sure if this makes a difference).

and here is my custom view:

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class AmentiesView extends View{

Bitmap image;
String title;
private final Paint paint = new Paint();

public AmentiesView(Context context) {
    super(context);
    image = BitmapFactory.decodeResource(getResources(), R.drawable.checkcircle);

}
public AmentiesView(Context context, AttributeSet attrs) {
    super(context, attrs);
    image = BitmapFactory.decodeResource(getResources(), R.drawable.checkcircle);

}

public void setTitle(String _title){
    title = _title;
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawBitmap(image, 0, 0, paint);
    canvas.drawText(title, image.getWidth() + 10, 0, paint);
    canvas.drawColor(Color.RED);
}


}

When I run the code, nothing is shown. Any ideas?

Upvotes: 0

Views: 679

Answers (1)

asenovm
asenovm

Reputation: 6517

Try setting some fixed numbers for width and height of your view - i.e 300x200. There's a good chance that since you're setting the width and height to wrap_content and your view basically has no content(nothing added in it) the canvas you're getting in onDraw is empty.

Upvotes: 1

Related Questions