Android list view demo.
Android listview demo
Listview provides a way to display several items in a vertical scrollable list. It can be achieved by using a ListView and an ArrayAdapter. Following are steps that demonstrate list view in android.
Steps to create Android Studio project:
- Open Android Studio.
- Click on Start a new Android Studio project.
- Enter your Application name.
- Select Minimum SDK version of your android app as 2.3.3 or you can select any other that suites your app.
- Select the type of Activity as Empty Activity.
- Next rename the Main activity or leave it as default.
Steps to implement Listview in android project:
- In your MainActivity type the following code:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;I
mport android.widget.ListView;
public class MainActivity extends AppCompatActivity {
String[] list_items = {"One","Two","Three","Four","Five"};
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter adapter = new ArrayAdapter<String>(this,R.layout.list_item, list_items);
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);
}
} 2. In your activity_main.xml type the following code:
String[] list_items = {"One","Two","Three","Four","Five"};
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter adapter = new ArrayAdapter<String>(this,R.layout.list_item, list_items);
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);
}
} 2. In your activity_main.xml type the following code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingTop="16dp"
tools:context="android.demo.MainActivity">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</RelativeLayout>
3. Create a xml file list_item.xml in res>layout folder and type the following code in list_item.xml.
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text"
android:padding="20dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="20dp"
>
</TextView>
Now you are ready to test your application in emulator or physical android phone via USB debugging.
Comments
Post a Comment