0

I am trying to design my fragment and i would like to create that the text will be exactly above the button and in the center. for now i just created a 2 RowTables. but i still not succeed to align it as i want.

this is the XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mainSearchRelativeLayout"
tools:context="com.example.matant.gpsportclient.Controllers.SearchEventFragmentController"
android:background="#ffffff">

<!-- TODO: Update blank fragment layout -->

<RelativeLayout
    android:layout_width="match_parent"
    android:id="@+id/secondSearchLayout"
    android:layout_height="wrap_content">

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/radioGroupSearchFragment">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Search by current location"
            android:id="@+id/radioButtonSearchCurrentLocation"
            android:checked="true" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Search by specific address"
            android:id="@+id/radioButtonSearchSpecificAdd"
            android:checked="false" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPostalAddress"
            android:ems="10"
            android:id="@+id/editText"
            android:hint="Please insert real Street address" />


    </RadioGroup>
    <View
        android:id="@+id/divider1"
        android:layout_width="fill_parent"
        android:layout_height="6dp"
        android:background="@android:color/darker_gray"
        android:layout_below="@+id/radioGroupSearchFragment"
        />

    <ScrollView
        android:layout_width="match_parent"
        android:id="@+id/searchScrollView"
        android:layout_below="@id/divider1"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:layout_width="match_parent"
            android:id="@+id/mandatorySearchLayout"
            android:layout_below="@+id/divider1"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Event Date Ranges"
                android:id="@+id/textViewEventDateRange"
                android:textColor="#000000"
                android:textStyle="bold"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/textViewSearchMandatory1" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="From"
                android:id="@+id/buttonSearchEventFrom"
                android:layout_below="@+id/textViewEventDateRange"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="To"
                android:id="@+id/buttonSearchEventTo"
                android:layout_below="@+id/buttonSearchEventFrom"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="*All the fields are mandatory"
                android:id="@+id/textViewSearchMandatory1"
                android:textColor="#FF0000"
                android:layout_alignParentTop="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true" />
            <TableRow
                android:layout_width="match_parent"
                android:id="@+id/row1"
                android:layout_below="@+id/buttonSearchEventTo"
                android:layout_height="wrap_content">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Start in"
                    android:layout_marginLeft="50dp"
                    android:id="@+id/textView6" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="End in"
                    android:layout_marginLeft="150dp"
                    android:id="@+id/textView7" />
            </TableRow>

            <TableRow
                android:layout_width="match_parent"
                android:id="@+id/row2"
                android:layout_below="@id/row1"
                android:layout_height="match_parent">

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="New Button"
                    android:layout_marginLeft="20dp"
                    android:id="@+id/button2" />

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="New Button"
                    android:layout_marginLeft="70dp"
                    android:id="@+id/button" />
            </TableRow>

        </RelativeLayout>


    </ScrollView>



    <View
        android:id="@+id/divider2"
        android:layout_width="fill_parent"
        android:layout_height="6dp"
        android:background="@android:color/darker_gray"
        android:layout_below="@+id/searchScrollView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="70dp" />

</RelativeLayout>

and this is what i got:

enter image description here

Matan Tubul
  • 774
  • 3
  • 11
  • 33

1 Answers1

1

As you haven't underline, which buttons and textViews you want to align, I suggested, that you want to get something like this:

enter image description here

To do this, I'd advocate to get rid of TableRows and replace them with, for example, LinearLayouts:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mainSearchRelativeLayout"
tools:context="com.example.matant.gpsportclient.Controllers.SearchEventFragmentController"
android:background="#ffffff">

<!-- TODO: Update blank fragment layout -->

<RelativeLayout
    android:layout_width="match_parent"
    android:id="@+id/secondSearchLayout"
    android:layout_height="wrap_content">

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/radioGroupSearchFragment">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Search by current location"
            android:id="@+id/radioButtonSearchCurrentLocation"
            android:checked="true" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Search by specific address"
            android:id="@+id/radioButtonSearchSpecificAdd"
            android:checked="false" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPostalAddress"
            android:ems="10"
            android:id="@+id/editText"
            android:hint="Please insert real Street address" />


    </RadioGroup>
    <View
        android:id="@+id/divider1"
        android:layout_width="fill_parent"
        android:layout_height="6dp"
        android:background="@android:color/darker_gray"
        android:layout_below="@+id/radioGroupSearchFragment"
        />

    <ScrollView
        android:layout_width="match_parent"
        android:id="@+id/searchScrollView"
        android:layout_below="@id/divider1"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:layout_width="match_parent"
            android:id="@+id/mandatorySearchLayout"
            android:layout_below="@+id/divider1"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Event Date Ranges"
                android:id="@+id/textViewEventDateRange"
                android:textColor="#000000"
                android:textStyle="bold"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/textViewSearchMandatory1" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="From"
                android:id="@+id/buttonSearchEventFrom"
                android:layout_below="@+id/textViewEventDateRange"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="To"
                android:id="@+id/buttonSearchEventTo"
                android:layout_below="@+id/buttonSearchEventFrom"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="*All the fields are mandatory"
                android:id="@+id/textViewSearchMandatory1"
                android:textColor="#FF0000"
                android:layout_alignParentTop="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true" />

            <LinearLayout
                android:layout_below="@+id/buttonSearchEventTo"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <LinearLayout
                    android:orientation="vertical"
                    android:layout_weight="1"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Start in"
                        android:id="@+id/textView6" />
                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="New Button"
                        android:id="@+id/button2" />
                </LinearLayout>
                <LinearLayout
                    android:orientation="vertical"
                    android:layout_weight="1"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="End in"
                        android:id="@+id/textView7" />
                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="New Button"
                        android:id="@+id/button" />
                </LinearLayout>
            </LinearLayout>

        </RelativeLayout>


    </ScrollView>



    <View
        android:id="@+id/divider2"
        android:layout_width="fill_parent"
        android:layout_height="6dp"
        android:background="@android:color/darker_gray"
        android:layout_below="@+id/searchScrollView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="70dp" />

</RelativeLayout>

The last thing then to think of - to remove the default button's shadow around, to make them fully align - this question is covered here - How to remove padding around buttons in android?

I hope, it helps

Community
  • 1
  • 1
Konstantin Loginov
  • 15,802
  • 5
  • 58
  • 95