• We just launched and are currently in beta. Join us as we build and grow the community.

Android - Simple Search List View

Islifer

Loot Hoarder
I Rep
0
0
0
Rep
0
I Vouches
0
0
0
Vouches
0
Posts
102
Likes
149
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
Operating System

Android

In this tutorial we will try to create a Simple Search List View using Android. This code can search any word through entering the keyword. Android is the world’s most widely used operating system with over a millions of user, that are already installed to smartphones, tablets, and even television. Android is an open source so that developer find it easy to develop and expand new features. So let's 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 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=

    "http://schemas.android.com/apk/res/android"
  3. xmlns:

    app=

    "http://schemas.android.com/apk/res-auto"
  4. xmlns:

    tools=

    "http://schemas.android.com/tools"
  5. android:

    layout_width=

    "match_parent"
  6. android:

    layout_height=

    "match_parent"
  7. tools:

    context=

    "com.razormist.simplesearchlistview.MainActivity"

    >

  8. <

    EditText
  9. android:

    id=

    "@+id/et_search"
  10. android:

    layout_width=

    "wrap_content"
  11. android:

    layout_marginTop=

    "10dp"
  12. android:

    layout_alignParentTop=

    "true"
  13. android:

    layout_marginLeft=

    "5dp"
  14. android:

    ems=

    "10"
  15. android:

    layout_height=

    "wrap_content"
  16. android:

    hint=

    "Search here..."

    />

  17. <

    ListView


  18. android:

    id=

    "@+id/lv_content"
  19. android:

    layout_height=

    "wrap_content"
  20. android:

    layout_width=

    "wrap_content"
  21. android:

    layout_below=

    "@+id/et_search"

    />
  22. </

    RelativeLayout>

After that we will create a layout that will display all the list. To do that right click on the layout select New then select resource file, and name it as list_content.xml. Write this certain xml code inside the newly created resource file.
  1. <?

    xml version=

    "1.0"

    encoding=

    "utf-8"

    ?>
  2. <

    LinearLayout xmlns:

    android=

    "http://schemas.android.com/apk/res/android"
  3. android:

    orientation=

    "vertical"

    android:

    layout_width=

    "match_parent"
  4. android:

    layout_height=

    "match_parent"

    >

  5. <

    TextView
  6. android:

    id=

    "@+id/tv_list"
  7. android:

    layout_height=

    "wrap_content"
  8. android:

    layout_width=

    "wrap_content"
  9. android:

    padding=

    "10dp"
  10. />
  11. </

    LinearLayout>

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=

    "http://schemas.android.com/apk/res/android"
  3. package

    =

    "com.razormist.simplesearchlistview"

    >

  4. <

    application
  5. android:

    allowBackup=

    "true"
  6. android:

    icon=

    "@mipmap/ic_launcher"
  7. android:

    label=

    "@string/app_name"
  8. android:

    roundIcon=

    "@mipmap/ic_launcher_round"
  9. android:

    supportsRtl=

    "true"
  10. android:

    theme=

    "@Style/AppTheme"

    >
  11. <

    activity android:

    name=

    ".MainActivity"
  12. android:

    configChanges=

    "orientation"
  13. android:

    screenOrientation=

    "portrait"

    >
  14. <

    intent-

    filter>
  15. <

    action android:

    name=

    "android.intent.action.MAIN"

    />

  16. <

    category android:

    name=

    "android.intent.category.LAUNCHER"

    />
  17. </

    intent-

    filter>
  18. </

    activity>
  19. </

    application>
  20. </

    manifest>

The Main Function

This code contains the main function of the application. This code will automatically filter the list every time the keyword is entered. To start with first locate your MainActivity java file and open it, then write this variable inside the MainActivity class.
  1. String

    [

    ]

    items;
  2. ArrayList<

    String>

    arrayList;
  3. ArrayAdapter<

    String>

    arrayAdapter;
  4. ListView

    lv_content;
  5. EditText et_search;

Then write these method to make to code work correctly.
  1. public

    void

    SearchResult

    (

    String

    keyword)

    {
  2. for

    (

    String

    item:

    items)

    {
  3. if

    (

    !

    item.contains

    (

    keyword)

    )

    {
  4. arrayList.remove

    (

    item)

    ;
  5. }
  6. }

  7. arrayAdapter.notifyDataSetChanged

    (

    )

    ;
  8. }

  9. public

    void

    IntializeList(

    )

    {
  10. items =

    new

    String

    [

    ]

    {
  11. "C"

    ,
  12. "C++"

    ,
  13. "C#"

    ,
  14. "Java"

    ,
  15. "VB"

    ,
  16. "Python"

    ,
  17. "Perl"

    ,
  18. "PHP"

    ,
  19. "Ruby"

    ,
  20. "Delphi"
  21. }

    ;

  22. arrayList =

    new

    ArrayList<>

    (

    Arrays

    .asList

    (

    items)

    )

    ;
  23. arrayAdapter =

    new

    ArrayAdapter<

    String>

    (

    this

    , R.layout

    .list_content

    , R.id

    .tv_list

    , arrayList)

    ;
  24. arrayAdapter.sort

    (

    new

    Comparator<

    String>

    (

    )

    {
  25. @Override
  26. public

    int

    compare(

    String

    lhs, String

    rhs)

    {
  27. return

    lhs.compareTo

    (

    rhs)

    ;
  28. }
  29. }

    )

    ;
  30. lv_content.setAdapter

    (

    arrayAdapter)

    ;
  31. }

Finally, initialize the require methods inside the onCreate method to run the application.
  1. lv_content =

    (

    ListView

    )

    findViewById(

    R.id

    .lv_content

    )

    ;
  2. et_search =

    (

    EditText)

    findViewById(

    R.id

    .et_search

    )

    ;

  3. IntializeList(

    )

    ;
  4. et_search.addTextChangedListener

    (

    new

    TextWatcher(

    )

    {
  5. @Override
  6. public

    void

    beforeTextChanged(

    CharSequence s, int

    start, int

    count, int

    after)

    {

  7. }

  8. @Override
  9. public

    void

    onTextChanged(

    CharSequence s, int

    start, int

    before, int

    count)

    {
  10. if

    (

    s.toString

    (

    )

    .equals

    (

    ""

    )

    )

    {
  11. IntializeList(

    )

    ;
  12. }

    else

    {
  13. IntializeList(

    )

    ;
  14. SearchResult

    (

    s.toString

    (

    )

    )

    ;
  15. }
  16. }

  17. @Override
  18. public

    void

    afterTextChanged(

    Editable s)

    {

  19. }
  20. }

    )

    ;
  21. <

    java>
  22. Try

    to run the app and see if

    it worked.

  23. There

    you have it we have created a <

    strong>

    Simple Search List

    View</

    strong>

    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!!!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.

2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

442,401

317,942

317,951

Top