1

I want to fix some text of string in the center. How can I do that? For example, take a look on this photo

Heading at the center

I have a string like that. Now I want some words (Heading) of my string to be shown in a TextView at the center horizontally but others word will remain as usual. How can I do that?

I have tried the following code but it doesn't work for various dimension's devices.

          String string= "                 SYRIA
\n\nSyria oh Syria why do you bleed?
        \nBrother fights brother without thought or need
    \nRuled by a tyrant for so many years
    \nAnd now the split blood is washed away by tears\n"
Abhinav Gupta
  • 2,225
  • 1
  • 14
  • 30
  • You can take two different textview and set first textview gravity to center – Nik Mar 15 '18 at 09:37
  • If this is the only case then you can use Two `TextView` for it . If this can multiple times like a paragraph then you should go with `HTML` text . Or you can directly use HTML for this scenario too . – ADM Mar 15 '18 at 09:39
  • You can refer this [link](https://stackoverflow.com/questions/432037/how-do-i-center-text-horizontally-and-vertically-in-a-textview-on-android) for making text align center in a textview. – Manpreet Matharu Mar 15 '18 at 09:40
  • I have long paragraph actually and some text of the paragraph needs to be aligned center only. Multiple TextView makes the whole purpose more complicated in my case. –  Mar 15 '18 at 10:47

3 Answers3

1

You can wither use 2 different Textviews or If there is single textview, you can use HTML or Spannable String for that. Spannable can be used as follows:

TextView resultView = new TextView(this);
final String SyriaText = "Syria";
final String OtherText = "Other Text";
final String result = SyriaText + "  " + OtherText;
final SpannableString finalResult = new SpannableString(result);
finalResult.setSpan(new AlignmentSpan.Standard(Alignment.ALIGN_CENTER), 0, SyriaText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
resultView.setText(finalResult);

Also you can style it with Spannable. You can learn more from : https://developer.android.com/reference/android/text/Spannable.html

Zain
  • 37,492
  • 7
  • 60
  • 84
Siraj Sumra
  • 934
  • 1
  • 11
  • 28
0

You can use Html.fromHtml.Using this you only need single textview. Just do this, it should work perfectly

 your_textview.setText(Html.fromHtml("<h><center><b><u>SYRIA</u></b></center></h>\n" +
            "    <p>Syria oh Syria why do you bleed?</p> </br>\n" +
            "    <p>Brother fights brother without thought or need</p> </br>\n" +
            "    <p>Ruled by a tyrant for so many years</p> </br>\n" +
            "    <p>And now the split blood is washed away by tears</p> </br>"));

enter image description here

Navneet Krishna
  • 5,009
  • 5
  • 25
  • 44
  • I tried this but the heading didn't take position at the center. –  Mar 15 '18 at 10:43
  • it will take the center position, iam attaching a screenshot of the result in my answer – Navneet Krishna Mar 15 '18 at 10:47
  • 1
    Please check my result. would you please share your xml code for textview? https://prnt.sc/iri6vn It seems to me from your pic that you have aligned the whole TextView as center. So, rest of the text gets also aligned center. But I need only heading at the center. –  Mar 15 '18 at 10:55
0
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical">


    <TextView
        android:id="@+id/textView1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_gravity="center"
        android:text="seria"
        android:textSize="20sp"
        />


    <TextView
        android:id="@+id/textView2"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:text="Syria oh Syria why do you bleed?
        \nBrother fights brother without thought or need
    \nRuled by a tyrant for so many years
    \nAnd now the spilt blood is washed away by tears"
        />

</LinearLayout>
Nik
  • 1,991
  • 2
  • 13
  • 30