0

I am having a very hard time justifying TextViews to the left in Android. I looked here but did not have success. The TextViews I want to justify are in Linear Layouts nested in a parent Linear Layout:

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal">

        <Button
            android:text="New Button"
            android:id="@+id/button1"
            style="@style/calibrate_button" />

        <TextView
            android:text="@string/calibrate"
            android:id="@+id/textView3"
            style="@style/calibrate_text" />
    </LinearLayout>

Style: @color/White 20sp wrap_content wrap_content ?android:attr/textAppearanceLarge right

<style name="calibrate_button">
    <item name="android:background">#fcfcfc</item>
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
</style>
Community
  • 1
  • 1
Soatl
  • 10,224
  • 28
  • 95
  • 153
  • 2
    Why not use RelativeLayout? – Raghunandan Jun 03 '14 at 15:39
  • I thought that a horizontal LinearLayout would be the way to go in this situation. I have a button and textview that I want to line up horizontally. I will look into a RelativeLayout though. – Soatl Jun 03 '14 at 15:42
  • What are you trying to do? That xml will create a button on the left and a TextView on its right – Gil Vegliach Jun 03 '14 at 15:44
  • I want the button to be on the left of the screen and the textview on the right. This creates a button and textview on the left side of the screen – Soatl Jun 03 '14 at 15:45
  • Then you should use a RelativeLayout, this is what it is for: it has attribute to align things relative to their parent or other things. Anyway why the layout_gravity attribute? Maybe you meant gravity? – Gil Vegliach Jun 03 '14 at 15:47

1 Answers1

2

Lik Gil said, you can use this way :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal" >

    <Button
        android:id="@+id/button1"
        style="@style/calibrate_button"
        android:layout_alignParentLeft="true"
        android:text="New Button" />

    <TextView
        android:id="@+id/textView3"
        style="@style/calibrate_text"
        android:text="@string/calibrate"
        android:layout_alignParentRight="true" />

</RelativeLayout>
Farouk Touzi
  • 3,451
  • 2
  • 19
  • 25
  • As I commented above, I don't know how the OP use this layout, but `layout_gravity="center_horizontal"` here is not needed. This will center the layout in the parent (and there is not parent in this example). To center the buttons *inside* the layout, use `gravity="center_horizontal"` – Gil Vegliach Jun 03 '14 at 15:56
  • He said that he wants to justify a TextView wich is in Linear Layout nested in a parent Linear Layout. – Farouk Touzi Jun 03 '14 at 16:04