Android Developer Trainee Android Developer Trainee - 2 months ago 7
Android Question

Android XML code : how to place an image left of Textview?

In my Android Application i have 2 textviews and 1 image to e displayed on the listview. While i used my imageview it is going to the new line..

I want one textview and imageview on the same line. Every time i place the imageview the whole alignment is changing ..

my XML code is

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

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:orientation="vertical" >

<ImageView
android:id="@+id/icon"

android:layout_width="50dp"
android:layout_height="50dp"
android:gravity="left"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:src="@drawable/album1" />


<TextView
android:layout_toRightOf="@id/icon"
android:id="@+id/textViewSongName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textColor="@android:color/white"
android:textSize="22dp" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<TextView
android:id="@+id/textViewArtist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:layout_weight="1"
android:textColor="@android:color/white"
android:textSize="15dp" />

<TextView
android:id="@+id/textViewDuration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:layout_marginLeft="10dp"
android:textSize="12dp" />
</LinearLayout>
</LinearLayout>

</LinearLayout>

Answer

you can use this layout i have set weightSum property.

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="1">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.3">

                <ImageView
                    android:id="@+id/icon"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="left"
                    android:layout_marginBottom="5dp"
                    android:layout_marginLeft="5dp"
                    android:layout_marginRight="5dp"
                    android:layout_marginTop="5dp"
                    android:src="@mipmap/ic_launcher" />


            </LinearLayout>
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:layout_weight="0.7">

                <TextView
                    android:layout_toRightOf="@id/icon"
                    android:id="@+id/textViewSongName"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:singleLine="true"
                    android:text="iconTextview"
                    android:textColor="@android:color/background_dark"
                    android:textSize="22dp" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:weightSum="1"

                    android:orientation="horizontal" >

                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="0.5">
                        <TextView
                            android:id="@+id/textViewArtist"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:singleLine="true"
                             android:text="textViewArtist"
                            android:textColor="@android:color/background_dark"
                            android:textSize="15dp" />

                    </LinearLayout>
                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="0.5">
                        <TextView
                            android:id="@+id/textViewDuration"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textColor="@android:color/background_dark"
                            android:layout_marginLeft="10dp"
                            android:text="textViewDuration"
                            android:textSize="12dp" />
                    </LinearLayout>


                </LinearLayout>

            </LinearLayout>


             </LinearLayout>

    </LinearLayout>
Comments