Write Multiline Text on Button in Android
Asked Answered
C

8

12

I want to know, How to write Multiline Text on Button

 <Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="100dp"
    android:background="@drawable/layout_border"
    android:text="Hours   Mon - sat 5pm" />

I am getting like this:-

enter image description here

but required this kind of button:-

enter image description here

Edited::--

Now i got something like this, by using answer given by, @Sino K D :

enter image description here

but still looking for help,

After adding drawable to left side, getting this:-

enter image description here

Capping answered 31/10, 2013 at 6:28 Comment(0)
M
29

Use &#10;

(new line)

example:-

android:text="Hi&#10;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>
Magellan answered 31/10, 2013 at 6:31 Comment(7)
Is Hours a static text?Magellan
then I think its better to design a image with text 'Hours' and give it as a left-drawable-icon of the button.Magellan
Example:- android:drawableLeft="@drawable/hour-text"Magellan
hours is also a text not a drawableCapping
as i said your answer is too useful, i used image, but will like to know how to add text to left side, anyways thanks for easy and great answerCapping
i hope you will find the solution, ok please see above how to align drawable center as well, on too left side, need exact shown in imageCapping
It may be obvious to everyone else, but it wasn't to me, so: don't forget to set the layout_height to something other than wrap_content, which forces the newline to collapse. You can use fill_parent like in this example, or a defined height like 100dp.Cavalryman
C
4

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)));
Cl answered 31/10, 2013 at 7:8 Comment(0)
B
2

Use &#10; (new line)

android:text="Hours&#10;Mon - sat 5pm"
Buckingham answered 31/10, 2013 at 6:32 Comment(0)
L
2

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);
Lambard answered 31/10, 2013 at 7:4 Comment(0)
E
0

For Android layout, multiple lines of text could be added to the elements.

Create a new variable with the character endOfLine "\n" in the res/values/strings.xml.

For example:

<string name="multiplelines">Line1 \n Line2</string>

Refer it in the layout file. For example,

<Button
    android:id="@+id/start"
    android:text="@string/multiplelines"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent">
</Button>
Extol answered 31/10, 2013 at 6:34 Comment(0)
W
0

you could use a LinearLayout instead of a button and achieve a similar effect:

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/layout_border"
android:orientation="horizontal" >
    <TextView
    android:layout_width="wrap_content"
    android:layout_height="match_content"
    android:textColor="@color/grey" // you'll have to define this yourself
    android:text="Hours"
    android:gravity="center_vertical" />

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:text="Mon - Sat 5:00pm&#10;Closed on Sundays"
    android:gravity="center_vertical />
</LinearLayout>

This might not work perfect, but it's a start

Wye answered 31/10, 2013 at 7:2 Comment(0)
M
0

The solution that can be used in strings.xml, the separator is \n within CDATA

<string name="switch_on_bluetooth"><![CDATA[Allumer\nBluetooth]]></string>
Meshwork answered 19/11, 2017 at 12:58 Comment(0)
E
0

Add

<Button
   android:layout_gravity="center_vertical" 
/>

in your XML file and use "\n" to break line in java file

btn3.setText("Selected Orders\nOnly");

https://i.sstatic.net/oXvNS.png

Epididymis answered 1/11, 2020 at 4:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.