Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8553211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:36:01+00:00 2026-06-11T14:36:01+00:00

I am attempting to create my own view in android which is simply an

  • 0

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 40×40 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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T14:36:02+00:00Added an answer on June 11, 2026 at 2:36 pm

    Inside onMeasure() of your custom view add below logic:

    int width = 100;
        if(getLayoutParams().width==LayoutParams.WRAP_CONTENT)
        {
            width = 40;
        }
        else if((getLayoutParams().width==LayoutParams.MATCH_PARENT)||(getLayoutParams().width==LayoutParams.FILL_PARENT))
        {
            width = MeasureSpec.getSize(widthMeasureSpec);
        }
        else
            width = getLayoutParams().width;
    
    
        int height = 100;
        if(getLayoutParams().height==LayoutParams.WRAP_CONTENT)
        {
    
        }
        else if((getLayoutParams().height==LayoutParams.MATCH_PARENT)||(getLayoutParams().height==LayoutParams.FILL_PARENT))
        {
            height = MeasureSpec.getSize(heightMeasureSpec);
        }
        else
            height = getLayoutParams().height;
    
    
        setMeasuredDimension(width|MeasureSpec.EXACTLY, height|MeasureSpec.EXACTLY);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to create an automated script on my own site which logs into
I'm attempting to create a CustomControl which will have various properties affected by an
I am attempting to create my own custom Autocomplete style dropdown control in c#
Im attempting to create a new operator :? on lists, which operates the same
I am attempting to create a jQuery plugin for my own use that can
I am attempting to create a UIView (and associated UIViewController ) which mimic the
i've been unsuccessfully attempting to create my own undo/redo classes that work well with
I'm attempting to create a CodeActivity object that will register a DLL on a
I'm attempting to create my own immutable datatype/methods with defrecord in Clojure. The goal
I've been attempting to create my own app for a band by my friends,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.