androidandroid-layouttextviewcenter-align

Center-align TextView on top of ListFragment with custom layout


In a simple test app at GitHub I am trying to display a user avatar, then two TextViews Username and City (which should be center-aligned) and finally the main ListView of the ListFragment:

screenshot

In the layout file fragment_account.xml I try to set android:textAlignment="center" for the both TextViews (marked by the red arrow in the above screenshot), but that does not have any effect - maybe because the app is for minSdkLevel=9?

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/avatar"
        android:layout_width="96dp"
        android:layout_height="96dp"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/photo"
        app:border_width="2dp"
        app:border_color="#FFF"/>

    <TextView                     <!-- how to center-align? -->
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:text="Username"/>

    <TextView                     <!-- how to center-align? -->
        android:id="@+id/city"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:text="City"/>

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ListView>

</LinearLayout>

Please advise, if there is a nice way to center-align those labels.


Solution

  • Try this

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/avatar"
            android:layout_width="96dp"
            android:layout_height="96dp"
            android:layout_gravity="center_horizontal"
            android:src="@drawable/photo"
            app:border_width="2dp"
            app:border_color="#FFF"/>
    
              <!-- add android:gravity="center" -->
        <TextView                     
            android:id="@+id/username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center" 
            android:text="Username"/>
    
        <TextView   
            android:id="@+id/city"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center" 
            android:text="City"/>
    
        <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </ListView>
    
    </LinearLayout>