Different Images with Different text In Android ListView(Part One)

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:DifferentIconListView,Project Name:DifferentIconListView,Package Name:com.Test.differenticonlistview  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>

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

package com.Test.differenticonlistview;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

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[] values = new String[] { “Sunday”, “Monday”, “Tuseday”,
“Wednessday”, “Thursday”, “Friday”, “Satday” };

MyPerformanceArrayAdapter adapter = new MyPerformanceArrayAdapter(this,
values);
listView.setAdapter(adapter);

}
}

3.Create a MyPerformanceArrayAdapter.java class and paste following code.

Here we are creating our own adapter class by simply extending this class to ArrayAdapter.

package com.Test.differenticonlistview;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyPerformanceArrayAdapter extends ArrayAdapter<String>{

Activity context;
String[] values;

public MyPerformanceArrayAdapter(Activity context, String[] values) {
// TODO Auto-generated constructor stub

super(context, R.layout.activity_main,values);
this.context = context;
this.values = values;
}

public View getView(int position,View view,ViewGroup parent){

LayoutInflater inflater = context.getLayoutInflater();
view= inflater.inflate(R.layout.row, null);

TextView textView = (TextView)view.findViewById(R.id.title);
ImageView imageView = (ImageView)view.findViewById(R.id.icon);

String s = values[position];
textView.setText(s);
if (s.startsWith(“Sunday”) || s.startsWith(“Thursday”)
|| s.startsWith(“Satday”)) {
imageView.setImageResource(R.drawable.ic_launcher);
} else {
imageView.setImageResource(R.drawable.icon_add);
}

return view;

}

}

Here need to create another layout under res/layout as row.xml.   

<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”
android:orientation=”horizontal”
tools:ignore=”UseCompoundDrawables,HardcodedText,ContentDescription” >

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

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

</LinearLayout>

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

2013-07-24_18-49-55

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