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

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