Last updated on August 12th, 2015 at 11:56 pm
In this post we are going to create a simple custom ListView. This post is very simple and for beginners.
Start your New Android Project
1. Create a new project in Eclipse from File ⇒ New ⇒ Android Application Project. I had left my main activity name as MainActivity.java .
2. Now open your activity_main.xml inside res ⇒ layout folder and insert ListView from Composite inside Palette to your activity_main.xml
activity_main.xml <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <ListView android:id="@+id/disp" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" > </ListView> </RelativeLayout>
3. Now create new XML file inside res ⇒ layout folder da_list.xml and add TextView in it.
da_list.xml <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > </TextView>
4. Now open your MainActivity.java inside src ⇒MainActivity.java. In this file we can insert data to ListView and handel click when user press on a list.
MainActivity.java package com.listview; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // List view listviewload(); handleClick(); } private void listviewload() { // TODO Auto-generated method stub String[] arr = { "aneh", "thakur", "love", "think", "code" }; ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.da_list, arr); ListView list = (ListView) findViewById(R.id.disp); list.setAdapter(adapter); } private void handleClick() { // TODO Auto-generated method stub ListView list = (ListView) findViewById(R.id.disp); list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> paret, View viewClicked, int position, long id) { // TODO Auto-generated method stub TextView textview = (TextView)viewClicked; String message = "You clicked"+ position + "Text"+textview.getText().toString(); Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show(); // Open new activity Intent i = new Intent(MainActivity.this, Data.class); i.putExtra("Data", message); startActivity(i); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.list, menu); return true; } }
In MainActivity.java create two method
listviewload(); handelClick();
In listviewload() we load the data into listview and show and in 2nd method handelClick() we handle user click.
listviewload() Method:
In this method first we create array string in which we are going to show in listview, after this we use array adapter. In ArrayAdapter we pass three parameter
ArrayAdapter<String>(Context context, Resource, ArrayData);
after that we set adapter data to ListView.
private void listviewload() { // TODO Auto-generated method stub String[] arr = { "aneh", "thakur", "love", "think", "code" }; ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.da_list, arr); ListView list = (ListView) findViewById(R.id.disp); list.setAdapter(adapter); }
handleClick() Method:
In this method we handle user click on list and open new activity. You need to create new xml file inside layout folder
data.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/dataa" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="98dp" android:layout_marginTop="55dp" android:text="" /> </RelativeLayout>
Now when user click on a particular list we open activity according to ListItem. We use setOnItemClickListener() to handle user click.
private void handleClick() { // TODO Auto-generated method stub ListView list = (ListView) findViewById(R.id.disp); list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> paret, View viewClicked, int position, long id) { // TODO Auto-generated method stub TextView textview = (TextView)viewClicked; String message = "You clicked"+ position + "Text"+textview.getText().toString(); Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show(); // Open new activity Intent i = new Intent(MainActivity.this, Data.class); i.putExtra("Data", message); startActivity(i); } }); }
Create new java file inside src folder name as Data.java this is the file we open when user click on list.
Data.java package com.listview; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; public class Data extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.data); Intent i = getIntent(); String d = i.getStringExtra("Data"); TextView txt = (TextView)findViewById(R.id.dataa); txt.setText(d); } }
You also need to add this activity entry in manifest file.
AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.listview" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="9" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:largeHeap="true" android:theme="@style/AppTheme" > <activity android:name="com.listview.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="Data" > </activity> </application> </manifest>
You can try this code and also download file from above download link.