Pallavi Sharma
Pallavi Sharma

Reputation: 655

how to add button on center of view?

I am trying to add button in center (in between the view) but when I add it just below the Textview and AutoCompleteTextView, may I give padding or margin hardcoded? I need "ok" button in between the view (horizontal and vertical center).

Layout XML below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Choose station"
            android:layout_marginLeft="20dp"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <AutoCompleteTextView
            android:id="@+id/item_autoComplete"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:ems="10"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:text="" >

            <requestFocus />
        </AutoCompleteTextView>

    </LinearLayout>
<Button
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:text="ok"
     android:id="@+id/item_buttonId"/>
</LinearLayout>

Upvotes: 0

Views: 79

Answers (2)

Dhinakaran Thennarasu
Dhinakaran Thennarasu

Reputation: 3356

android:layout_gravity="center"


   <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

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

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Choose station"
                  android:layout_marginLeft="20dp"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <AutoCompleteTextView
                android:id="@+id/item_autoComplete"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:ems="10"
                android:layout_marginLeft="20dp"
                 android:layout_marginRight="20dp"
                android:text="" >

                <requestFocus />
            </AutoCompleteTextView>

        </LinearLayout>
    <Button
         android:layout_height="wrap_content"
         android:layout_width="wrap_content"
         android:text="ok"
    android:layout_gravity="center"
         android:id="@+id/item_buttonId"/>
    </LinearLayout>

Upvotes: 1

Remees M Syde
Remees M Syde

Reputation: 2609

I think your looking for this.Choose some margin or use RelativeLayout. Result:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Choose station"
              android:layout_marginLeft="20dp"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <AutoCompleteTextView
            android:id="@+id/item_autoComplete"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:ems="10"
            android:layout_marginLeft="20dp"
             android:layout_marginRight="20dp"
             android:layout_marginBottom="100dp"
            android:text="" >

            <requestFocus />
        </AutoCompleteTextView>

    </LinearLayout>
<Button
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:text="ok"
     android:layout_gravity="center"
     android:id="@+id/item_buttonId"/>
</LinearLayout>

Upvotes: 0

Related Questions