How to align ImageView and TextView in Custom ListView

I have created a Custom Adapter to show ImageView and TextView together in ListView. But the ImageView is showing on the top of TextView in every row of Listview. I have attached a screenshot of app below:

Custom Adapter Class:

public class CustomeAdapter extends BaseAdapter {
    private Context context;
    private ListView listView;
    private ArrayList<PlayersModel> playersModelArrayList;

    public CustomeAdapter(Context context, ArrayList<PlayersModel> playersModelArrayList) {

        this.context = context;
        this.playersModelArrayList = playersModelArrayList;
    }

    @Override
    public int getViewTypeCount() {
        return getCount();
    }
    @Override
    public int getItemViewType(int position) {

        return position;
    }

    @Override
    public int getCount() {
        return playersModelArrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return playersModelArrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position,View convertView,ViewGroup parent) {
        ViewHolder holder;


        if (convertView == null) {
            holder = new ViewHolder();
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.lv_item, null, true);

            holder.icon = (TextView) convertView.findViewById(R.id.TextID);
            holder.tvname = (TextView) convertView.findViewById(R.id.name);
            holder.tvdescr = (TextView) convertView.findViewById(R.id.descr);
            holder.tvlink = (TextView) convertView.findViewById(R.id.link);
            holder.ImageIcon = (ImageView) convertView.findViewById(R.id.logoImageView);

            convertView.setTag(holder);
        }else {
            holder = (ViewHolder)convertView.getTag();


        }

        holder.ImageIcon.setImageResource(playersModelArrayList.get(position).getImageId());
        holder.icon.setText(playersModelArrayList.get(position).getICON());
        holder.tvname.setText(playersModelArrayList.get(position).getName());
        holder.tvdescr.setText(playersModelArrayList.get(position).getDescr());
        holder.tvlink.setText(playersModelArrayList.get(position).getLink());


            Glide.with(context)
                    .load("http://csalabs.in/wp-content/uploads/2017/10/CSA-Final-PNG-300x350.png")
                    .override(200,200)
                    .into(holder.ImageIcon);
            Log.d("Icon URL",holder.icon.toString());

        return convertView;
    }
    private class ViewHolder {

        protected TextView icon, tvname, tvdescr, tvlink;
        protected ImageView ImageIcon;
    }
}

Layout File

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:padding="10dp"
    android:orientation="vertical">

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

        <ImageView
            android:id="@+id/logoImageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="fitStart"
            android:adjustViewBounds="true"
            tools:srcCompat="@tools:sample/avatars[0]" />

        <TextView
            android:id="@+id/name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#383838"
            android:layout_marginTop="10dp"
            android:textSize="15sp"
            android:textStyle="bold"
            android:gravity="center_vertical"

            android:textAppearance="?android:attr/textAppearanceMedium"
            android:paddingLeft="10dp"
            android:text="Name" />
        <TextView
            android:id="@+id/descr"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#313131"
            android:textSize="12sp"
            android:gravity="center_vertical"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:paddingLeft="10dp"
            android:text="Country" />
        <TextView
            android:id="@+id/link"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#383838"
            android:gravity="center_vertical"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:paddingLeft="10dp"
            android:text="@string/city"
            android:visibility="gone"/>
        <TextView
            android:id="@+id/TextID"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#313131"
            android:textSize="12sp"
            android:gravity="center_vertical"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:paddingLeft="10dp"/>


    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:background="@color/colorAccent"/>

</LinearLayout>

How to align ImageView on the left side of TextView? Thanks in advance. BTW I am newbie in android and java.

Try this

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
    android:orientation="vertical"
    android:padding="10dp">

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

        <ImageView
            android:id="@+id/logoImageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:scaleType="fitStart"
            tools:srcCompat="@tools:sample/avatars[0]" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="buttonClickNew"
            android:orientation="vertical">


            <TextView
                android:id="@+id/name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:gravity="center_vertical"
                android:paddingLeft="10dp"
                android:text="Name"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#383838"
                android:textSize="15sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/descr"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:paddingLeft="10dp"
                android:text="Country"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#313131"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/link"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:paddingLeft="10dp"
                android:text="city"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#383838"
                android:visibility="visible" />

            <TextView
                android:id="@+id/TextID"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:paddingLeft="10dp"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#313131"
                android:textSize="12sp" />


        </LinearLayout>

    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:background="@color/colorAccent" />

</LinearLayout>

Android Custom ListView with Image and Text Example Posted on: October 12, 2017 at 3:30 pm by Sanket Mhaddalkar - 2 Comments . In this article will see how to create custom ListView in android. By creating a custom ListView you can design how a single row will look like in the view. ListView is the basic component to view the data in a list.

Use like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:padding="10dp"
android:orientation="vertical">

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

 <ImageView
        android:id="@+id/logoImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitStart"
        android:adjustViewBounds="true"
        tools:srcCompat="@tools:sample/avatars[0]" />

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

    <TextView
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#383838"
        android:layout_marginTop="10dp"
        android:textSize="15sp"
        android:textStyle="bold"
        android:gravity="center_vertical"

        android:textAppearance="?android:attr/textAppearanceMedium"
        android:paddingLeft="10dp"
        android:text="Name" />
    <TextView
        android:id="@+id/descr"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#313131"
        android:textSize="12sp"
        android:gravity="center_vertical"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:paddingLeft="10dp"
        android:text="Country" />
    <TextView
        android:id="@+id/link"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#383838"
        android:gravity="center_vertical"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:paddingLeft="10dp"
        android:text="@string/city"
        android:visibility="gone"/>
    <TextView
        android:id="@+id/TextID"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#313131"
        android:textSize="12sp"
        android:gravity="center_vertical"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:paddingLeft="10dp"/>`enter code here`

