I am attempting to create my own view in android which is simply an oval and will be drawn correctly based on the typical android:layout_width and android:layout_height parameters.
So for example, if in my XML layout file, I have a LinearLayout which is the highest level View Container, I then want to be able to add multiple of my own oval Views. (which I called "PieFiller")
I know I am very close, but for some reason, I do not know how to determine if the user in the XML file said "fill_parent" or "wrap_content" and I don't know if I should even be asking for that information. Lets assume that wrap_content would simply draw a circle that is 40x40 pixels. But, if the XML layout file specifies that the width should "fill_parent" and the height "wrap_content" I want a long horizontal oval that is 40 pixels high and the number of pixels of the screen across.
Below is the code from my layout xml file and my custom view:
layout file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res/com.example.relativelayoutexample"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.example.relativelayoutexample.PieFiller
android:layout_width="match_parent"
android:layout_height="wrap_content"
custom:labelPosition="left"
custom:showText="true" />
<com.example.relativelayoutexample.PieFiller
android:layout_width="fill_parent"
android:layout_height="wrap_content"
custom:labelPosition="left"
custom:showText="true" />
</LinearLayout>
Custom view class called "PieFiller"
package com.example.relativelayoutexample;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
public class PieFiller extends View
{
private boolean mShowText;
private int mTextPos;
private Paint paint;
private RectF mBounds;
public PieFiller(Context context, AttributeSet attrs)
{
super(context, attrs);
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.PieFiller,0,0);
try
{
mShowText = a.getBoolean(R.styleable.PieFiller_showText, false);
mTextPos = a.getInteger(R.styleable.PieFiller_labelPosition, 0);
}
finally
{
a.recycle();
}
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.FILL);
mBounds = new RectF();
}
public void onDraw(Canvas canvas)
{
canvas.drawCircle(40.0f, 40.0f, 40.0f, paint);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
int minw = getSuggestedMinimumWidth();
int w = Math.max(minw,MeasureSpec.getSize(widthMeasureSpec));
int minh = this.getSuggestedMinimumHeight();
int h = Math.max(minh, MeasureSpec.getSize(heightMeasureSpec));
setMeasuredDimension(w,h);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
mBounds = new RectF(0,0,w,h);
}
public boolean ismShowText()
{
return mShowText;
}
public void setmShowText(boolean mShowText)
{
this.mShowText = mShowText;
this.invalidate();
this.requestLayout();
}
public int getmTextPos()
{
return mTextPos;
}
public void setmTextPos(int mTextPos)
{
this.mTextPos = mTextPos;
this.invalidate();
this.requestLayout();
}
}
By the way, the result of the code posted above, the first PieFiller takes up the entire screen. (which I don't want it to)