Justified Text with central alignment in android

2k views Asked by At

Hello everyone, i am working on android application in which i am showing some text using TextView with central alignment on the screen. But the problem is that the text is not properly justified on the screen. This is the xml code which i am using for showing text with central alignment on the screen. Please help me for doing this.

<TextView
        android:id="@+id/lastMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:layout_gravity="center"
        android:text="Mana addd asdwd ekff fec greq crrq grooe cieurnf covhrpw cjcneie ejducbc ueiche deidhe idhcecm ekwp ww"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textSize="12sp" />
2

There are 2 answers

0
elcid9 On

This library will provide you a way to justify text.

Compatible for Android 2.2 to 5.X

https://github.com/bluejamesbond/TextJustify-Android

0
jérémy On

If you want to center align text, use android:gravity="center" as follows:

<TextView
    android:id="@+id/lastMessage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:layout_gravity="center"
    android:gravity="center"
    android:text="Mana addd asdwd ekff fec greq crrq grooe cieurnf covhrpw cjcneie ejducbc ueiche deidhe idhcecm ekwp ww"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textSize="12sp" />