Android - Simple List View

Submitted by: 
Operating System: 
Visitors have accessed this post 1676 times.

In this tutorial we will try to create a Simple List View using Android. This simple app can be used to display multiple data as a list. Android also provide an adaptive framework that allow the developer to develop an apps in a simpler way. It used in several gadget like smartphone, tablet, and even television. Android is open source to developers who has an interest in developing mobile apps. So let's now do the coding...

Getting Started:

First you will have to download & install the Android Development IDE (Android Studio or Eclipse). Android Studio is an open source development feel free to develop your things.

Here's the link for the Android Studio https://developer.android.com/studio/index.html.

Layout Design

We will now create the design for the application, first to do is to create a resource that we will be using for the design. To create the resource, go to drawable folder then create a new xml file named border. Then write these certain block of codes inside the newly created file.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:android="<a href="http://schemas.android.com/apk/res/android"" rel="nofollow">http://schemas.android.com/apk/res/android"</a> android:shape="rectangle" >
  3. <solid android:color="@android:color/white" />
  4. <stroke android:width="2dp" android:color="#5BC0DE"/>
  5. </shape>

Now that we're done with the graphic resource we will create then a layout that display the value of the list. First locate the layout file called name_list.xml, then write these codes inside your layout file.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TextView xmlns:android="<a href="http://schemas.android.com/apk/res/android"
  3. " rel="nofollow">http://schemas.android.com/apk/res/android"
  4. </a> android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:textSize="30sp"
  7. android:padding="15dp">
  8.  
  9. </TextView>

Now that all the settings are ready we will now create the layout of the app. First locate the layout file called activity_main.xml, this is the default name when create a new activity. Then write these codes inside your layout file.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android"
  3. " rel="nofollow">http://schemas.android.com/apk/res/android"
  4. </a> xmlns:app="<a href="http://schemas.android.com/apk/res-auto"
  5. " rel="nofollow">http://schemas.android.com/apk/res-auto"
  6. </a> xmlns:tools="<a href="http://schemas.android.com/tools"
  7. " rel="nofollow">http://schemas.android.com/tools"
  8. </a> android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. tools:context="com.razormist.simplelistview.MainActivity">
  11.  
  12. <TextView
  13. android:id="@+id/tv_title"
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content"
  16. android:layout_alignParentTop="true"
  17. android:text="Programming Languages"
  18. android:textSize="30sp"
  19. android:gravity="center"
  20. android:layout_marginTop="20dp"
  21. android:layout_marginBottom="20dp"
  22. android:background="@drawable/border"/>
  23.  
  24. android:layout_below="@+id/tv_title"
  25. android:id="@+id/lv_display"
  26. style="@style/Widget.AppCompat.ListView"
  27. android:layout_width="match_parent"
  28. android:layout_height="match_parent"
  29. android:layout_alignParentLeft="true"
  30. android:layout_alignParentStart="true"
  31. android:footerDividersEnabled="false"
  32. android:headerDividersEnabled="true" />
  33.  
  34. </RelativeLayout>

Android Manifest File

The Android Manifest file provides essential information about your app to the Android system in which the system must required before running the code. It describe the overall information about the application. It contains some libraries that needed to access the several method within the app.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="<a href="http://schemas.android.com/apk/res/android"
  3. " rel="nofollow">http://schemas.android.com/apk/res/android"
  4. </a> package="com.razormist.simplelistview">
  5.  
  6. <application
  7. android:allowBackup="true"
  8. android:icon="@mipmap/ic_launcher"
  9. android:label="@string/app_name"
  10. android:roundIcon="@mipmap/ic_launcher_round"
  11. android:supportsRtl="true"
  12. android:theme="@style/AppTheme">
  13. <activity android:name=".MainActivity"
  14. android:configChanges="orientation"
  15. android:screenOrientation="portrait">
  16. <intent-filter>
  17. <action android:name="android.intent.action.MAIN" />
  18.  
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. </application>
  23.  
  24. </manifest>

The Main Function

This code contains the main function of the application. This code will display all the list that have been store to an array of data. To start with first locate your MainActivity java file and open it, then write these some important variables inside the MainActivity class.

  1. private ListView listView;
  2. private static String[] LANG = new String[] {"JavaScript", "Java", "Python", "Ruby", "PHP", "C++", "C", "Shell", "C#", "Objective-C", "R", "Viml", "Go", "Perl", "CoffeScript", "TeX", "Scala", "Haskell", "Emacs Lips"};

Then write these several method inside the activity class to make the apps work properly.

  1. void ListItem(){
  2. listView = (ListView)findViewById(R.id.lv_display);
  3. ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.name_list,LANG);
  4. adapter.sort(new Comparator<String>() {
  5. @Override
  6. public int compare(String lhs, String rhs) {
  7. return lhs.compareTo(rhs);
  8. }
  9. });
  10. listView.setAdapter(adapter);
  11. listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  12. @Override
  13. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  14. String display = (String)listView.getItemAtPosition(position);
  15. Toast.makeText(MainActivity.this, display, Toast.LENGTH_SHORT).show();
  16. }
  17. });
  18. }

Finally, initialize all the methods inside the onCreate method to run the application.

  1. ListItem();

Try to run the app and see if it worked.

There you have it we create a Simple List View using Android. I hope that this tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.