ListView is a commonly used widget in android which allow us to display several items in a vertical scrollable list. The list items are automatically inserted to the list using an adapter that pulls content from a source such as an array or database. In this tutorial we will see how to implement a simple listview in android. Let us start by creating a new project in Android studio.
1.Creating New Project
- Create a new project in Android Studio from File -> New Project
- Fill the required fields like Application name and package name and click Next
- Select Basic activity and click Next, and click Finish Button.A default activity will be created as MainActivity.java with layouts activity_main.xml and content_main.xml
2. Here is the code for your activity_main.xml
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context="codes4.com.simplelistview.MainActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <include layout="@layout/content_main" /> </android.support.design.widget.CoordinatorLayout>
3. Next step is to add the code for content_main.xml
content_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" 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" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="codes4.com.simplelistview.MainActivity" tools:showIn="@layout/activity_main"> <ListView android:id="@+id/lv_languages" android:layout_height="wrap_content" android:layout_width="match_parent"> </ListView> </RelativeLayout>
4. Finally add the following code to MainActivity.java which will contain reference of above created listview in layout
and and Array adapter for listview. Using ArrayAdapter you can insert the data into listview.
package codes4.com.simplelistview; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.widget.ArrayAdapter; import android.widget.ListView; public class MainActivity extends AppCompatActivity { Toolbar toolbar; ListView lv_languages; ArrayAdapter<String> list_adapter; String[] languages = new String[] { "SQL", "JAVA", "JAVA SCRIPT", "C#", "PYTHON", "C++", "PHP", "IOS", "ANDROID" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); lv_languages.setAdapter(list_adapter); } private void init() { toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setTitle("Languages"); list_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, languages); lv_languages = (ListView) findViewById(R.id.lv_languages); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
Output

Since the above shown is an example of Android simple ListView,but if you want to create a custom listview with images or any other layout based on your desire , you may need to use custom listview adapter. Refer the following tutorial to see how to implement custom listview in android How to implement custom listview in android
Hi Ramees ! You are not defining any where or providing steps to add @dimen resource which you are refering in content_main.xml file