1

I'm a pretty big noob when it comes to android and I'm stuck in a situation. How it should be

enter image description here

I want my table layout to go under my button located in the linear layout but it just overlaps.

Eventually I will have to be able to click the button to hide the table entirely.

    <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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="eu.agp_it.agpmobilemover.OverviewActivity">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:weightSum="1.0"
        android:gravity="center"
        android:id="@+id/layout_button_today"
        >
        <Button
            android:id="@+id/overview_button_today"
            android:layout_height="wrap_content"
            android:layout_weight="0.95"
            android:layout_width="0dip"
            android:textSize="20sp"
            android:text="@string/overview_today"
            android:gravity="center"/>
    </LinearLayout>

<TableLayout
    android:layout_width="20sp"
    android:layout_height="fill_parent"
    android:visibility="visible"
    >
    <TableRow>
        <TextView android:id="@+id/label_depart" android:text="@string/overview_depart_time"/>
        <TextView />
    </TableRow>

</TableLayout>
    <LinearLayout
        android:id="@+id/layout_button_upcoming"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:weightSum="1.0"
        android:gravity="center"
        android:layout_above="@id/layout_button_today">
        <Button
            android:id="@+id/overview_button_upcoming"
            android:layout_height="wrap_content"
            android:layout_weight="0.95"
            android:layout_width="0dip"
            android:textSize="20sp"
            android:text="@string/overview_upcoming"
            android:gravity="center"/>
    </LinearLayout>
</RelativeLayout>
2
  • Use LinearLayout as root or set the layout_above/layout_below correctly.
    – stefana
    Mar 1, 2016 at 10:55
  • use LinearLayout instead of RelativeLayout and "fill_parent" is deprecated, if you use the LinearLayout then android:layout_height="0dp" android:layout_weight="1" in TableLayout Mar 1, 2016 at 11:00

5 Answers 5

0

Take two table as

<TableRow>
    </TableRow> 

<TableRow>
        </TableRow> 

inside TableLayout in which 1st table contain your button and 2nd table contains your linearlayout view. Also as you wish you can hide 2nd table row as desire on button click.

0

I think that you have 2 buttons and a tableLayut. one of the buttons is in top of page and another in bottom of page. I suggested this code:

<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:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        tools:context="eu.agp_it.agpmobilemover.OverviewActivity">
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:weightSum="1.0"
            android:gravity="center"
            android:id="@+id/layout_button_today">
            <Button
                android:id="@+id/overview_button_today"
                android:layout_height="wrap_content"
                android:layout_weight="0.95"
                android:layout_width="0dip"
                android:textSize="20sp"
                android:text="@string/overview_today"
                android:gravity="center"/>
        </LinearLayout>

    <TableLayout
        android:layout_width="20sp"
        android:layout_height="fill_parent"
        android:layout_below="@+id/layout_button_today"
        android:layout_above="@+id/layout_button_upcoming"
        android:visibility="visible" >
        <TableRow>
            <TextView android:id="@+id/label_depart"
                   android:text="@string/overview_depart_time"/>
            <TextView />
        </TableRow>

    </TableLayout>
        <LinearLayout
            android:id="@+id/layout_button_upcoming"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:weightSum="1.0"
            android:gravity="center"
            android:layout_above="@id/layout_button_today">
            <Button
                android:id="@+id/overview_button_upcoming"
                android:layout_height="wrap_content"
                android:layout_weight="0.95"
                android:layout_width="0dip"
                android:textSize="20sp"
                android:text="@string/overview_upcoming"
                android:gravity="center"/>
        </LinearLayout>
    </RelativeLayout>
2
  • Yeah, I do have two buttons, I forgot to delete the lower one for this question. My plan is to show one table when the first button is pressed and one when the other is pressed.
    – Madgarr
    Mar 1, 2016 at 11:53
  • I suggested you use RecyclerView or ListView and add data dynamicely after clicking buttons. then you can notify your adapter to change list. Mar 1, 2016 at 12:20
0

Replace your TableLayout with this one.

<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="eu.agp_it.agpmobilemover.OverviewActivity">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:weightSum="1.0"
        android:gravity="center"
        android:id="@+id/layout_button_today">
        <Button
            android:id="@+id/overview_button_today"
            android:layout_height="wrap_content"
            android:layout_weight="0.95"
            android:layout_width="0dip"
            android:textSize="20sp"
            android:text="@string/overview_today"
            android:gravity="center"/>
    </LinearLayout>

    <TableLayout
        android:id="@+id/my_table_layout"
        android:layout_width="20sp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/layout_button_today"
        android:visibility="visible" >
        <TableRow>
            <TextView android:id="@+id/label_depart"
                android:text="@string/overview_depart_time"/>
            <TextView />
        </TableRow>

    </TableLayout>
    <LinearLayout
        android:id="@+id/layout_button_upcoming"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:weightSum="1.0"
        android:gravity="center"
        android:layout_below="@id/my_table_layout">
        <Button
            android:id="@+id/overview_button_upcoming"
            android:layout_height="wrap_content"
            android:layout_weight="0.95"
            android:layout_width="0dip"
            android:textSize="20sp"
            android:text="@string/overview_upcoming"
            android:gravity="center"/>
    </LinearLayout>
</RelativeLayout>
1
  • Did you only add the layout_below? Because if you did, I tried that and it didn't work and your solution did. Thanks!
    – Madgarr
    Mar 1, 2016 at 11:13
0

Set parent layout as LinearLayout. Orientation as vertical. Also set visibility of table layout gone initially in your layout. In the code , set Visibility visible in your on click method

tableLayout.setVisibility(View.VISIBLE);

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_margin="10dp"
    android:layout_gravity="center">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="Button"/>

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="+@id/tableLayout"
    android:visibility="gone"
    android:layout_margin="10dp">

    <TableRow>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
           android:text="text1"/>
    </TableRow>

    <TableRow>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="text2"/>
    </TableRow>

    </TableLayout>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="Button"/>

    </LinearLayout>
0

You can use under code :

    <LinearLayout
        android:id="@+id/layout_button_today"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:weightSum="1.0" >

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".05" />

            <Button
                android:id="@+id/overview_button_today"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight=".90"
                android:gravity="center"
                android:text="@string/overview_today"
                android:textSize="20sp" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight=".05" />



    </LinearLayout>

 <TableLayout
        android:layout_width="20sp"
        android:layout_height="fill_parent"
        android:layout_below="@+id/layout_button_today"
        android:layout_above="@+id/layout_button_upcoming"
        android:visibility="visible" >
        <TableRow>
            <TextView android:id="@+id/label_depart"
                   android:text="@string/overview_depart_time"/>
            <TextView />
        </TableRow>

    </TableLayout>
        <LinearLayout
            android:id="@+id/layout_button_upcoming"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:weightSum="1.0"
            android:gravity="center"
            android:layout_above="@id/layout_button_today">
            <Button
                android:id="@+id/overview_button_upcoming"
                android:layout_height="wrap_content"
                android:layout_weight="0.95"
                android:layout_width="0dip"
                android:textSize="20sp"
                android:text="@string/overview_upcoming"
                android:gravity="center"/>
        </LinearLayout>

    </RelativeLayout>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.