Wednesday 25 September 2013

Android ListView Example

Overview: 

 In this tutorial i will show how to build simple android ListView. This tutorial is all about creating listview.


What is the ListView & Why we used it?  


ListView is a view group that displays a list of scrollable items. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database query and converts each item result into a view that's placed into the list.So to show the content in list fashion we used ListView.


Now Creating a project in eclipse for ListView.
  •  Create a new project by going to File > New > Android Application Project. Fill all the details and name of your activity,I have taken it ListViewActivity.                                                                                                
  • Copy paste all code that i have given below under your main activity called ListViewActivity.
package com.rkt.listviewexample;

import java.util.ArrayList;
import java.util.Arrays;
import com.rkt.listviewexample.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ListViewActivity extends Activity {

    private ListView mainListView;
    private ArrayAdapter<String> listAdapter;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);

        // Find the ListView resource.
        mainListView = (ListView) findViewById(R.id.Androidlist);

        // Create and populate a List of Country names.
        String[] countries = new String[] { "India", "Iran", "Canada",
                "Turkey", "Zimbabwe", "Iceland", "Bahrain", "Australia",
                "Afghanistan" };
        ArrayList<String> al = new ArrayList<String>();
        al.addAll(Arrays.asList(countries));

        // Create ArrayAdapter using the ArrayList al.
        listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, al);



//here you can add items directly in listAdapter
        listAdapter.add("Malaysia");
        listAdapter.add("Mexico");
        listAdapter.add("France");
        listAdapter.add("Israel");
        listAdapter.add("Mongolia");

        // Set the ArrayAdapter as the ListView's adapter.
        mainListView.setAdapter(listAdapter);
    }
}


  • Lets start your work on UI part,you need a xml file that show our item's in list manner. So create an XML file under res folder and name it as activity_list_view.xml and paste the following code.
( Right Click on res/layout ⇒ New ⇒ Android XML File)

 <LinearLayout 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:orientation="vertical" >

    <ListView
        android:id="@+id/Androidlist"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffff00" >
    </ListView>

</LinearLayout>


  •  Now you have to create one more xml file  to show the text in listview.Create an XML file under res/layout folder and name it as simplerow.xml and type the following code under this file.This file only contains the TextView to display the text.
(Again right click on res/layout ⇒ New ⇒ Android XML File

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rowTextView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="15dp"
    android:textColor="#191970"
    android:textColorHighlight="#ffff00"
    android:textSize="16sp"
    android:textStyle="bold" >

</TextView> 

  •  Ok,this is the time to run your project by right clicking on your project folder ⇒ Run As ⇒ Android Application.You should see the following result in your device.

Output:

 Please Share this tutorial On:

27 comments:

  1. Very simple explanation!!!!!!!! Good.

    ReplyDelete
  2. ListView is a view group that displays a list of scrollable items. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database query and converts each item result into a view that's placed into the list.Android Training in chennai | Android Training|Android Training in chennai with placement | Android Training in velachery

    ReplyDelete
  3. Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic.
    Click here:
    Microsoft azure training in chennai
    Click here:
    Microsoft azure training in online

    ReplyDelete
  4. This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me.. 

    Data Science Training in Chennai
    Data science training in bangalore
    Data science online training
    Data science training in pune
    Data science training in kalyan nagar
    selenium training in chennai

    ReplyDelete
  5. This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me.. 
    java training in tambaram | java training in velachery

    java training in omr | oracle training in chennai

    java training in annanagar | java training in chennai

    ReplyDelete
  6. Thank you for taking the time and sharing this information with us. It was indeed very helpful and insightful while being straight forward and to the point.
    Blueprism training in tambaram

    Blueprism training in annanagar

    Blueprism training in velachery

    ReplyDelete
  7. Very well written blog and I always love to read blogs like these because they offer very good information to readers with very less amount of words....thanks for sharing your info with us and keep sharing.

    angularjs Training in bangalore

    angularjs Training in btm

    angularjs Training in electronic-city

    angularjs online Training

    angularjs Training in marathahalli

    ReplyDelete
  8. This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.
    Data Science training in rajaji nagar | Data Science with Python training in chenni
    Data Science training in electronic city | Data Science training in USA
    Data science training in pune | Data science training in kalyan nagar

    ReplyDelete
  9. Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.


    AWS Training in BTM Layout |Best AWS Training in BTM Layout

    AWS Training in Marathahalli | Best AWS Training in Marathahalli

    ReplyDelete
  10. I am really happy with your blog because your article is very unique and powerful for new reader.
    Click here:
    selenium training in chennai
    selenium training in bangalore
    selenium training in Pune
    selenium training in pune
    Selenium Online Training

    http://mindbendingpuzzles.blogspot.com/2014/06/android-tip-3-sharing-folder-between.html

    ReplyDelete
  11. You blog post is just completely quality and informative. Many new facts and information which I have not heard about before. Keep sharing more blog posts.

    devops online training

    aws online training

    data science with python online training

    data science online training

    rpa online training

    ReplyDelete
  12. Very good brief and this post helped me alot. Say thank you I searching for your facts. Thanks for sharing with us!
    Very good brief and this post helped me alot. Say thank you I searching for your facts. Thanks for sharing with us!

    ReplyDelete
  13. @Taskia Sultana, You broken all records of promotions. Buddy This is not a porn site Whr u promoting such content. People are fighting to solve their technical problems and u want them to watch ur porn site.

    ReplyDelete
  14. This is the exact information I am been searching for, Thanks for sharing the required infos with the clear update and required points. To appreciate this I like to share some useful information regarding Microsoft Azure which is latest and newest,
    Data Science Training In Chennai

    Data Science Online Training In Chennai

    Data Science Training In Bangalore

    Data Science Training In Hyderabad

    Data Science Training In Coimbatore

    Data Science Training

    Data Science Online Training

    ReplyDelete
  15. Really Thanks For Posting Such a Useful and informative article. oracle training in chennai

    ReplyDelete
  16. ExcelR provides data scientist course in pune . It is a great platform for those who want to learn and become a data scientist. Students are tutored by professionals who have a degree in a particular topic. It is a great opportunity to learn and grow.

    data scientist course in pune


    ReplyDelete
  17. Want to set your career towards the software field? Then join hands with Infycle Technologies to make this into reality. Infycle Technologies, the best software training center in Chennai, gives the combined and best software training in Chennai, with various stages of multiple courses such as Big Data, Python, Data Science, Oracle, etc., which professional tutors will guide in the field. The Hands-on practical training and the mock interview sessions will be given to the candidates to face the interviews with full confidence. Apart from all, the candidates will be placed in the top MNC's with the highest salary package in the market. To get it all, call 7502633633 and make this happen for your happy life.Best Software Training Center in Chennai | Infycle Technologies

    ReplyDelete
  18. Infycle Technologies offers couples for care and technology in addition to Python Training in Chennai, 100% of the internship class will be prepared. After completing the training, the participants will be sent to the upper MNCs interviews. Call 750633333 to get more information and get a free display.

    ReplyDelete
  19. Recruiting solidity developers involves more than just scanning resumes and profiles. These resources are a great place to start if you have no previous experience hiring Solidity Developers.

    ReplyDelete
  20. We can help your web or mobile app development vision to grow with high-quality, cost-effective services from our react js agency!

    ReplyDelete