Search code examples
androidimageviewandroid-linearlayout

ImageView is not getting shown in the Linear Layout


I am trying to display a textview and 2 imageviews in an xml which to be used as a view for a list item in the ListView. It should be like this pattern:

TextView ImageView ImageView

But my imageviews are not getting displayed.

My layout file is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent"

    >


    <LinearLayout
        android:layout_width="360dp"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/flowerName"
            />

    </LinearLayout>



    <LinearLayout
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:orientation="vertical"
       >


       <ImageView
           android:layout_width="50dp"
           android:layout_height="50dp"
           android:id="@+id/shareImage"
           android:src="@drawable/share"
           />

       <ImageView
           android:layout_width="50dp"
           android:layout_height="50dp"
           android:id="@+id/idImage"
           android:src="@drawable/white_star"

           />



   </LinearLayout>


</LinearLayout>

Solution

  • You should consider using weight.
    Global layout would have a 100 weightSum. TextView a 90 weight and your layout holding ImageView 10, with gravity end.

    Using Weight, you are sure all your view will be visible whatever the screen size is.

    Something like :

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal" android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="100">
    
    
        <LinearLayout
            android:layout_weight="90"
            android:layout_width="0dp"
            android:minHeight="50dp"
            android:layout_height="wrap_content">
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/flowerName"/>
    
        </LinearLayout>
    
    
        <LinearLayout
            android:layout_weight="10"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="end"
            android:orientation="horizontal">
    
            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:id="@+id/shareImage"
                android:src="@drawable/share"/>
    
            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:id="@+id/idImage"
                android:src="@drawable/white_star"/>
    
        </LinearLayout>
    
    
    </LinearLayout>