Search code examples
androidtablelayout

Android. TableLayout does not displayed. ImageView problem


I am new in Android and somewhere didnt understand how it works. I has this XML code

<?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:id="@+id/mainWindow"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableLayout
        android:id="@+id/katalog"
        android:layout_width="388dp"
        android:layout_height="179dp"
        app:layout_constraintBottom_toTopOf="@+id/button5"
        app:layout_constraintStart_toStartOf="parent">

        <TableRow
            android:id="@+id/row"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView                                  // problem is here
                android:id="@+id/imageView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                tools:srcCompat="@tools:sample/avatars" />

            <LinearLayout
                android:id="@+id/linear"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/textView11"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Name"
                    android:textSize="18sp" />

                <TextView
                    android:id="@+id/textView12"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Description"
                    android:textSize="18sp" />

                <TextView
                    android:id="@+id/textView13"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Stock"
                    android:textSize="18sp" />

                <Button
                    android:id="@+id/button9"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Button" />
            </LinearLayout>
        </TableRow>

    </TableLayout>

    <Button
        android:id="@+id/button3"
        android:layout_width="135dp"
        android:layout_height="51dp"
        android:text="Stuff"
        app:backgroundTint="#8BC34A"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/button5"
        android:layout_width="135dp"
        android:layout_height="51dp"
        android:layout_marginEnd="1dp"
        android:layout_marginRight="1dp"
        android:text="Person"
        app:backgroundTint="#8BC34A"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/button6"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toEndOf="@+id/button3" />

    <Button
        android:id="@+id/button6"
        android:layout_width="135dp"
        android:layout_height="51dp"
        android:layout_marginStart="1dp"
        android:layout_marginLeft="1dp"
        android:text="card"
        app:backgroundTint="#07464E"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/button5" />

</androidx.constraintlayout.widget.ConstraintLayout>

And i have another one with almost same options. But this one doesn't displayed. Also java class has few action listeners for buttons, and nothing for TableLayuout. But another java class with table layout doesnt have anything with code, like that

public class admpanel extends lk{
    private TableLayout table;

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.admpanel);
    }
}

And thats works.

P.S. another one TableLayout doesnt displayed because im forgot to use "id" for pieces, then i fixed it. But now im idk

EDIT: That code start work if i am delete ImageView. How to use Image View? Pointed problem


Solution

  • The issue with your code is that the code has not specified a proper android:src or app:srcCompat for the imageView which is necessary to inflate the UmageView to some height or it considers the height as 0dp resulting in the ImageView not show up. tools:src is used just to show the image in the XML design tab to have an idea of how much space or how the view will look. The image specified can not be used for showing it in the application. I have updated your code for a better design and also added a proper image for your ImageView(Resource here). Save the image in your drawable folder as a12_logo. Check this code and comment if you have any issues ahead.

    Also, if the purpose of this TableLayout is to use it for showing a list of items, switch to RecyclerView for better performance and easy data handling.

    Also, your Buttons have fixed height and width which is not ideal as the buttons might get out of the screen for some devices so I have added better code for the buttons.

    <?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"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
        <TableLayout
            android:id="@+id/katalog"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:stretchColumns="1"
            app:layout_constraintBottom_toTopOf="@id/button3">
    
            <TableRow
                android:id="@+id/row"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
    
                <ImageView
                    android:id="@+id/imageView"
                    android:layout_width="120dp"
                    android:layout_height="match_parent"
                    android:layout_column="0"
                    android:src="@drawable/a12_logo" />
    
                <LinearLayout
                    android:id="@+id/linear"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_column="1"
                    android:orientation="vertical">
    
                <TextView
                    android:id="@+id/textView11"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Name"
                    android:textSize="18sp" />
    
                    <TextView
                        android:id="@+id/textView12"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Description"
                        android:textSize="18sp" />
    
                    <TextView
                        android:id="@+id/textView13"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Stock"
                        android:textSize="18sp" />
    
                    <Button
                        android:id="@+id/button9"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Button" />
                </LinearLayout>
            </TableRow>
        </TableLayout>
    
        <Button
            android:id="@+id/button3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Stuff"
            app:backgroundTint="#8BC34A"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/button5"
            app:layout_constraintHorizontal_chainStyle="spread"
            app:layout_constraintStart_toStartOf="parent" />
    
        <Button
            android:id="@+id/button5"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="1dp"
            android:text="Person"
            app:backgroundTint="#8BC34A"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/button6"
            app:layout_constraintStart_toEndOf="@+id/button3" />
    
        <Button
            android:id="@+id/button6"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="1dp"
            android:text="card"
            app:backgroundTint="#07464E"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/button5" />
    </androidx.constraintlayout.widget.ConstraintLayout>