user2706686
user2706686

Reputation: 77

How to show text with central align

how to show textview like this image?

mockup

my textview will look like this image

screenshot

below is my code, it's not showing text centrally:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="vertical" >

  <RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/imagelogo2"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/test_button_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:paddingTop="20dp"
        android:src="@drawable/back" >
    </ImageView>

    <ImageView
        android:id="@+id/test_button_image2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:paddingLeft="5dp"
        android:paddingRight="15dp"
        android:paddingTop="15dp"
        android:src="@drawable/options1" />
  </RelativeLayout>

  <TextView
      android:id="@+id/text"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:gravity="center"
      android:text="If your school is not in the list.Please check" />

  <TextView
      android:id="@+id/text"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:gravity="center"
      android:text="back later or contact your district&apos;s Nutrition Services" />

  <TextView
      android:id="@+id/text"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:gravity="center"
      android:text="department for more information" />

</LinearLayout>

Upvotes: 1

Views: 646

Answers (4)

Armaan Stranger
Armaan Stranger

Reputation: 3130

Instead of using 3 textviews you should use one textview.

and the text that you want to show, had to convert it to HTML.

String code = "<p><b>First, </b><br/>" +
                "Please press the <img src ='addbutton.png'> button beside the to insert a new event.</p>" +
                "<p><b>Second,</b><br/>" +
                "Please insert the details of the event.</p>"
                "<p>The icon of the is show the level of the event.<br/>" +
                "eg: <img src = 'tu1.png' > is easier to do.</p></td>";

        message = (TextView) findViewById (R.id.message);       
        Spanned spanned = Html.fromHtml(code, this, null);
        message.setText(spanned);
        message.setTextSize(16);

and make textview gravity to center.

For Times New Roman Fonts you need to do this:

  • create one folder "fonts" in assets folder
  • copy times new roman.ttf file in "fonts" folder. (times fonts file should be in small caps)

then add this class to your activity class:

public class Typefaces{

        private final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();

            public Typeface get(Context c, String name){
                synchronized(cache){
                    if(!cache.containsKey(name)){
                        Typeface t = Typeface.createFromAsset(
                                c.getAssets(), 
                                String.format("fonts/%s.ttf", name)
                            );
                        cache.put(name, t);
                    }
                    return cache.get(name);
                }
            }

        }

then try this code:

Typefaces tf = new Typefaces();
tvMessage.setTypeface(tf.get(context, "timesnewroman"));

Hope it Helps!!

Upvotes: 0

Hariharan
Hariharan

Reputation: 24853

Try this...

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff"
    android:orientation="vertical" 
    android:gravity="center"

    >

        <TextView 
          android:id="@+id/text" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:gravity="center"
          android:textStyle="italic"
          android:layout_margin="5dp"
          android:text="If your school is not in the list.Please check"
            />

        <TextView 
          android:id="@+id/text" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:gravity="center"        
          android:layout_margin="5dp"
          android:textStyle="italic"
          android:text="back later or contact your district's Nutrition Services"
            />

        <TextView 
          android:id="@+id/text" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:gravity="center"        
          android:layout_margin="5dp"
          android:textStyle="italic"
          android:text="department for more information"
            />

Upvotes: 0

user1952459
user1952459

Reputation:

You just need to replace

android:layout_width = "wrap_content"

with

android:layout_width="fill_parent"

for all the TextView. Try the layout below:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="vertical" >

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/imagelogo2"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/test_button_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:paddingTop="20dp"
        android:src="@drawable/back" >
    </ImageView>

    <ImageView
        android:id="@+id/test_button_image2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:paddingLeft="5dp"
        android:paddingRight="15dp"
        android:paddingTop="15dp"
        android:src="@drawable/options1" />
</RelativeLayout>

<TextView
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="If your school is not in the list.Please check" />

<TextView
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="back later or contact your district&apos;s Nutrition Services" />

<TextView
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="department for more information" />

Upvotes: 0

Ken Wolf
Ken Wolf

Reputation: 23279

The problem is all your 3 TextViews are different widths, so "center" is different for each of them.

Change them all to:

android:layout_width="match_parent" 

Upvotes: 1

Related Questions