Cannot set alignment to the right when using gravity

I want to set a label on the left side of floating action button. But so far i'm unable to set the text alignment to the right although i've already set the value in the 'gravity'. Currently it is still on the left alignment. How can i do this? Thank you.

<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="bottom|end"
        android:layout_marginBottom="100dp">

        <TextView
            android:id="@+id/textViewMedia"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="@string/media"
            android:layout_marginRight="16dp"
            android:visibility="invisible"
            android:gravity="right|center_vertical"
            android:textAppearance="?android:attr/textAppearanceMedium"/>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab_media"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/ic_menu_camera"
            android:tint="@android:color/white"
            android:visibility="invisible"
            android:layout_marginRight="@dimen/fab_margin"
            app:pressedTranslationZ="12dp"/>
    </LinearLayout>

1 answer

  • answered 2018-03-11 13:56 Karanjeet Singh

    Please try below written code, may be you want like this:

    <LinearLayout android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_gravity="bottom|end"
    android:layout_marginBottom="100dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">
    
    <TextView
        android:id="@+id/textViewMedia"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="media"
        android:layout_marginRight="16dp"
        android:visibility="visible"
        android:gravity="right|center_vertical"
        android:textAppearance="?android:attr/textAppearanceMedium"/>
    
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_media"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        app:srcCompat="@drawable/ic_menu_camera"
        android:tint="@android:color/white"
        android:visibility="invisible"
        android:layout_marginRight="@dimen/fab_margin"
        app:pressedTranslationZ="12dp"/>
     </LinearLayout>