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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:18:59+00:00 2026-06-16T18:18:59+00:00

I got stuck in an odd issue with Android – I want to have

  • 0

I got stuck in an odd issue with Android – I want to have a button that looks like this:

 |-----------------------------------------------------------------------|
 |                   [icon] <5px> [text text text]                       |
 |-----------------------------------------------------------------------|

and the group ([icon] <5px> [text text text]) should be centred. Note that 5px is used just as a placeholder for any padding you want to have between the icon and the text

I found some answers here that were more or less graviting around either setting a background (which I don’t want to do because I have another background) or using the android:drawableLeft property to set the icon.

However looks like the documentation of the setCompoundDrawablesWithIntrinsicBounds method is a bit misleading (see here). It states that the image is placed on the left/right/top/bottom side of the TEXT wich is not true. The icon is placed on the corresponding side of the BUTTON. For example:

Setting the android:drawableLeft property puts the icon on the most left position and gets me this (with gravity CENTER):

 |-----------------------------------------------------------------------|
 | [icon]                     [text text text]                           |
 |-----------------------------------------------------------------------|

or this (with gravity LEFT):

 |-----------------------------------------------------------------------|
 | [icon] [text text text]                                               |
 |-----------------------------------------------------------------------|

Both are ugly as hell 🙁

I found a workaround that looks like this:

public static void applyTextOffset(Button button, int buttonWidth) {
    int textWidth = (int) button.getPaint().measureText(button.getText().toString());
    int padding = (buttonWidth / 2) - ((textWidth / 2) + Constants.ICON_WIDTH  + Constants.ICON_TO_TEXT_PADDING);
    button.setPadding(padding, 0, 0, 0);
    button.setCompoundDrawablePadding(-padding);
}

And it works more or less but I don’t find it to my liking for following reasons:

  • it requires to know the button width. With auto-sized buttons it will not be known until the actual layout is done. Google recommend using a listener to learn the actual width after the rendering is done but this immensely complicates the code.
  • I feel like I’m taking over the layout responsibility from the Android layout engine

Isn’t there a more elegant solution?

  • 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-16T18:19:01+00:00Added an answer on June 16, 2026 at 6:19 pm

    You can use the following Button subclass to achieve this effect.

    1. Paste this class into your project and tweak the package name if desired.
        package com.phillipcalvin.iconbutton;
    
        import android.content.Context;
        import android.content.res.TypedArray;
        import android.graphics.Rect;
        import android.graphics.Paint;
        import android.graphics.drawable.Drawable;
        import android.util.AttributeSet;
        import android.widget.Button;
    
        public class IconButton extends Button {
          protected int drawableWidth;
          protected DrawablePositions drawablePosition;
          protected int iconPadding;
    
          // Cached to prevent allocation during onLayout
          Rect bounds;
    
          private enum DrawablePositions {
            NONE,
            LEFT,
            RIGHT
          }
    
          public IconButton(Context context) {
            super(context);
            bounds = new Rect();
          }
    
          public IconButton(Context context, AttributeSet attrs) {
            super(context, attrs);
            bounds = new Rect();
          }
    
          public IconButton(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            bounds = new Rect();
          }
    
          public void setIconPadding(int padding) {
            iconPadding = padding;
            requestLayout();
          }
    
          @Override
          protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
            super.onLayout(changed, left, top, right, bottom);
    
            Paint textPaint = getPaint();
            String text = getText().toString();
            textPaint.getTextBounds(text, 0, text.length(), bounds);
    
            int textWidth = bounds.width();
            int contentWidth = drawableWidth + iconPadding + textWidth;
    
            int contentLeft = (int)((getWidth() / 2.0) - (contentWidth / 2.0));
            setCompoundDrawablePadding(-contentLeft + iconPadding);
            switch (drawablePosition) {
            case LEFT:
              setPadding(contentLeft, 0, 0, 0);
              break;
            case RIGHT:
              setPadding(0, 0, contentLeft, 0);
              break;
            default:
              setPadding(0, 0, 0, 0);
            }
          }
    
          @Override
          public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom) {
            super.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);
            if (null != left) {
              drawableWidth = left.getIntrinsicWidth();
              drawablePosition = DrawablePositions.LEFT;
            } else if (null != right) {
              drawableWidth = right.getIntrinsicWidth();
              drawablePosition = DrawablePositions.RIGHT;
            } else {
              drawablePosition = DrawablePositions.NONE;
            }
            requestLayout();
          }
        }
    

    2. Modify your layout to use this new subclass instead of plain Button:

        <com.phillipcalvin.iconbutton.IconButton
            android:id="@+id/search"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:drawableLeft="@drawable/search"
            android:text="@string/search" />
    

    3. If you want to add padding between the drawable and the text, add the following to your activity’s onCreate:

        // Anywhere after setContentView(...)
        IconButton button = (IconButton)findViewById(R.id.search);
        button.setIconPadding(10);
    

    This subclass also supports drawableRight. It does not support more than one drawable.

    If you want more features, such as the ability to specify the iconPadding directly in your layout XML, I have a library that supports this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got some HTML that looks like this: <tr class=row-even> <td align=center>abcde</td> <td align=center><a
Really got stuck on this simple regex. Need it to validate a string, that
I got stuck in here, I have a custom scrollview in a view this
I got stuck with this, I want to select all the fields in 3
I got stuck with this scenario. Let me explain as follows. I have a
I recently got stuck in a situation like this: class A { public: typedef
Got stuck while trying to make an element in website behave like this: <div
I got stuck with the following scenario like I have two timers. If user
I have got stuck in a dump issue but am not able to work
So I got stuck in this dead end. I have AJAX script and jQuery

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.