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 8993887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:17:47+00:00 2026-06-15T23:17:47+00:00

I’m trying to draw a line on the Compound Drawable of a regular old

  • 0

I’m trying to draw a line on the Compound Drawable of a regular old Android button. I started out by just trying to get the drawable of the button using Button.getCompoundDrawables()[1] but the lines would never show up. So, I went ahead and put an actual image in my XML layout for the compound drawable on the button. This works OK (the orange square in the pic) but when the phone is rotated, the orange square doesn’t resize, proprotional to the button, so it ends up being way too big. Do I need to be calling getBounds() upon rotation or something?

There must be some resizing going on with the drawable because if you notice, the red line goes to the corners in Horizontal orientation, but not in the vertical; it’s off or something. The orange square resides as different sizes in drawable-[lhm]dpi/ directories, but I do not have two separate layouts for horizontal and vertical.

Code for drawing the line:

    @Override
            public View getView (int position, View convertView, ViewGroup parent)
            {           
                View row = convertView;
                if (row == null)
                {
                    LayoutInflater inflater = (LayoutInflater) _context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
                    row = inflater.inflate (R.layout.monthview, parent, false);

                }


                btn_cell = (Button) row.findViewById (R.id.bcell);
...
                    BitmapDrawable btn_draw = (BitmapDrawable) btn_cell.getCompoundDrawables ()[1];

                    if (btn_draw != null)
                    {
                        Log.d (TAG, "+++++++++++++ drawing line");
                        Bitmap btn_bmp = btn_draw.getBitmap ();
                        Bitmap offscreen_bmp = Bitmap.createBitmap(btn_bmp.getWidth(), btn_bmp.getHeight(), btn_bmp.getConfig());
                        BitmapDrawable offscreen_draw = new BitmapDrawable (offscreen_bmp);
                        offscreen_draw.setBounds (btn_draw.getBounds ());

                        Canvas c = new Canvas(offscreen_bmp);

                        // draw line
                        Paint p = new Paint();
                        p.setAntiAlias(true);
                        p.setStrokeWidth(1);
                        p.setStyle(Style.FILL_AND_STROKE);
                        p.setColor(Color.RED);

                        c.drawBitmap (btn_bmp, 0, 0, p);
                        c.drawLine (0, 0, offscreen_bmp.getWidth (), offscreen_bmp.getHeight (), p);

                        (R.drawable.cal_left_arrow_off), null, null);
                        btn_cell.setCompoundDrawables(null, offscreen_draw, null, null);
                    }

enter image description here
enter image description here

  • 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-15T23:17:48+00:00Added an answer on June 15, 2026 at 11:17 pm

    In case it helps someone else, I ended up doing away with the Button and instead using an ImageView. I ran into a (apparently) typical problem of just how to draw a line on the ImageView. I knew about sub-classing ImageView and drawing lines in the onDraw() method, however I didn’t know you could reference that “custom” sub-class inside Eclipse’s XML Layout GUI. All I needed to do was create a myImageView class and then reference it in the XML source code. Instead of specifying <ImageView> I needed to use <com.example.myImageView> and everything worked.

    I was using myImageView in a custom BaseAdapter so getting the lines placed on myImageView in the getView() method was just a matter of:

            myImageView iv = (myImageView) row.findViewById (R.id.iv_draw);
            iv.setBarLength (15);
            iv.setOnClickListener (ocl);
    

    com.example.test.myImageView.java:

    public class myImageView extends android.widget.ImageView
    {
        private final String TAG = this.getClass ().getName ();
        private int mBarLen = 5;
    
        /**
         * @param context
         * @param attrs
         */
        public myImageView (Context context, AttributeSet attrs)
        {
            super (context, attrs);
        }
    
        public void setBarLength (int length)
        {
            mBarLen = length;
        }
    
        @Override
        protected void onDraw (Canvas canvas)
        {
            super.onDraw (canvas);
    
            // draw 1px border
            Paint p = new Paint ();
    
            int x = 1;
            int y = 1;
            Rect bounds = canvas.getClipBounds ();
    
            int x2 = bounds.right - 1;
            int y2 = bounds.bottom - 1;
    
            canvas.drawLine (x, y, x2, y, p);
            canvas.drawLine (x2, y, x2, y2, p);
            canvas.drawLine (x2, y2, x, y2, p);
            canvas.drawLine (x, y2, x, y, p);
    
            p.setColor (Color.RED);
            p.setStrokeWidth (2);
            int bx = x + 5;
            int by = y + 5;
            canvas.drawLine (bx, by, bx+mBarLen, by, p);
            canvas.drawLine (bx, by, bx+mBarLen, by, p);
    
        }
    
    }
    

    xml file:

    <com.example.test.myImageView
        android:id="@+id/iv_draw"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:adjustViewBounds="true"
        android:minHeight="48dp"
        android:minWidth="24dp"
        android:scaleType="fitXY" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I am trying to render a haml file in a javascript response like so:
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only

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.