</LinearLayout>
</LinearLayout>
<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:background="@color/colorAccent"/>

</LinearLayout>

So here is the complete step by step project for Android custom listview with imageview and textview using ArrayAdapter String. Main Points and files description about this project please read them to more easily understand the whole project:

Try to wrap Your text to separated LinearLayout with vertical orientation, and than put this LinearLayout with text on same level as ImageView inside another LinearLayout with orientation="horizontal"

But also, you should remember about render and measure performance. You can read about this in https://android.jlelse.eu/constraint-layout-performance-870e5f238100

<LinearLayout 
    android:orientation="horizontal">
  <ImageView 
     android:layout_width="0dp"
     android:layout_weight="1" // try to read about this parameter more
  />
  <LinearLayout
     android:layout_width="0dp"
     android:layout_weight="4"
     android:orientation="vertical">

    <TextView ... />
    <TextView ... />
    <TextView ... />
  </LinearLayout>
</LinearLayout>

Custom ListView with ImageView and TextView In Android. Guest Posts coolbhushans . In this tutorial we are implement the customListView using BaseAdapter.

Try to create two linear layouts with orientation vertical under parent linear layout which has the horizontal orientation.

Set the second linear layout's width to match_parent and the image layout should be a fixed width value.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:padding="10dp">

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


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

            <ImageView

                android:id="@+id/logoImageView"
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:scaleType="fitXY"
                tools:srcCompat="@tools:sample/avatars[0]" />

        </LinearLayout>

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

            <TextView
                android:id="@+id/name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:gravity="center_vertical"
                android:paddingLeft="10dp"
                android:text="Name"
                android:textAppearance="?android:attr/textAppearanceMedium"

                android:textColor="#383838"
                android:textSize="15sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/descr"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:paddingLeft="10dp"
                android:text="Country"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#313131"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/link"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:paddingLeft="10dp"
                android:text="City"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#383838"
                android:visibility="gone" />

            <TextView
                android:id="@+id/TextID"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:paddingLeft="10dp"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#313131"
                android:textSize="12sp" />

        </LinearLayout>


    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:background="@color/colorAccent" />

</LinearLayout>

In this Android 4 example, we will create custom ListView where each row item consists of one ImageView and two TextView (one for displaying image title and other for displaying image description) and populate its items using custom ArrayAdapter.

In order to simplify your xml, you should take a look to ConstraintLayout

To use it, make the import in your app gradle by adding implementation 'com.android.support.constraint:constraint-layout:1.1.3'

Then, refactor your xml as below:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">

    <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/colorAccent" android:id="@+id/view"
            tools:layout_editor_absoluteX="0dp" app:layout_constraintTop_toBottomOf="@+id/logoImageView"
            android:layout_marginTop="8dp"/>
    <ImageView
            android:layout_width="64dp"
            android:layout_height="64dp" tools:srcCompat="@tools:sample/avatars"
            android:id="@+id/logoImageView"
            app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="8dp"
            android:layout_marginTop="8dp" app:layout_constraintTop_toTopOf="parent"/>
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:id="@+id/name"
            tools:text="Name"
            android:textAppearance="@style/TextAppearance.AppCompat" android:textStyle="bold"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toTopOf="@+id/descr" app:layout_constraintStart_toEndOf="@+id/logoImageView"
            android:layout_marginStart="8dp"/>
    <TextView
            tools:text="Description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/descr" android:textColor="#313131"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toBottomOf="@+id/logoImageView"
            app:layout_constraintStart_toEndOf="@+id/logoImageView" android:layout_marginStart="8dp"/>
</android.support.constraint.ConstraintLayout>

As the simple ListView, custom ListView also uses Adapter classes which added the content from data source (such as string array, array, database etc). Adapter bridges data between an AdapterViews and other Views. Example of Custom ListView. In this custom listview example, we are adding image, text with title and its sub-title.

I have the following layout for a ListView item: And this is the rendered result: As you can see there is a gap between the real text and the bottom of the image I was expecting to have the bottom of the text matching exactly with the bottom of the imageso question is: How can I align the text

Custom ListView with ImageView and TextView In Android part 1 #1 Android Custom ListView with ImageView EditText Button and Custom Adapter Custom ListView-ListView with Image and Text in

Use the id textview to dynamically set the android:text value. Since a RealativeLayout stacks its children, defining the TextView after ImageView puts it โ€˜overโ€™ the ImageView. NOTE: Similar results can be attained using a FrameLayout as the parent, along with the efficiency gain over using any other android container.

Comments
  • Can you show you item layout for list view
  • yep, updating ques.
  • I would good if you use good UI components like RecyclerView
  • You have used vertical linear layout. Use horizontal orientation for your linear layout
  • I didn't knew about it. Can you explain how could I use RecyclerView?
  • TextView not showing now. Only having ImageView on screen.
  • Wow. Worked like a charm. Thanks for your help :)
  • @Strange Glad to have been of help! Feel free to accept my answer if you feel it was useful to you. ๐Ÿ™‚