0

In one of the activity of my application, I am displaying user names with their points in front of the names.

The name may have different number of characters, but I want the strings to end at the same place.

I am able to provide correct number of spaces and the strings are equal in character lengths but the characters take uneven space and ruin the symmetry of the strings.

Is there any work around for this?

Here's the code :

private String separateNamePoints{
    String text="";

            //text is separated by ,
    String[]splittedRawText=rawText.split(",");

    String name=(splittedRawText)[0];
    String points=(splittedRawText)[1];

    int pointsLength=points.length();
    int reqSpaceLength=40-name.length();

    String space="";

    for(int i=0;i<reqSpaceLength;i++){
        space+=" ";
    }

    if(pointsLength==1)
        space+="   ";
    else if(pointsLength==2)
        space+="  ";
    else if(pointsLength==3)
        space+=" ";

    text=name+space+points;

    return text;
}

And here's the image :

enter image description here

Shail Adi
  • 1,502
  • 4
  • 14
  • 35

4 Answers4

0

There are ways you can handle this..

  • Your approach

In here, you can try using String.format() to format as per requirement .. here read this for ref

  • You can use 2 textViews and use alignment to solve your problem..
CRUSADER
  • 5,486
  • 3
  • 28
  • 64
  • I have to set everything dynamically!! – Shail Adi Jun 18 '13 at 08:16
  • For, case 1 .. have you read the link provided ?? Just try out first.. check [this for ref](http://stackoverflow.com/a/13789157/2345913)..For case 2 ... follow code snippet provided by @Dimmerg.. That will do the trick.. – CRUSADER Jun 18 '13 at 09:46
  • It does not help. It gives the same problem that I face with the logic above. The console and logcat give proper strings but in the textview of android, texts are not justified. – Shail Adi Jun 18 '13 at 11:21
  • I think need to use separate textviews somehow. – Shail Adi Jun 18 '13 at 11:22
0

Try to use this xml for list item and write adapter for this:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/name" 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="4"/>

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

</LinearLayout>
Dimmerg
  • 2,113
  • 1
  • 13
  • 14
0

Android do not provide complete text justification.

In this case, need to create a custom control containing separate edittexts to display the data.

Shail Adi
  • 1,502
  • 4
  • 14
  • 35
-1

Your code will work fine. I think your input string having some space after the number. please try with this

String[]splittedRawText=rawText.trim().split(",");
Sivaraman
  • 61
  • 7