Search code examples
androidbuttontext-formatting

Format text within a button


I'm working on a custom numberpicker and I have a button with a '+' and '-' sign.
At the moment I just have this code to create the button.

increment = new Button(context);
increment.setTextSize(25);
increment.setText("+");

Now, i want to add the minimum and maximum value to be shown on the button like this.

example of target look
How can I achieve this?
So I need to center the max label, and give it a different size as the plus sign.

Any help would be greatly appreciated! :)


Solution

  • I don't know whether this is what your need or not,,

    But in your layout xml file,

    <Button
             android:id="@+id/btn1"
             android:layout_width="0dip"
             android:background="@android:color/darker_gray"
             android:drawableBottom="@drawable/image_plus"
             android:text="Max: 10"
             />
    
    <!-- 
    android:drawableBottom="@drawable/image_plus" // plus sign from drawable
    android:text="Max: 10" // Text you want to put on top of image
     -->
    

    Or using code,

    increment = new Button(context);
    increment.setTextSize(25);
    increment.setText("Max: 10");
    increment.setCompoundDrawables(null, null, null, getResources().getDrawable(R.drawable.plus_sign)); // Here you have to apply + sign image to drawable