I made this tableLayout, but as you can see, it has some margin that I don't know how to remove: https://i.sstatic.net/vE6fA.jpg Another problem is that the last row is open. This is my xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:id="@+id/slidelinearlayout">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/tl"
android:layout_width="170dp"
android:layout_height="180dp"
android:layout_marginLeft="750dp"
android:layout_marginTop="150dp"
android:layout_marginRight="0dp"
android:background="@android:color/white"
android:padding="16dp"
tools:context=".MainActivity">
<TableRow
android:id="@+id/tr_item_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/table_row_bg"
android:padding="5dp">
<TextView
android:id="@+id/item_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/table_cell_bg"
android:paddingRight="10dp"
android:text="Flessibilità" />
<TextView
android:id="@+id/f_item_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/table_cell_bg"
android:paddingLeft="10dp"
android:paddingRight="10dp" />
</TableRow>
<TableRow
android:id="@+id/tr_item_2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/table_row_bg"
android:padding="5dp">
<TextView
android:id="@+id/item_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/table_cell_bg"
android:paddingRight="10dp"
android:text="Fluidità" />
<TextView
android:id="@+id/fl_item_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/table_cell_bg"
android:paddingLeft="10dp"
android:paddingRight="10dp" />
</TableRow>
<TableRow
android:id="@+id/tr_item_3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/table_row_bg"
android:padding="5dp">
<TextView
android:id="@+id/item_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/table_cell_bg"
android:paddingRight="10dp"
android:text="Originalità" />
<TextView
android:id="@+id/o_item_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/table_cell_bg"
android:paddingLeft="10dp"
android:paddingRight="10dp" />
</TableRow>
<TableRow
android:id="@+id/tr_item_4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/table_row_bg"
android:padding="5dp">
<TextView
android:id="@+id/item_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/table_cell_bg"
android:paddingRight="10dp"
android:text="Elaborazione" />
<TextView
android:id="@+id/el_item_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/table_cell_bg"
android:paddingLeft="10dp"
android:paddingRight="10dp" />
</TableRow>
<TableRow
android:id="@+id/tr_item_5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/table_row_bg"
android:padding="5dp">
<TextView
android:id="@+id/item_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/table_cell_bg"
android:paddingRight="10dp"
android:text="Titolo" />
<TextView
android:id="@+id/t_item_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/table_cell_bg"
android:paddingLeft="10dp"
android:paddingRight="10dp" />
</TableRow>
</TableLayout>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/tl_2"
android:layout_width="300dp"
android:layout_height="130dp"
android:layout_marginLeft="750dp"
android:layout_marginTop="370dp"
android:background="@android:color/white"
android:padding="16dp"
tools:context=".MainActivity">
<TableRow
android:id="@+id/tr_item_6"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/table_row_bg"
android:padding="5dp">
<TextView
android:id="@+id/t1_item_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/table_cell_bg"
android:paddingRight="10dp"
android:text="Tempo di reazione" />
<TextView
android:id="@+id/tempo_item_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/table_cell_bg"
android:paddingLeft="10dp"
android:paddingRight="10dp" />
</TableRow>
<TableRow
android:id="@+id/tr_item_7"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/table_row_bg"
android:padding="5dp">
<TextView
android:id="@+id/t2_item_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/table_cell_bg"
android:paddingRight="10dp"
android:text="Tempo di completamento" />
<TextView
android:id="@+id/tempo_item_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/table_cell_bg"
android:paddingLeft="10dp"
android:paddingRight="10dp" />
</TableRow>
<TableRow
android:id="@+id/tr_item_8"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/table_row_bg"
android:padding="5dp">
<TextView
android:id="@+id/n_cancellature"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/table_cell_bg"
android:paddingRight="10dp"
android:text="Numero cancellature" />
<TextView
android:id="@+id/n_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/table_cell_bg"
android:paddingLeft="10dp"
android:paddingRight="10dp" />
</TableRow>
</TableLayout>
</RelativeLayout>
Do you know how can I solve that? As you can see al the tags are closed. I used the RelativeLayout to set the elements wherever I want in the screen, can that be part of the problem? PS: For the margins problem, I even tried to resize the table but nothing changed
You are not seeing your layout like you want because you are using fixed sizes on your views:
Different phones got different screen size, in your layout you are using fixed size on your view (fixed size is android:layout_marginTop="150dp"
for example) and the result is that what may look good on one screen (your android studio preview screen) will not look good on another screen (your actual phone).
If you want to see your table the same way on all devices you can use ConstraintLayout with guidelines to achieve your wanted look:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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">
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="TextView"
app:layout_constraintBottom_toTopOf="@+id/textView5"
app:layout_constraintEnd_toStartOf="@+id/textView4"
android:background="@color/cardview_dark_background"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3" />
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="@+id/textView5"
app:layout_constraintEnd_toEndOf="parent"
android:background="@color/cardview_dark_background"
app:layout_constraintStart_toEndOf="@+id/textView5"
app:layout_constraintTop_toTopOf="@+id/textView5" />
<TextView
android:id="@+id/textView3"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="1"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintEnd_toStartOf="@+id/textView6"
app:layout_constraintStart_toStartOf="parent"
android:background="@color/colorAccent"
app:layout_constraintTop_toTopOf="@+id/guideline" />
<TextView
android:id="@+id/textView4"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
android:background="@color/colorAccent"
app:layout_constraintStart_toEndOf="@+id/textView"
app:layout_constraintTop_toTopOf="@+id/textView" />
<TextView
android:id="@+id/textView5"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="TextView"
app:layout_constraintBottom_toTopOf="@+id/guideline2"
android:background="@color/colorAccent"
app:layout_constraintEnd_toStartOf="@+id/textView2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<TextView
android:id="@+id/textView6"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="2"
app:layout_constraintBottom_toBottomOf="@+id/textView3"
app:layout_constraintEnd_toEndOf="parent"
android:background="@color/cardview_dark_background"
app:layout_constraintStart_toEndOf="@+id/textView3"
app:layout_constraintTop_toTopOf="@+id/textView3" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent=".3" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent=".5" />
</androidx.constraintlayout.widget.ConstraintLayout>
It will look like this: (the arrow points to the guidelines for better understanding)
Now, I know that at first look this may look like a lot of work and some may wonder if this is really worth the effort but here is why I believe ConstraintLayout is the proper way to build your UI:
It's really user-friendly.
ConstraintLayout is very easy and simple to learn.
once you have learned it you will see that you are saving a lot of development time because making your UI is simply fast.
Constraint layout is meant to support different screen sizes so no need to build a layout for every screen size.