Simple Android ListView without ListActivity

Few days ago, I was creating an Android app for which I needed to implement ListView in my activity. As I was using the Appcompat library, I had to extend my activity from ActionBarActivity and not ListActivity. For those of you who don’t know, Appcompat is a support library provided by Google which lets you port features introduced in newer Android versions to older Android versions starting from Android 2.2 (API Level 8) and up. One such example is using the same Action Bar for all Android versions.

Implementing a ListView with ListActivity is very easy, however with ActionBarActivity this was not the case. So, I did some searching on the internet. I found some solutions but most of them seemed too complex for such a simple task. So I fiddled with my code a bit and came with a very simple solution to implement ListView without extending ListActivity (or with extending ActionBarActivity).

So, lets dive into the actual code now.

First create a new Android project in Eclipse (assuming you are using that, I do). If you are using the latest Android development tools, it will automatically include the Appcompat library in your project.  If not, you can include it manually.

Your project should include these files by default (MainActivity.java in /src folder and activity_main.xml in /res/layout folder). Lets start with layout file first. Put this code into the file.

activity_main.xml

This code is pretty simple and self-explanatory. I created a  RelativeLayout with vertical orientation and added a ListView with id “actresslist” as its child. I normally use RelativeLayout because it provides more flexibility than LinearLayout and it is easier to add other UI elements like banner ads.

Since we are working in the layout directory, lets add another file to it. You can do this by clicking on “New Android XML File” in the toolbar and naming the file “list_item.xml” and clicking finish. After that, add this code to the file.

list_item.xml

This provides the layout for individual list item for our ListView. I could use the default Android layout for simple list item but this gives you more flexibility and is easier to use with ActionBarActivity. I just added a TextView but you could add other elements too like ImageView and EditText.

Now, lets focus on Java source code for our main activity and bind these layout files together. Add this code to “MainActivity.java” file in your project.

MainActivity.java

Here, we are extending our MainActivity class from ActionBarActivity. Then using the setContentView in the onCreate method, we are setting the view of our main activity from layout file “activity_main.xml“. Then we created a string array named “actressArray” and filled it with our values. We can also use something like ArrayList or Hashmap here, but string array is pretty simple and faster.

After that, we declare a ListView object “lv” and assign it the list view from our main layout file. Then we create a simple ArrayAdapter and set it as an adapter for our list view. The ArrayAdapter has following arguments – “this” for activity context, “R.layout.list_item” for individual list item, “R.id.actress_name” for textview and “actressArray” for listview data.

After that, run the project and you will have a result similar to this:

Android ListView without ListActivity

So, thats it. We have successfully created a ListView without extending ListActivity in our main activity. Hope this tutorial was helpful.

If you face any problems, leave a comment below. I will be glad to help you.

Related Posts

14 Comments

  1. carrera
    October 14, 2014
    • Saurabh Bansal
      October 14, 2014
  2. yiou
    October 24, 2014
    • Saurabh Bansal
      November 1, 2014
      • elvis
        December 30, 2014
  3. Joseph
    November 25, 2014
    • Saurabh Bansal
      November 25, 2014
  4. Aman Tuladhar
    January 1, 2015
  5. Ahmad
    January 10, 2015
  6. Claudiu
    February 4, 2015
  7. Sam
    February 25, 2015
  8. shiva shankar
    March 18, 2015

Add Comment