Home > Android > Android Listview Example

Android Listview Example

Android Listview Example

Android Listview  

Android Listview Example

Android Listview Example    Android Listview Example

Java Code(ListViewAct)

import android.app.Activity;
import android.content.Intent;
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 ListViewAct extends Activity {
ListView lv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
lv1=(ListView)findViewById(R.id.lv1);
final String items[]={“c”,”java”,”c++”,”android”};

ArrayAdapter aa=new ArrayAdapter(this,android.R.layout.simple_dropdown_item_1line,items);
lv1.setAdapter(aa);
lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

Toast.makeText(ListViewAct.this, items[position], Toast.LENGTH_SHORT).show();
}
});
}

}



Layout XML Code(activity_list_view)

<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”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”com.andro.tech.androidproject.ListViewAct”>

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:textSize=”@dimen/textsize”
android:textStyle=”bold”
android:layout_gravity=”center”
android:text=”@string/listviewtext”
android:layout_marginBottom=”10dp” />

<ListView
android:layout_width=”match_parent”
android:layout_height=”300dp”
android:id=”@+id/lv1″
android:layout_marginBottom=”50dp”>
</ListView>

<Button
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:text=”@string/sourcecode”
android:background=”@color/bgcolor”
android:textStyle=”bold”
android:textColor=”@color/text”
android:onClick=”view1″/>

</LinearLayout>


Output : 

Android Listview Example    Android Listview Example

Download Source Code


Follow Us – Never Miss Update

Fb/PageTwitterGoogle+InstagramPinterest 

Recent   


Android Listview Example

Leave a Reply

Your email address will not be published. Required fields are marked *

Translate »