ListView is ViewGroup that helps create a list of repeating items much like a Scrollable Grid. In this example we have defined a List inside an existing Linear Layout with TextViews at the Top and Bottom of the List. To display the List we have to first create an ArrayAdapter from an ArrayList of countries and then set the listView adatpter to the ArrayAdapter using the listView.setAdapter() method. The OnItemClickListener attached to the ListView helps identify the country that was clicked.
Source for Activity - AndroidListViewLayoutActivity
package com.as400samplecode;
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.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class AndroidListViewLayoutActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Generate list View from ArrayList
displayListView();
}
private void displayListView() {
//Array list of countries
List<String> countryList = new ArrayList<String>();
countryList.add("Aruba");
countryList.add("Anguilla");
countryList.add("Netherlands Antilles");
countryList.add("Antigua and Barbuda");
countryList.add("Bahamas");
countryList.add("Belize");
countryList.add("Bermuda");
countryList.add("Barbados");
countryList.add("Canada");
countryList.add("Costa Rica");
countryList.add("Cuba");
countryList.add("Cayman Islands");
countryList.add("Dominica");
countryList.add("Dominican Republic");
countryList.add("Guadeloupe");
countryList.add("Grenada");
countryList.add("Greenland");
countryList.add("Guatemala");
countryList.add("Honduras");
countryList.add("Haiti");
countryList.add("Jamaica");
//create an ArrayAdaptar from the String Array
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
R.layout.country_list, countryList);
ListView listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
//enables filtering for the contents of the given ListView
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast toast = Toast.makeText(getApplicationContext(),
((TextView) view).getText(), Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP, 25, 300);
toast.show();
}
});
}
}
Source for Main Screen Layout - main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:padding="10dp"
android:text="@string/some_text" android:textSize="20sp" />
<ListView android:id="@+id/listView1" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_marginBottom="40dp" />
<RelativeLayout android:layout_width="wrap_content"
android:layout_gravity="bottom" android:id="@+id/bottomLayout"
android:layout_height="match_parent" android:layout_marginTop="-40dp">
<TextView android:id="@+id/textView1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:padding="5dp"
android:text="Some Random Text at the Bottom"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
</LinearLayout>
Source for List Layout - country_list.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
Source for application variables - strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Android ListView</string>
<string name="some_text">
Just some random Text inside our view before we display a list.
We are going to display some North American Countries!
</string>
</resources>
Source for application manifest - AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.as400samplecode" android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="15" />
<application android:icon="@drawable/ic_launcher"
android:label="@string/app_name" android:theme="@android:style/Theme.Light">
<activity android:name=".AndroidListViewLayoutActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
References
No comments:
Post a Comment
NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.