user8875509
user8875509

Reputation:

Align TextView on left and center

How do I exactly achieve this? I tried messing around with android:layout_gravity center|left and android:gravity center|left and none seems to work. Does it make any difference that I am using a spannable string?

Here is what I have right now and what I want to achive. I want my Text to align to the red line

Thanks in advance.

My Layout.. It is the TextView with the ID status

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >


    <!-- Main layout -->

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <FrameLayout
        android:id="@+id/main_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#f2f2f2">



            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center_horizontal"

                >


                <android.support.v4.widget.SwipeRefreshLayout
                    android:id="@+id/swiperefresh"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"

                    >


                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_gravity="center"
                        android:orientation="vertical"><![CDATA[
                        >

                        ]]>

                        <include
                            layout="@layout/actionbarmenu"
                            android:layout_width="match_parent"
                            android:layout_height="40dp"
                            android:layout_weight="0.64" />

                        <TextView
                            android:id="@+id/textView3"
                            android:layout_width="match_parent"
                            android:layout_height="30dp"
                            android:layout_marginLeft="5dp"
                            android:background="#FAFAFA"
                            android:gravity="center|left"
                            android:text="Notfallübersicht"
                            android:textColor="#c9c9c9"
                            android:textSize="18sp" />

                        <EditText
                            android:id="@+id/Notfall"
                            android:layout_width="match_parent"
                            android:layout_height="70dp"
                            android:layout_weight="0.36"
                            android:background="@drawable/round_grey"
                            android:ellipsize="end"
                            android:gravity="center"
                            android:hint="Notfall-Code Eingabe"
                            android:inputType="text"
                            android:paddingLeft="6dp"
                            android:singleLine="true"
                            android:textAllCaps="false"
                            android:textColor="#585858"
                            android:textColorHint="#c9c9c9"
                            android:textCursorDrawable="@drawable/black_cursor" />

                        <TextView
                            android:id="@+id/textView7"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.70" />

                        <TextView
                            android:id="@+id/status"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="3.63"
                            android:gravity="center"
                            android:textColor="#6f7070"
                            android:textSize="20sp"
                            android:visibility="visible" />

                        <TextView
                            android:id="@+id/abgemeldet"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="10.08"
                            android:text="Bitte melde dich mit deinen Pengueen Log-In Daten an, um die App zu aktivieren.  "
                            android:textSize="24sp" />

                        <TextView
                            android:id="@+id/user"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="5dp"
                            android:layout_weight="0.60"
                            android:text="Angemeldet als :"
                            android:textSize="14sp"
                            android:visibility="invisible" />

                        <TextView
                            android:id="@+id/angemeldet"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="5dp"
                            android:layout_weight="1.09"
                            android:text="Letzte Abfrage vor  1 Minute"
                            android:textSize="14sp"
                            android:textStyle="bold"
                            android:visibility="invisible" />

                        <Button
                            android:id="@+id/button"
                            style="@style/Widget.AppCompat.Button"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:background="#4697c4"
                            android:text="Anmleden"
                            android:textColor="#ffffff"
                            android:visibility="gone" />

                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="50dp"
                            android:layout_weight="0.74">

                            <!-- This could be your fragment container, or something -->
                            <FrameLayout
                                android:id="@+id/contentContainer"
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:layout_above="@+id/bottomBar" />

                            <com.roughike.bottombar.BottomBar
                                android:id="@+id/bottomBar"
                                android:layout_width="match_parent"
                                android:layout_height="60dp"
                                android:layout_alignParentBottom="true"
                                android:layout_gravity="bottom"
                                app2:bb_activeTabAlpha="1"
                                app2:bb_activeTabColor="#ffffff"
                                app2:bb_inActiveTabAlpha="0.6"
                                app2:bb_inActiveTabColor="#ffffff"
                                app2:bb_tabXmlResource="@xml/bottombar_tabs" />

                        </RelativeLayout>


                    </LinearLayout>
                </android.support.v4.widget.SwipeRefreshLayout>
            </LinearLayout>

    </FrameLayout>

    <!-- The navigation drawer -->

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#51aadd"
        android:choiceMode="singleChoice"
        android:divider="@drawable/list_divider"
        android:footerDividersEnabled="false"
        android:headerDividersEnabled="true"
        android:scrollbars="none" />



</android.support.v4.widget.DrawerLayout>

Upvotes: 1

Views: 1492

Answers (2)

Joey
Joey

Reputation: 23

  • android:gravity sets the gravity of the contents of the View .
  • android:layout_gravity sets the gravity of the View relative to its parent.

you can see Difference between gravity and layout_gravity on Android

android:gravity of TextView default value is left ,it is what you want , you just need the change the layout_gravity。

when set TextView android:layout_gravity="center_horizontal", you should set android:layout_width="wrap_content", not 'match_parent'。

android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"

Upvotes: 1

Ruann Reis
Ruann Reis

Reputation: 159

You will need this inside the desired textviews :

android:layout_width="wrap_content"
android:gravity="left"
android:layout_gravity="center_horizontal"

Upvotes: 0

Related Questions