Home » Android » android – How do I give space in between text in textview?

android – How do I give space in between text in textview?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I need to make something like this according to Android screen bound,

 S             M             T              W               T              F               S

I am doing like this:

    <TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="45dp"
    android:text="S       M       T        W        T        F       S" />

This is not working properly, not according to device screen bound.
I need something like this:

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="45dp"
    android:text="S""10dp""M""10dp""T""10dp""W""10dp""T""10dp""F""10dp""S""10dp" />
How to&Answers:

We can use it.

<TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="A B     C"/>

Answer:

Use the letterSpacing attribute for spacing between letters:

android:id="@+id/txt_work"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:typeface="serif"
android:layout_below="@+id/txt_wecm"
android:layout_marginLeft="20dp"
android:textSize="32dp"
android:letterSpacing="0.1"
android:textColor="#fff"
android:text="World" 

Answer:

USE Unicode Character NO-BREAK SPACE TextView support this

try this

<TextView
  android:id="@+id/textView1"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_marginTop="45dp"
  android:text="@string/days" />

put this in String.xml

<string name="days">S\u00A0M\u00A0T\u00A0W\u00A0T\u00A0F\u00A0S</string>

EDIT 1

it is fulfill your requirement but not efficient way till you not find solution with one TextView you can try like this

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="S" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="M"
    android:textAlignment="center" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="T" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="W" />

<TextView
    android:id="@+id/textView5"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="T" />

<TextView
    android:id="@+id/textView6"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="F" />

<TextView
    android:id="@+id/textView7"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="S" />

Answer:

Letter-spacing in the TextView –

android:textScaleX

Sets the horizontal scaling factor for the text.

Must be a floating point value, such as “1.2”.

You can increase the scaling factor depending upon the scale that you need between letters.

Android docs for android:textScaleX.

Answer:

If you want some space between Text in textView, you can use android:lineSpacingExtra=”10dp”

you can try like that:-

<TextView
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:lineSpacingExtra="10dp"
        android:text="hi
                     hello"/>

Answer:

textView.setText(Html.fromHtml("<span style=letter-spacing:10px>TEXT</span> "));

But i dont if this works