Skip to main content

Android ListView with Image and Text

https://www.viralandroid.com/2016/02/android-listview-with-image-and-text.html



n this tutorial you will learn to create a custom listview android application with images and text in each list item. To display image, topic, and description in ListView we have to create a new XML file in res/layout folder and add ImageView and TextView.

To implement ListView in android, you have to work more in java activity file. ListView is a common component/layout in android application. It is difficult to make some awesome application without using listview like news app, tutorial app, social app, etc.

Related:
Simple Android ListView Example
Android ListView Example
Android User Interface (UI) Design Tutorial

Android Example: How to Implement Custom ListView with Image and Text


Let’s start by creating new android project with following information.
Application name: Custom Android ListView ImageAndText Example
Company Domain: viralandroid.com
Package name: com.viralandroid.customandroidlistviewtutorialeample
Minimum SDK: Android 3.2 (API 10)

Adding ListView in XML Layout File

Open your XML layout file and add a listview. XML layout file will looks like following.

res/layout/ activity_listview_with_image_and_text.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="viralandroid.com.androidlistviewwithimageandtext.ListViewWithImageAndText">
<ListView
android:id="@+id/list_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>

Custom ListView XML File

To add image and text in listview first you have to create a new XML layout file in res/layout/ folder. In this file I have added an ImageView two TextView with LinearLayout. Following is the complete content this file.

res/ listview_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/android_list_view_tutorial_with_example"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="7dp">
<ImageView
android:id="@+id/listview_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/app_name"
android:padding="10dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="7dp">
<TextView
android:id="@+id/listview_item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="@+id/listview_item_short_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>

Java Activity File

Following is the complete java activity file where we control all thing about listview.

src/ ListViewWithImageAndText.java
// Android Custom ListView with Image and Text Tutorial with Example and Source Code
package viralandroid.com.androidlistviewwithimageandtext;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class ListViewWithImageAndText extends AppCompatActivity {
// Array of strings for ListView Title
String[] listviewTitle = new String[]{
"ListView Title 1", "ListView Title 2", "ListView Title 3", "ListView Title 4",
"ListView Title 5", "ListView Title 6", "ListView Title 7", "ListView Title 8",
};
int[] listviewImage = new int[]{
R.drawable.profile_pc, R.drawable.profile_pc, R.drawable.profile_pc, R.drawable.profile_pc,
R.drawable.profile_pc, R.drawable.profile_pc, R.drawable.profile_pc, R.drawable.profile_pc,
};
String[] listviewShortDescription = new String[]{
"Android ListView Short Description", "Android ListView Short Description", "Android ListView Short Description", "Android ListView Short Description",
"Android ListView Short Description", "Android ListView Short Description", "Android ListView Short Description", "Android ListView Short Description",
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listview_with_image_and_text);
List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < 8; i++) {
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("listview_title", listviewTitle[i]);
hm.put("listview_discription", listviewShortDescription[i]);
hm.put("listview_image", Integer.toString(listviewImage[i]));
aList.add(hm);
}
String[] from = {"listview_image", "listview_title", "listview_discription"};
int[] to = {R.id.listview_image, R.id.listview_item_title, R.id.listview_item_short_description};
SimpleAdapter simpleAdapter = new SimpleAdapter(getBaseContext(), aList, R.layout.listview_activity, from, to);
ListView androidListView = (ListView) findViewById(R.id.list_view);
androidListView.setAdapter(simpleAdapter);
}
}

Strings.xml File

res/values/strings.xml
<resources>
<string name="app_name">Android ListView With Image And Text</string>
</resources>
view rawstrings.xml hosted with ❤ by GitHub

Android Example: How to Implement Custom ListView with Image and Text

That’s all. Now run your Android ListView with Image and Text application, you will see lists with image, list title and short description which will look like above screenshot.

Comments

Popular posts from this blog

web2apk

http://web2apk.com/create.aspx Create App   Intro   About   Changes   MalWare ?   Contact   Privacy Useful Links Bluetooth Mini Keyboards Android Mini PC Reset Android URL App Title Icon or

Android Bar Chart Using MpAndroidChart Library Tutorial

https://www.numetriclabz.com/android-bar-chart-using-mpandroidchart-library-tutorial/ Android Bar Chart Using MpAndroidChart Library Tutorial Objective In this tutorial we learn how to implement Bar Chart using MpAndroidChart Library in your Android App. Download Source Code       Step 1 Contents ·        1  Introduction ·        2  Creating Bar chart o    2.1  Create a new Project o    2.2  Adding library in Project o    2.3  Create Layout o    2.4  To Plot Bar Chart §   2.4.1  Initialize the graph id §   2.4.2  Creating a Dataset §   2.4.3  Defining X-axis labels §   2.4.4  Set the data §   2.4.5  Add the description to the chart §   2.4.6  Run your App § ...

how to retrieve image from sqlite database in android and display in listview

 Android platform provides several ways to store data in our application. 1. SQLite database 2. SharedPreferences etc For our post, we will only work with SQLite database. First and foremost, we need to understand what an SQLite database is? SQLite database  is an open source SQL database that stores data to a text file on a device. It executes SQL Commands to perform a set of functions, that is, create, read, update and delete operations. On my previous post, I showed how to  store data in SQLite database from edit text, retrieve and populate it in a listview . For this post, I will show the SQLite CRUD operations with images from gallery and text from EditText. We need to understand this; images are stored in SQLite database as BLOB data type. A BLOB is a large binary object that can hold a variable amount of data.  Note, we can only store images in the database as BLOB data type. We need to convert our image path to a bitmap th...