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

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.

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:orientation=”vertical” >

<ListView
android:id=”@+id/list”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
/>
</LinearLayout>

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

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout 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=”SpUsage,HardcodedText” >

<ImageView
android:id=”@+id/icon”
android:layout_width=”80dp”
android:layout_height=”80dp”
android:contentDescription=”image”
android:paddingLeft=”10dp”
android:paddingRight=”10dp” />

<TextView
android:id=”@+id/title”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_toRightOf=”@+id/icon”
android:paddingBottom=”10dp”
android:textColor=”#CC0033″
android:textSize=”16dp” />

<TextView
android:id=”@+id/desc”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_below=”@+id/title”
android:layout_toRightOf=”@+id/icon”
android:paddingLeft=”10dp”
android:textColor=”#3399FF”
android:textSize=”14dp” />

</RelativeLayout>

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

package com.Test.differenticonlistview ;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity implements
OnItemClickListener {

public static final String[] titles = new String[] { “Sunday”,
“Monday”, “Tuseday”, “Thursday”,”Friday”,”Satday” };

public static final String[] descriptions = new String[] {
“Today is Sunday”,
“Today is Monday”, “Today is Tuseday”,
“Today is Thursday”,”Today is Friday”,”Today is Satday” };

public static final Integer[] images = { R.drawable.icon_add,
R.drawable.ic_launcher, R.drawable.icon_add, R.drawable.ic_launcher,R.drawable.icon_add,R.drawable.ic_launcher };

ListView listView;
List<RowItem> rowItems;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

rowItems = new ArrayList<RowItem>();
for (int i = 0; i < titles.length; i++) {

RowItem item = new RowItem(images[i], titles[i], descriptions[i]);
rowItems.add(item);
}

listView = (ListView) findViewById(R.id.list);
MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(this,
R.layout.row, rowItems);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast toast = Toast.makeText(getApplicationContext(),
“Item ” + (position + 1) + “: ” + rowItems.get(position),
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}
}

4.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 java.util.List;

import android.app.Activity;
import android.content.Context;
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 MySimpleArrayAdapter extends ArrayAdapter<RowItem> {

Context context;
List<RowItem> items;

public MySimpleArrayAdapter(Context context, int resourceId,
List<RowItem> items) {
super(context, resourceId, items);
this.context = context;
this.items = items;
}

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

RowItem rowItem = getItem(position);

LayoutInflater inflater = ((Activity) context).getLayoutInflater();
convertView = inflater.inflate(R.layout.row, null);
TextView   txtDesc = (TextView) convertView.findViewById(R.id.desc);
TextView   txtTitle = (TextView) convertView.findViewById(R.id.title);
ImageView imageView = (ImageView) convertView.findViewById(R.id.icon);

txtDesc.setText(rowItem.getDesc());
txtTitle.setText(rowItem.getTitle());
imageView.setImageResource(rowItem.getImageId());

return convertView;
}
}

5. Creat a RowItem.class and paste following code .

package com.Test.differenticonlistview;

public class RowItem {

private int imageId;
private String title;
private String desc;

public RowItem(int imageId, String title, String desc) {
this.imageId = imageId;
this.title = title;
this.desc = desc;
}
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String toString() {
return title + “\n” + desc;
}

}

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

2013-07-24_19-30-38

 

 

Advertisements

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

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

 

 

 

Simple ListView without using android layout

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

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

import android.os.Bundle;
import android.app.ListActivity;
import android.content.Intent;
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 ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

String[] items=new String[] {“sunday”,”monaday”,”tuesday”,”wednesday”,”thrusday”,”friday”,”saturday”};

final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,items);

setListAdapter(adapter);

ListView listView = getListView();

listView.setOnItemClickListener(new OnItemClickListener() {

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

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

// ListView Clicked item value

//   String  filename= (String) listView.getItemAtPosition(position);
//   remove final from ArrayAdapter to use.

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

}

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

ListViewListView

How to Create a Simple ListView in Android

The display of elements in a lists is a very common pattern in mobile applications. The user sees a list of items and can scroll through them. If he selects one item, this can update the ActionBar or triggers a detailed screen for the selection.
Android provides the ListView class which is capable of displaying a scrollable list of items. These items can be of any type(Array Of anytype).

For example the following listing shows a layout file with includes a ListView.
<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:orientation=”vertical” >
<ListView
android:id=”@+id/listView1″
android:layout_width=”match_parent”
android:layout_height=”wrap_content” >
</ListView>
</LinearLayout>

Adapters and ListView implementations

A ListView receives its data via an adapter. The adapter also defines how each row in the ListView is displayed.
The adapter is assigned to the list via the setAdapter method on the ListView object.
An adapter extend the BaseAdapter class. Android provides several standard adapters; Like ArrayAdapter and CursorAdapter.

ArrayAdapter:

ArrayAdapter can handle data based on Arrays or java.util.List.The ArrayAdapter class can handle any Java object as input. It maps the data of this input to a TextView in the layout. You can define one in the constructor otherwise the android.R.id.text1 ID will be used.
ArrayAdapter uses the toString() method of the data input object to determine the String which should be displayed.

   [Except these two another adapter is SimpleCursorAdapter,which can handle database related data. (Check other post)]

The Android platform provides default layouts for rows in ListViews. For example
android.R.layout.simple_list_item1. By default ArrayAdapter uses the
android.R.id.text1 ID, but you can define another one in the ArrayAdapter constructor.

The following coding shows an example of the usage of the ArrayAdapter class and a predefinedlayout from the Android system.

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:SimpleListView,Project Name:SimpleListView,Package Name:com.test.simplelistview  and click on Next button to finish .

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

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

import android.os.Bundle;
import android.app.ListActivity;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Switch;
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[] values = new String[] {“sunday”,”monaday”,”tuesday”,”wednesday”,”thrusday”,”friday”,”saturday”};
// First paramenter – Context
// Second parameter – Layout for the row
// Third parameter – ID of the TextView to which the data is written
// Forth – the Array of data
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,  values);
// Assign adapter to ListView
listView.setAdapter(adapter);

listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
String filename = adapter.getItem(position).toString();

 // ListView Clicked item value
//   String  filename= (String) listView.getItemAtPosition(position);
//   remove final from ArrayAdapter to use.

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

}

}

4. The final step is to  AndroidManifest.xml file. Open you AndroidManifest.xml file and modify the code as below.

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
package=”com.testview.listviewtest”
android:versionCode=”1″
android:versionName=”1.0″ >

<uses-sdk
android:minSdkVersion=”8″
android:targetSdkVersion=”16″ />

<application
android:allowBackup=”true”
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name”
android:theme=”@style/AppTheme” >
<activity
android:name=”com.testview.listviewtest.MainActivity”
android:label=”@string/app_name” >
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
<activity android:name=”com.testview.listviewtest.SundayActivity”></activity>
<activity android:name=”.MondayActivity”></activity>
</application>

</manifest>

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

ListViewListView