Write Multiline Text on Button in Android
Why dont you try this by coding
String styledText = "<small> <font color='#000000'>"
+ "Mon-Sat 5:00 pm" + "</font> </small>"+ "<br/>"
+ "<small> <font color='#000000'>" + "Closed on Sunday"
+ "</font> </small>";
sendrequest.setText((Html
.fromHtml(styledText)));
you can achieve using this.
1->create a button in layout as
<Button
android:id="@+id/buton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Mon - Sat 5 pm\nClosed on sunday"
/>
2-> Add this class in your project.
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
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.setTextSize(20f);
paint.setAntiAlias(true);
paint.setFakeBoldText(true);
paint.setShadowLayer(6f, 0, 0, Color.BLACK);
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;
}
}
3-> add these lines in your activity class
Button button=(Button)findViewById(R.id.button);
button.setCompoundDrawables( new TextDrawable("Hour"), null, null, null);
Use
(new line)
android:text="Hours Mon - sat 5pm"
Use
(new line)
example:-
android:text="Hi Hello"
OR
1) Define in ../res/values/strings.xml:
<string name="multilines">Line1Line1\nLine2Line2</string>
2) Refer it in the layout file:
<Button
android:id="@+id/btn_multilines"
android:text="@string/multilines"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
</Button>