Wednesday, September 4, 2013

Create a Simple List View In android Development



Create a project ListTest.

An Android Project Have Three Parts.

1.Activity 
is using to Write our Logic and Events.

2.Manifest
is using to set configuration and application Permissions.

3.Layout 
is an XML File it defines Controls using in our Application.



Look Our Manifest File.[AndroidManifest.xml]
is locating in root of our project


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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"

        [@string/app_name is Our application Titile
        It Defined in res/values/string.xml file.]

        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.listtest.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>
    </application>


</manifest>


Look at the Values  in String.xml file.


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

    <string name="app_name">Indian Actors List</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>


</resources>

Then Completed our Configuration Then we want to Create The Layout.

Look our Layout File.[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"

tools:context=".AdapterActivity" >



<ListView

android:id="@+id/list_id"

android:layout_width="fill_parent"

android:layout_height="fill_parent"
android:columnWidth="10dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" >
</ListView>

</RelativeLayout>

Finally Defining Activity[MainActivity.java]

package com.example.listtest;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

ListView listView;

static final String[] numbers = new String[] { "Mammooty", "Mohan Lal", "Amithab Bachan",
"Salman Khan", "Amir Khan", "Ram Charan", 
"Allu Arjun", "Surya", "Vikram", "Vijay" };

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  listView = (ListView) findViewById(R.id.list_id);
  ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
  android.R.layout.simple_selectable_list_item, numbers);

  listView.setAdapter(adapter);

   listView.setOnItemClickListener(new OnItemClickListener() {

      @Override
      public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
 Toast.makeText(getApplicationContext(),
        ((TextView) v).getText(), Toast.LENGTH_SHORT).show();

       }

   });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}


Done...

if any doubts in this article please write your doubts as comments..