SoFunction
Updated on 2025-03-02

Android programming method to add pictures and text to Button

This article describes the Android programming method to add pictures and text to Button. Share it for your reference, as follows:

//How to add pictures and text to buttonspublic Spanned getSpan(int id, String s) {
 ImageGetter imgGetter = new () {
  @Override
  public Drawable getDrawable(String source) {
  // TODO Auto-generated method stub
  Drawable drawable = null;
  drawable = ().getDrawable(
   (source));
  (0, 0, (),
   ());
  return drawable;
  }
 };
 StringBuffer sb = new StringBuffer();
 ("<img src=\"").append(id).append("\"/>")
  .append("       ").append("<font>" + s + "</font>");
 ;
 Spanned span = ((), imgGetter, null);
 return span;
}

Use of this method:

button=(Button)findViewById();
(getSpan(.button_iv,"Button"));

Among them, .button_iv is an int type and is stored in the res/drawable folder. It is the image added to the button. It corresponds to the id in getSpan(int  id,String  s), and the s in this method refers to the text added to the button!

I hope this article will be helpful to everyone's Android programming design.