0

I need to select a particular line within dynamic content from database. In every row of a table, few lines start with <#c>. I want to select and center the line which starts with <#c>. I am using SpannableString. I am able to remove <#c> but all texts become centered.

My problem is I am unable to select the paricular line. In the

setSpan (Object what, int start, int end, int flags)

How will I write start and end characters?

My problem is with

int start and  int end

How will I use spannableString.length() for int start and int end.

Below is like the content.

 |<#c>aaaaaaa    |
 |bbbbbbbbbb     |
 |<#c>dddddddd   |
 |cccccccccc     |
 |jjjjjjjjjj     |
 |<#c>ffffffff   |
 |rrrrrrrrr      |
 |ggggggggggg    |

I am trying below;

    TextView textView1 = ((TextView) rootView.findViewById(R.id.text2));
    String text1 = args.getString(ARG_OBJECT2);

    SpannableString spannableString;
    Object standard;
    if (text1.contains("<#c>")) {
        spannableString = new SpannableString(text1.replaceAll("<#c>", ""));
        standard = new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER);
        spannableString.setSpan(standard, 0, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        textView1.setText(spannableString);

    }

I checked with the following:

System.out.println("spannableString.length : " + spannableString.length());

Result was :

I/System.out: spannableString.length : 1763
I/System.out: spannableString.length : 537
I/System.out: spannableString.length : 931

spannableString.length() is taking all the characters in the content not taking only the particular lines where <#c> contains.

How to get the length of that particular line which contains <#c>?

xml file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/ScrlView"
        android:fillViewport="true"
        android:clickable="true"
        android:focusable="true"
        android:scrollbars="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/ll"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:width = "0dp"
                android:id="@+id/text2"
                android:textSize="20sp"
                android:textStyle="bold"
                android:textColor="@color/blue"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="2dp"
                android:layout_marginBottom="5dp"/>

        </LinearLayout>

    </ScrollView>

</RelativeLayout>

With the above code All the dynamic contents in the row of the table are becoming "centered". I need to center that particular lines which start with <#c>.

  • @Nilesh Rathod Old question was regarding multiple style but now question is how to select the line that means how to calculate starting and ending characters with spannableString.length(). Will you give me any suggestion? Actually I am unable to solve the problems that why I am asking. – priyanka naskar Sep 24 '19 at 05:24

1 Answers1

0

You can try this.

If your String start with <#c> then if part will execute else String will display as-it-is in else part

TextView textView1 = ((TextView) rootView.findViewById(R.id.text2));
String text1 = args.getString(ARG_OBJECT2);

SpannableString spannableString;
Object standard;
if (text1.startsWith("<#c>")) {
  spannableString = new SpannableString(text1.replaceAll("<#c>", ""));
  standard = new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER);
  spannableString.setSpan(standard, 0, spannableString.length(), 
  Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  textView1.setText(spannableString);
}else{
   textView1.setText(text1);
}
Ankit
  • 1,068
  • 6
  • 10