ListView in Android
In this tutorial we will learn how to implement simple ListView using the system Adapter along with the onItemClick() event to launch the next Activity when user hits the list item with a simple example in Android Studio. Listview is most commonly used UI component in Android.
Example:
- Create a new project and name it as ListViewInAndroid.
- Design a listView in main layout.
- Create a new Activity and name it as SecondActivity.
- Design a textview in second layout to show a text as "Welcome to Simplified Android".
<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="com.example.hss_24.listviewinandroid.MainActivity"> <ListView android:id="@+id/simple_listView" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView> </RelativeLayout>
File: activity_second.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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:gravity="center" tools:context="com.example.hss_24.listviewinandroid.SecondActivity"> <TextView android:textStyle="bold" android:textColor="#008000" android:gravity="center" android:textSize="30sp" android:text="Welcome to Simplified Android" android:layout_width="match_parent" android:layout_height="wrap_content" /> </RelativeLayout>
- Declare an array in the main java file.
// declare Array String[] names_of_android = {"Cupcake","Donut","Eclair","Froyo","Gingerbread","Honeycomb", "IceCream Sandwich","Jelly Bean","Lollipop","Marshmallow"};
- Call an Adapter and inflate the array in layout.
// Inflate array in layout and store in array_adapte rArrayAdapter adapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,names_of_android);
- Display the array in ListView.
// Display array in listView ListView listView = (ListView)findViewById(R.id.simple_listView); listView.setAdapter(adapter);
- Set onItemClickListener to listview, this listener is to perform click event in listView.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { } });
File: MainActivity.java:
import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; public class MainActivity extends AppCompatActivity { // declare Array String[] names_of_android = {"Cupcake", "Donut", "Eclair", "Froyo", "Gingerbread", "Honeycomb", "IceCream Sandwich", "Jelly Bean", "Lollipop", "Marshmallow"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Inflate array in layout and store in array_adapter final ArrayAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, names_of_android); // Display array in listView ListView listView = (ListView) findViewById(R.id.simple_listView); listView.setAdapter(adapter); // Set onClickItem event to listView listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // Launch Activity Intent intent = new Intent(MainActivity.this, SecondActivity.class); startActivity(intent); } }); } }
- Run the Application.
Download Project here
Thank you!!!
Please like and share...



No comments:
Post a Comment