How to Create a ListView with Same Image icon but Different text in each row in Android

Let’s get start by creating a project in Android ADT.

1. Create a new project by going to File ⇒ New Android Application. Fill all the details  As Application Name:SameIconsListView,Project Name:SameIconsListView,Package Name:com.test.sameiconslistview  and click on Next button to finish .

2 .Once the project is created open your activity_main.xml (res/layout) and paste the following code.

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
xmlns:tools=”http://schemas.android.com/tools&#8221;
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
tools:ignore=”InefficientWeight” >

<ListView
android:id=”@+id/listView1″
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_weight=”1″ >
</ListView>

</LinearLayout>

  • Create another xml under res/layout as row.xml.

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
xmlns:tools=”http://schemas.android.com/tools&#8221;
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:orientation=”horizontal”
tools:ignore=”UseCompoundDrawables,HardcodedText,ContentDescription” >

<ImageView
android:id=”@+id/imageView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:src=”@drawable/ic_launcher” />

<TextView
android:id=”@+id/textView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”TextView”
android:layout_marginTop=”17dp”/>

</LinearLayout>

3.Open MainActivity.java class and paste following code.

package com.test.sameiconslistview;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ListView listView = (ListView) findViewById(R.id.listView1);
String[] items = new String[] { “sunday”, “monaday”, “tuesday”,
“wednesday”, “thrusday”, “friday”, “saturday” };

final ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getApplicationContext(), R.layout.row, R.id.textView1, items);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
// ListView Clicked item value

String filename = adapter.getItem(position).toString();

Toast.makeText(
getApplicationContext(),
“Slected File=” + filename + “;”
+ ” Selected Position=” + position,
Toast.LENGTH_LONG).show();

}
});

}

}
4. Finally run your project by right clicking on your project folder ⇒ Run As ⇒ 1 Android Application.

listwithicon listwithicon

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s