Al Lelopath
Al Lelopath

Reputation: 6778

Positioning components within a LinearLayout

I am unable to precisely position a component in a LinearLayout. The LinearLayout is horizontal (ImageButton-TextView-ImageButton). The image below shows the problem. The numbers (the TextView) are not centered vertically or horizontally with respect to the ImageButtons.(Red lines indicating centers). As you can see in the code, I have the TextView component layout set to center|center. The numbers are always between 0 and 99.

enter image description here

    <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_gravity="center_horizontal"
    android:layout_margin="25dip"
    android:layout_weight="1" >

    <ImageButton
        android:id="@+id/game4PreviousButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        android:background="@null"
        android:contentDescription="@string/previousbutton"
        android:enabled="false"
        android:onClick="clickCheck"
        android:src="@drawable/previousbutton" />

    <TextView
        android:id="@+id/game4Step"
        android:layout_width="80dip"
        android:layout_height="wrap_content"
        android:layout_gravity="center|center"
        android:layout_marginLeft="75dp"
        android:layout_marginRight="75dp"
        android:contentDescription="@string/game4step1"
        android:text=""
        android:textColor="#FFFFFF"
        android:textSize="50sp"
        android:textStyle="bold" />

    <ImageButton
        android:id="@+id/game4NextButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        android:background="@null"
        android:contentDescription="@string/nextbutton"
        android:enabled="true"
        android:onClick="clickNext"
        android:src="@drawable/nextbutton" />
</LinearLayout>

Upvotes: 0

Views: 1053

Answers (2)

kandroidj
kandroidj

Reputation: 13932

I Assume that the LinearLayout is not the xml parent element since there is no <?xml /> directly above it, or maybe you forgot it. But

I believe you are looking for the following :

<TextView
    android:id="@+id/game4Step"
    android:layout_width="80dip"
    android:layout_height="match_parent"
    android:layout_gravity="center_vertical"
    android:layout_marginLeft="75dp"
    android:layout_marginRight="75dp"
    android:contentDescription="@string/game4step1"
    android:text=""
    android:textColor="#FFFFFF"
    android:textSize="50sp"
    android:textStyle="bold" />

Also change LinearLayout to

android:layout_height="wrap_content"

and

android:layout_weight="1" can be removed.

and make both IMageButtons

android:layout_height="match_parent"

Upvotes: 1

Hariharan
Hariharan

Reputation: 24853

Try this..

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="horizontal" >

            <ImageButton
                android:id="@+id/game4PreviousButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="4dp"
                android:background="@null"
                android:contentDescription="@string/previousbutton"
                android:enabled="false"
                android:gravity="center"
                android:onClick="clickCheck"
                android:src="@drawable/previousbutton" />
        </LinearLayout>

        <TextView
            android:id="@+id/game4Step"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:contentDescription="@string/game4step1"
            android:gravity="center"
            android:text="01"
            android:textSize="50sp"
            android:textStyle="bold" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="horizontal" >

            <ImageButton
                android:id="@+id/game4NextButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="4dp"
                android:background="@null"
                android:contentDescription="@string/nextbutton"
                android:enabled="true"
                android:gravity="center"
                android:onClick="clickNext"
                android:src="@drawable/nextbutton" />
        </LinearLayout>
    </LinearLayout>

Upvotes: 1

Related Questions