Tuesday 29 September 2015

SimpleListView in Android

In a Android, ListView is arranges components or items  in a vertical scrollable list. If you have a long list of items that you simply want to show to the user, the ListView view is the answer. This example demonstrates the way to use the ListView to show a listing of items contained within an array.

activity_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"
   
tools:context=".MainActivity">


    <
ListView
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:id="@+id/listView"
       
android:divider="#ff554b"
       
android:dividerHeight="4dp" />
</
LinearLayout>
MainActivity.Java
----------------------------------
package com.android.anil.simplelistview1;



import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.AdapterView;

import android.widget.ArrayAdapter;

import android.widget.ListView;

import android.widget.Toast;



public class MainActivity extends AppCompatActivity {

    ListView list;

    String[] names={"Name1","Name2","Name3","Name4","Name5","Name6","Name7","Name8","Name9","Name10"};



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        list=(ListView)findViewById(R.id.listView);

        ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,names);

        list.setAdapter(arrayAdapter);

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override

            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Toast.makeText(MainActivity.this, "Name is : "+names, Toast.LENGTH_SHORT).show();

            }

        });



    }



}
Output is:
----------------------------------







No comments:

Post a Comment

Encryption Decryption Example Android Using AES Alogirthm

activity_main.xml ------------------------- <?xml version="1.0" encoding="utf-8"?> <LinearLayou...