Writing multi-line text to a button in Android

I want to know how to write multiline text on a 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 get the following: -

enter image description here

but this button is required: -

enter image description here

Edited :: -

Now I have something like this, using the answer given by @Sino KD:

enter image description here

but still looking for help

After adding on the left side of the side, having received the following: -

enter image description here

+6
source share
7 answers

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 to 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> 
+17
source

you can achieve this.

1-> create a button in the 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 to 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 to your activity class

 Button button=(Button)findViewById(R.id.button); button.setCompoundDrawables( new TextDrawable("Hour"), null, null, null); 
+1
source

Why aren't you trying to do 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))); 
+1
source

Use &#10; (new line)

 android:text="Hours&#10;Mon - sat 5pm" 
0
source

For an Android layout, you can add multiple lines of text to elements.

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

For instance:

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

Refer to it in the layout file. For instance,

 <Button android:id="@+id/start" android:text="@string/multiplelines" android:layout_height="wrap_content" android:layout_width="fill_parent"> </Button> 
0
source

you can use 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> 

It may not work perfectly, but it's a start

0
source

Solution that can be used in strings.xml , delimiter \ n inside CDATA

 <string name="switch_on_bluetooth"><![CDATA[Allumer\nBluetooth]]></string> 
0
source

Source: https://habr.com/ru/post/957105/


All Articles