Friday, 14 March 2014

ListView Functionality Creation and Implementation in Android Applications

Static ListView

This post describe about static ListView functionality in android applications. For list view take string array list with list items in res->values->strings.xml editor and add this list item reference with list view entries in main.xml editor.

main.xml


<LinearLayout 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:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="List View"
        android:layout_marginTop="20dp"
        android:layout_gravity="center"
        android:textColor="#336633"
        android:textSize="20sp"
        android:textStyle="bold" />



    <ListView
        android:id="@+id/listView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_gravity="center"
        android:entries="@array/list" >
    </ListView>

</LinearLayout>

ListViewActivity.java

package com.rakesh.tiwari.listview;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class ListViewActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

@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_view, menu);
return true;
}

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.rakesh.tiwari.listview"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:screenOrientation="portrait"
            android:name="com.rakesh.tiwari.listview.ListViewActivity"
            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>

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">List View</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    
    <string-array name="list">
        <item >C</item>
        <item >C++</item>
        <item >Java</item>
        <item >Android</item>
        <item >Blackberry</item>
        <item >.Net</item>
        <item >C#</item>
        <item >PHP</item>
        </string-array>

</resources>

ListView
ListView