Wednesday, 16 January 2013

ListView in Android

This is simple ListView example:

ScreenShots:



text.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"
    android:textSize="20dp"
    android:textColor="#000000" >

</TextView>

activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    android:background="#ff00ff" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:cacheColorHint="#ff00ff" >
    </ListView>

</LinearLayout>

second.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is second activity"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

MainActivity.java:
package com.ram.simplelistview;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
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 implements OnItemClickListener {

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

ListView list = (ListView) findViewById(R.id.listView1);
//ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), //android.R.layout.simple_list_item_1, names);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.text, names);
list.setAdapter(adapter);
list.setOnItemClickListener(this);
registerForContextMenu(list);
}

String[] names = { "name1", "name2", "name3", "name4", "name5", "Second Activity",
"name7", "name8", "name9", "name10" , "name11", "name12", "name13", "name14", "name15", "name16",
"name17", "name18", "name19", "name20"};

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "selected name is :"+names[position], Toast.LENGTH_SHORT).show();
if(position ==5 ){
startActivity(new Intent(getApplicationContext(), Second.class));
}
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.activity_main, menu);
super.onCreateContextMenu(menu, v, menuInfo);
}

}

Second.java:
package com.ram.simplelistview;

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

public class Second extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

setContentView(R.layout.second);
}
}



AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ram.simplelistview"
    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.ram.simplelistview.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="Second"></activity>
    </application>

</manifest>








1 comment: