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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:45:36+00:00 2026-06-09T17:45:36+00:00

I have an ImageButton, which should contain the text. For this I use LayerDrawable

  • 0

I have an ImageButton, which should contain the text.
For this I use LayerDrawable (createImage method in CustomViewFactory class)
In short

 public class CustomViewFactory extends ImageButton {
    public CustomViewFactory createButton(String text) {
    this.setBackgroundDrawable(createImage(R.drawable.back_configuration, text));
    return this;
    }

back_configuration.xml

  <?xml version="1.0" encoding="utf-8"?>
     <selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/back_button_pressed" adroid:state_pressed="true">     </item>
    <item android:drawable="@drawable/back_button_normal"></item>
    <item android:drawable="@drawable/back_button_pressed" android:state_focused="true"/>

    </selector>

back_button_normal and back_button_pressed – 9 patch png files.

public LayerDrawable createImage(int resource, String text){
        Drawable [] layers = new Drawable[2];
        layers[0] = new TextDrawable(text);
        layers[1] = r.getDrawable(R.drawable.back_configuration);
        return new LayerDrawable(resource);
    }

public class TextDrawable extends Drawable {

    private final String text;
    private final Paint paint;
    public TextDrawable(String text) {

        this.text = text;
       this.paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setFilterBitmap(true); 
        paint.setTextSize(textSize);
        paint.setAntiAlias(true);
        paint.setFakeBoldText(true);
        paint.setStyle(Paint.Style.FILL);
        paint.setTextAlign(Paint.Align.LEFT);

    }

    @Override
    public void draw(Canvas canvas) {
        canvas.drawText(text, 0, 0, paint);
    }

    @Override
    public void setAlpha(int alpha) {
        paint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
        paint.setColorFilter(cf);
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }
}

Problem. If the text is large, then it is cut off. How to make ImageButton itself increased in size depending on the text inside. What are the alternative methods.

And if I use the Button, the image is stretched on the button

I apologize for my English. Thank you very much.

  • 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-09T17:45:37+00:00Added an answer on June 9, 2026 at 5:45 pm

    You can try using textView rather than imageView or imageButton and then assign background for the text view as the image you want and set the text you want for textView and make it wrap_content for layout_width

    Edit: check if you use padding for the text view remove it.

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

Sidebar

Related Questions

Currently I have this.. which works fine <asp:ImageButton ID=ImageButton1 runat=server Width=80 Height=60 ImageUrl='<%# Bind(ImageUrl)
I have ImageButton with CommandArgument attribute which is having multiple Eval value. When I
I have a RotateAnimation attached to an ImageButton which upon click rotates it and
I have the following code which creates an ImageButton and plays a sound when
I have an asp:ImageButton with OnClick=Btn_OnClick. In Btn_OnClick I have this line: DataTable dtTable
I have an ImageButton which on click i show a dialog box where users
I have a RelativeLayout in which i have one ImageButton. I need the position
I have a GalleryView which I want to add ImageButton to each image in
I have this app which was working find and suddenly is crashing caused by
I have a panel on an aspx page which contains an UpdatePanel. This panel

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.