Note that there are some explanatory texts on larger screens.

plurals
  1. POAutoresize ImageButton with LayerDrawable background
    text
    copied!<p>I have an ImageButton, which should contain the text. For this I use LayerDrawable (createImage method in CustomViewFactory class) In short</p> <pre><code> public class CustomViewFactory extends ImageButton { public CustomViewFactory createButton(String text) { this.setBackgroundDrawable(createImage(R.drawable.back_configuration, text)); return this; } </code></pre> <p>back_configuration.xml</p> <pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:drawable="@drawable/back_button_pressed" adroid:state_pressed="true"&gt; &lt;/item&gt; &lt;item android:drawable="@drawable/back_button_normal"&gt;&lt;/item&gt; &lt;item android:drawable="@drawable/back_button_pressed" android:state_focused="true"/&gt; &lt;/selector&gt; </code></pre> <p>back_button_normal and back_button_pressed - 9 patch png files.</p> <pre><code>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; } } </code></pre> <p>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.</p> <p>And if I use the Button, the image is stretched on the button</p> <p>I apologize for my English. Thank you very much. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload