0

I've been trying to get the chronometer (child of textview) to center in this horizontal linear layout for a few hours. I've looked on here and saw everyone mentioning the gravity attribute. I changed this and experimented with other attributes to no success. Below is the XML from android studio. What am I doing wrong?

    <LinearLayout
    android:id="@+id/statusDisplay"
    android:layout_width="match_parent"
    android:layout_height="20dp"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/score"
        android:layout_width="wrap_content"
        android:layout_height="20dp"
        android:gravity="left"
        android:text="Score" />

    <Chronometer
        android:id="@+id/timer"
        android:layout_width="wrap_content"
        android:layout_height="20dp"
        android:gravity="center_vertical" />

</LinearLayout>
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
roboNerd
  • 1
  • 2

2 Answers2

0

Can you try this?

<LinearLayout
     android:id="@+id/statusDisplay"
     android:layout_width="match_parent"
     android:layout_height="20dp"
     android:orientation="horizontal"
     android:gravity="center">

     <TextView
        android:id="@+id/score"
        android:layout_width="wrap_content"
        android:layout_height="20dp"
        android:text="Score" />

    <Chronometer
        android:id="@+id/timer"
        android:layout_width="wrap_content"
        android:layout_height="20dp" />
</LinearLayout>

Otherwise, You can use RelativeLayout

<RelativeLayout
    android:id="@+id/statusDisplay"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="20dp">

    <TextView
        android:id="@+id/score"
        android:layout_width="wrap_content"
        android:layout_height="20dp"
        android:text="Score"/>

    <Chronometer
        android:id="@+id/timer"
        android:layout_width="wrap_content"
        android:layout_height="20dp"
        android:layout_centerHorizontal="true"/>
</RelativeLayout>
0

Either use Gravity property in the parent layout. Or use a RelativeLayout as a parent instead and use attribute layout:center_Horizontal ="true" on the parent.