简体   繁体   中英

How to give equal spacing between text?

I have to display following data in this format in text view.

1 Address : Sample address.
2 Contact : contact1, contact2
3 Email : sampleemail@hgjasd.com.
4 Website : www.sample.com.
5 Attached with : Apolo.
6 Services : Heart Transplant.

but the ":" is not aligned properly and there is no equal space between them How can i acheive this

This is what i tried

if (mrecordsObj.has("website")) {

            if(!mrecordsObj.getString("website").isEmpty())
            mBuilder.append("Website    :" + mrecordsObj.getString("website") + "\n"+"\n");

        }
        if (mrecordsObj.has("extrainfo")) {

            if(!mrecordsObj.getString("extrainfo").isEmpty())
            mBuilder.append("Extrainfo  :" + mrecordsObj.getString("extrainfo") + "\n"+"\n");

        }

If this is your string:

1 Address : Sample address. 2 Contact : contact1, contact2 3 Email : sampleemail@hgjasd.com. 4 Website : www.sample.com. 5 Attached with : Apolo. 6bServices : Heart Transplant

then you need to use the \\n and \\t characters to align it properly when it is displayed.

1 Address : \\t Sample address. \\n 2 Contact : \\t contact1, contact2 \\n 3 Email : \\t sampleemail@hgjasd.com. \\n 4 Website : \\t www.sample.com. \\n 5 Attached with : \\t Apolo. \\n 6bServices : \\t Heart Transplant

So the \\t character adds a tab and \\n is for a new line. It should align and format properly when you display this string with a TextView .

Try below,

   if (mrecordsObj.has("website")) {

        if(!mrecordsObj.getString("website").isEmpty())
        mBuilder.append("Website\t : \t" + mrecordsObj.getString("website") + "\n\n");

    }
    if (mrecordsObj.has("extrainfo")) {

        if(!mrecordsObj.getString("extrainfo").isEmpty())
        mBuilder.append("Extrainfo\t : \t" + mrecordsObj.getString("extrainfo") + "\n\n");

    }

Dear Try to take Layout Like this..

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_backgroud"
android:orientation="vertical"
android:padding="5dp">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="1"
    android:orientation="horizontal">

<TextView
    android:id="@+id/lbl1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"

    android:textColor="@color/colorPrimary"
    android:layout_alignParentTop="true"
    android:text="1. Address"
    android:layout_weight="0.50"

    />

    <TextView

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:textColor="@color/colorPrimary"
        android:layout_alignParentTop="true"
        android:text=":"

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

        android:textColor="@color/colorPrimary"
        android:layout_alignParentTop="true"
        android:text="Sample address"
        android:layout_marginLeft="5dp"
        android:layout_weight="0.50"
        />

</LinearLayout>


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="1"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/lbl2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"

        android:textColor="@color/colorPrimary"
        android:layout_alignParentTop="true"
        android:text="2. Contact"
        android:layout_weight="0.50"

        />

    <TextView

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:textColor="@color/colorPrimary"
        android:layout_alignParentTop="true"
        android:text=":"

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

        android:textColor="@color/colorPrimary"
        android:layout_alignParentTop="true"
        android:text="contact1, contact2"
        android:layout_marginLeft="5dp"
        android:layout_weight="0.50"

        />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="1"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/lbl3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"

        android:textColor="@color/colorPrimary"
        android:layout_alignParentTop="true"
        android:text="3. Email"
        android:layout_weight="0.50"

        />

    <TextView

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:textColor="@color/colorPrimary"
        android:layout_alignParentTop="true"
        android:text=":"

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

        android:textColor="@color/colorPrimary"
        android:layout_alignParentTop="true"
        android:text="sampleemail@hgjasd.com"
        android:layout_marginLeft="5dp"
        android:layout_weight="0.50"

        />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="1"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/lbl4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"

        android:textColor="@color/colorPrimary"
        android:layout_alignParentTop="true"
        android:text="4. Website"
        android:layout_weight="0.50"

        />

    <TextView

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:textColor="@color/colorPrimary"
        android:layout_alignParentTop="true"
        android:text=":"

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

        android:textColor="@color/colorPrimary"
        android:layout_alignParentTop="true"
        android:text="www.sample.com"
        android:layout_marginLeft="5dp"
        android:layout_weight="0.50"

        />

</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="1"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/lbl5"
        android:layout_width="0dp"
        android:layout_height="wrap_content"

        android:textColor="@color/colorPrimary"
        android:layout_alignParentTop="true"
        android:text="5. Attached with"
        android:layout_weight="0.50"

        />

    <TextView

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:textColor="@color/colorPrimary"
        android:layout_alignParentTop="true"
        android:text=":"

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

        android:textColor="@color/colorPrimary"
        android:layout_alignParentTop="true"
        android:text="Apollo"
        android:layout_marginLeft="5dp"
        android:layout_weight="0.50"

        />

</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="1"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/lbl6"
        android:layout_width="0dp"
        android:layout_height="wrap_content"

        android:textColor="@color/colorPrimary"
        android:layout_alignParentTop="true"
        android:text="6. Service"
        android:layout_weight="0.50"

        />

    <TextView

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:textColor="@color/colorPrimary"
        android:layout_alignParentTop="true"
        android:text=":"

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

        android:textColor="@color/colorPrimary"
        android:layout_alignParentTop="true"
        android:text="Heart Transplant."
        android:layout_marginLeft="5dp"
        android:layout_weight="0.50"

        />

</LinearLayout>

you can set text accoding to code

if you want to this in list than take only one row and set text according to your json.

if you take on layout than also add row according to your json with one row using Inflate function to your layout with addview method also...

if you want to do dynamically than do this

First take Row..

row.xml

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

<TextView
    android:id="@+id/lbl1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"

    android:textColor="@color/colorPrimary"
    android:layout_alignParentTop="true"
    android:text="1. Address"
    android:layout_weight="0.50"

    />

    <TextView

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:textColor="@color/colorPrimary"
        android:layout_alignParentTop="true"
        android:text=":"

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

        android:textColor="@color/colorPrimary"
        android:layout_alignParentTop="true"
        android:text="Sample address"
        android:layout_marginLeft="5dp"
        android:layout_weight="0.50"
        />

</LinearLayout>

Yourmain.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="1"
    android:orientation="vertical">

android:id="@+id/main"

ON Your code

.java file

for(int i=0;i<jsonarray.length;i++)
 {
 LinearLayout item = (LinearLayout )findViewById(R.id.main);
 View child = getLayoutInflater().inflate(R.layout.row, null);
 item.addView(child);

  TextView lbl1 = (TextView)child .findViewById(R.id.lbl1 );
  TextView tv1 = (TextView)child .findViewById(R.id.tv1 );



   if (mrecordsObj.has("website")) {

        if(!mrecordsObj.getString("website").isEmpty())
        lbl1.append("Website");
        tv1.append(mrecordsObj.getString("website"));


    }
   else if (mrecordsObj.has("extrainfo")) {


        lbl1.append("Extrainfo  ");
        tv1.append(mrecordsObj.getString("extrainfo"));


    }

 }

Or do according to you.. to do..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM