3

I can't make my Android TableLayout works, it's going out of the screen. I tried layout:weight and android:shrinkColumns="*".

My xml:

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="dindz.oso.MainActivity">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:theme="@style/ThemeOverlay.AppCompat.Dark" />

<TableLayout
    android:id="@+id/tl"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/toolbar"
    android:gravity="center_horizontal"
    android:shrinkColumns="*"
    android:stretchColumns="0,1,2,3">

    <TableRow
        android:id="@+id/header_tabeli"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="1dp"
        android:background="#000000"
        android:gravity="center">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_column="0"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text=" Data\nstartu "
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textStyle="bold"
            android:layout_weight="1"/>

        <TextView
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_column="1"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="Stacja"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textStyle="bold" />

        <TextView
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_column="2"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="Rodzaj"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textStyle="bold" />

        <TextView
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_column="3"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="Poziom"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textStyle="bold" />
    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_margin="1dp"
        android:layout_weight="1"
        android:background="#000000">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_column="0"
            android:layout_margin="1dp"
            android:layout_weight="1"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="data 2301.23.23 11:23 ksajd"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_column="1"
            android:layout_margin="1dp"
            android:layout_weight="1"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="stacja wrocław ul. długiego 32"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_column="2"
            android:layout_margin="1dp"
            android:layout_weight="1"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="rodzaj no2"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_column="3"
            android:layout_margin="1dp"
            android:layout_weight="1"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="4"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </TableRow>


</TableLayout>

<EditText
    android:id="@+id/editText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tl"
    android:layout_centerHorizontal="true"
    android:ems="10"
    android:inputType="textPersonName"
    android:text="Brak aktywnych alarmów"
    android:visibility="invisible" />

Screenshots:

What I want

The difference between those images is only shorter/longer text.

I was trying some combinations with wrap_content/match_parent, but it didn't help me.

Can you help me? Thanks in advance.

EDIT to ysl's answer: the result of your code

Rob
  • 2,243
  • 4
  • 29
  • 40
Maciek
  • 45
  • 5

2 Answers2

2

Try following code might be helpful :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:theme="@style/ThemeOverlay.AppCompat.Dark" />

    <TableLayout
        android:id="@+id/tl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toolbar"
        android:collapseColumns=" Data\nstartu "
        android:gravity="center"
        android:shrinkColumns="*"
        android:stretchColumns="*">

        <TableRow
            android:id="@+id/header_tabeli"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="#000000"
            android:gravity="center">

            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_column="0"
                android:background="#FFFFFF"
                android:gravity="center"
                android:text="Stacja ugugugugugug "
                android:textStyle="bold" />


            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_column="1"
                android:layout_margin="1dp"
                android:background="#FFFFFF"
                android:gravity="center"
                android:text="Stacja ugugugugugug"
                android:textStyle="bold" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_column="2"
                android:layout_margin="1dp"
                android:background="#FFFFFF"
                android:gravity="center"
                android:text="Rodzaj"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="3"
                android:layout_margin="1dp"
                android:background="#FFFFFF"
                android:gravity="center"
                android:text="Poziom"
                android:textStyle="bold" />
        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:background="#000000">

            <TextView
                android:id="@+id/textView2"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_column="0"
                android:layout_margin="1dp"
                android:background="#FFFFFF"
                android:gravity="center"
                android:text="stacja Data\nstartu  " />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="1"
                android:layout_margin="1dp"
                android:background="#FFFFFF"
                android:gravity="center"
                android:text="stacja Data\nstartu  " />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_column="2"
                android:layout_margin="1dp"
                android:background="#FFFFFF"
                android:gravity="center"
                android:text="rodzaj no2" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_column="3"
                android:layout_margin="1dp"
                android:background="#FFFFFF"
                android:gravity="center"
                android:text="4" />
        </TableRow>


    </TableLayout>

</RelativeLayout>
yogesh lokhande
  • 1,245
  • 1
  • 11
  • 20
  • I just changed text to longer in your code in 2nd textview of 2nd TableRow. Doesn't work (screen above in EDIT). Still thanks for Your post. – Maciek Sep 27 '17 at 11:09
  • Change android:stretchColumns="0,1,2,3" to android:stretchColumns="*" – yogesh lokhande Sep 27 '17 at 11:27
  • I simply modify your answer (changed all TV width to 0dp) and it did the trick. Thank you ysl. TableRow: android:layout_width="0dp" android:layout_height="wrap_content" TextView: android:layout_width="0dp" android:layout_height="wrap_content" I can't up-vote you :/ – Maciek Sep 27 '17 at 11:28
  • If you found my efforts useful then please accept my answer – yogesh lokhande Sep 27 '17 at 11:41
0

Working code if someone needs:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:theme="@style/ThemeOverlay.AppCompat.Dark" />

    <TableLayout
        android:id="@+id/tl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_below="@+id/toolbar"
        android:layout_alignParentStart="true">
<!--        android:shrinkColumns="*"
        android:stretchColumns="*"
        android:collapseColumns="*"-->

        <TableRow
            android:id="@+id/header_tabeli"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="#000000"
            android:gravity="center">

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_weight="2"
                android:layout_margin="1dp"
                android:background="#FFFFFF"
                android:gravity="center"
                android:text="Data\nstartu"
                android:textStyle="bold" />


            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_column="1"
                android:layout_margin="1dp"
                android:layout_weight="4"
                android:background="#FFFFFF"
                android:gravity="center"
                android:text="Stacja"
                android:textStyle="bold" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_column="2"
                android:layout_margin="1dp"
                android:layout_weight="1"
                android:background="#FFFFFF"
                android:gravity="center"
                android:text="Rodzaj"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/textView"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_column="3"
                android:layout_margin="1dp"
                android:layout_weight="1"
                android:background="#FFFFFF"
                android:gravity="center"
                android:text="Poziom"
                android:textStyle="bold" />
        </TableRow>

        <TableRow
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:background="#000000">

            <TextView
                android:id="@+id/textView2"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_column="0"
                android:layout_margin="1dp"
                android:layout_weight="2"
                android:background="#FFFFFF"
                android:gravity="center"
                android:text="9/21/2017 1:52:00 PM" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_column="1"
                android:layout_margin="1dp"
                android:layout_weight="4"
                android:background="#FFFFFF"
                android:gravity="center"
                android:text="Wrocław, ul. Wyb. J.Conrada-Korzeniowskiego 18" />
            <!--Wrocław, ul. Wyb. J.Conrada-Korzeniowskiego 18-->

            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_column="2"
                android:layout_margin="1dp"
                android:layout_weight="1"
                android:background="#FFFFFF"
                android:gravity="center"
                android:text="no2" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_column="3"
                android:layout_margin="1dp"
                android:layout_weight="1"
                android:background="#FFFFFF"
                android:gravity="center"
                android:text="4" />
        </TableRow>


    </TableLayout>

</RelativeLayout>
Maciek
  • 45
  • 5