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

  • Home
  • SEARCH
  • 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 6776091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:59:09+00:00 2026-05-26T15:59:09+00:00

All i wanted was to create a button that in some cases will contain

  • 0

All i wanted was to create a button that in some cases will contain a ‘v’ image to mark that it’s active. not really a check box since not every time you press the button it changes it’s state.

But the idea is this.
I need a plain button and i have another small image of ‘v’ that if i use a toggle() method on that button the button will get extended enough so the ‘v’ image can squeeze nicely to it’s upper right corner.

this is my code:

public class ButtonWithV extends Button {
    private Drawable mVImageDrawable;
    private int mImageWidth;
    private boolean mIsMarked;

    public ButtonWithV(Context context) {
        this(context, null);
    }

    public ButtonWithV(Context context, AttributeSet attrs) {
        this(context, attrs, R.attr.ButtonWithVStyle);
    }

    public ButtonWithV(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mImageWidth = 0;
        TypedArray a = context.obtainStyledAttributes(
                attrs, R.styleable.ButtonWithV, defStyle, 0);
        Drawable d = a.getDrawable(R.styleable.ButtonWithV_button_v_drawable);
        if( d != null){
            setVImageDrawable(d);
        }
//      setPadding(getPaddingLeft(), getPaddingTop(),
//              getPaddingRight(), getPaddingBottom());
        mIsMarked = false;
    }

    public void setMarked(boolean marked){
        if( marked != mIsMarked){
            mIsMarked = marked;
            requestLayout();
//          invalidate();
        }
    }

    private void setVImageDrawable(Drawable d) {
        mVImageDrawable = d;
        mImageWidth = mVImageDrawable.getIntrinsicWidth();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Rect temp = canvas.getClipBounds();
        System.out.println(String.format("top: %d, bottom: %d, left: %d, right: %d",
                temp.top, temp.bottom, temp.left, temp.right));
        super.onDraw(canvas);
        System.out.println(String.format("top: %d, bottom: %d, left: %d, right: %d",
                temp.top, temp.bottom, temp.left, temp.right));

        if( mVImageDrawable != null && mIsMarked){
            Rect bounds = canvas.getClipBounds();
            int right = bounds.right - 5;
            int left = right - mImageWidth;
            int top = bounds.top + 2;
            int bottom = top + mVImageDrawable.getIntrinsicHeight();
            mVImageDrawable.setBounds(left, top, right, bottom);
            mVImageDrawable.draw(canvas);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if( mIsMarked ){
            setMeasuredDimension(getMeasuredWidth() + mImageWidth, getMeasuredHeight());
        }
    }

    public void toggle(){
        setMarked(! mIsMarked );
    }

}

Please note the -5 and +2 (they cannot remain as such i just wanted to see exact numbers).
What I find problematic here, and will probably be problematic, is in all widgets I cannot use the widget’s getRight(), getLeft() etc…

Since they give me the REAL widget location on the canvase, however (in onDraw) when I get the canvas it’s clipped and translated to (0,0).

If I get it correctly and I want to know the widget’s rightest corner I need to get the clip rightest corner or the measured width. same goes for the height.

Another problem is I have no idea how the background is drown but I do know that since I have to remove 5 pixels from the right of the widget to get to it’s exact right position it means inner margins or padding.

It’s not regular paddings, I checked. so what can it be ? how can I get, always, including when user set paddings and margins the upper right relative coordinates ? I need to draw there.

  • 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-05-26T15:59:10+00:00Added an answer on May 26, 2026 at 3:59 pm

    eventually i used padding to solve the issue, i just add more padding the width of the needed button to the right so i can draw the overlay image.
    here’s the fixed code snapshot:

    public void setMarked(boolean marked){
            if( marked != mIsMarked){
                mIsMarked = marked;
                requestLayout();
            }
        }
    
    
        private void setVImageDrawable(Drawable d) {
            mVImageDrawable = d;
            mImageWidth = mVImageDrawable.getIntrinsicWidth();
        }
    
        @Override
        public int getCompoundPaddingRight() {
            int orig = super.getCompoundPaddingRight();
            int res = orig;
            if( mIsMarked ){
                res += mImageWidth;
            }
            return res;
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            if( mVImageDrawable != null && mIsMarked){
                Rect bounds = canvas.getClipBounds();
                int right = bounds.right;
                int left = right - mImageWidth;
                int top = bounds.top;
                int bottom = top + mVImageDrawable.getIntrinsicHeight();
                mVImageDrawable.setBounds(left, top, right, bottom);
                mVImageDrawable.draw(canvas);
            }
        }
    
        public void toggle(){
            setMarked(! mIsMarked );
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to set some handler for all the unexpected exceptions that I might
all, I wanted to create a multiline textbox but all the info i googled
I'm working on some out transitions on image when a user presses the button
So, I can create an input button with an image using <INPUT type="image" src="/images/Btn.PNG"
i wanted to create a field , which is all in same length. i.e
I've inherited some code and wanted to run this modification by you all, my
I have a UINavigationBar that has two button items already. I will need to
I wanted to list all my Recently Used Items. I use this code: public
I wanted to sorting items in select, but all solution what I finded have
I wanted to know if we can have a unique identifier for all objects

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.