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

Android - Simple Custom List View With Image

Ricis

Gadget Geek
R Rep
0
0
0
Rep
0
R Vouches
0
0
0
Vouches
0
Posts
93
Likes
16
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 500 XP
Operating System

Android

In this tutorial we will create a Simple Custom List View With Image using Android. This simple app display a list with image using custom array. Android is basically a piece of software which allows your hardware to function. It used in several gadget like smartphone, tablet, and even television. The android is an open source operating system it's free and user friendly to mobile developers. 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 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:

    tools=

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

    layout_width=

    "match_parent"
  5. android:

    layout_height=

    "match_parent"
  6. tools:

    context=

    "com.razormist.simplelistviewwithimage.MainActivity"

    >

  7. <

    ListView


  8. android:

    layout_width=

    "match_parent"
  9. android:

    layout_height=

    "match_parent"
  10. android:

    id=

    "@+id/lv_content"

    />

  11. </

    RelativeLayout>

Now that we're done with the main layout we will create then a layout that display the value of the list. First right click on resource then create a new layout resource namely laptop_list then write these codes inside your layout file.
  1. <?

    xml version=

    "1.0"

    encoding=

    "utf-8"

    ?>
  2. <

    LinearLayout xmlns:

    android=

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

    app=

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

    orientation=

    "horizontal"

    android:

    layout_width=

    "match_parent"
  5. android:

    layout_height=

    "match_parent"

    >


  6. <

    LinearLayout
  7. android:

    orientation=

    "horizontal"
  8. android:

    layout_width=

    "match_parent"
  9. android:

    layout_height=

    "wrap_content"

    >

  10. <

    TextView
  11. android:

    id=

    "@+id/tv_list"
  12. android:

    layout_width=

    "wrap_content"
  13. android:

    layout_height=

    "wrap_content"
  14. android:

    text=

    "TextView"
  15. android:

    layout_marginTop=

    "30dp"
  16. android:

    fontFamily=

    "monospace"

    />

  17. <

    LinearLayout
  18. android:

    orientation=

    "horizontal"
  19. android:

    layout_width=

    "match_parent"
  20. android:

    layout_height=

    "wrap_content"
  21. android:

    gravity=

    "right"

    >

  22. <

    ImageView
  23. android:

    id=

    "@+id/iv_image"
  24. android:

    layout_width=

    "wrap_content"
  25. android:

    layout_height=

    "wrap_content"
  26. app:

    srcCompat=

    "@mipmap/ic_launcher"
  27. android:

    padding=

    "3dp"

    />

  28. </

    LinearLayout>
  29. </

    LinearLayout>

  30. </

    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.simplelistviewwithimage"

    >

  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>

Creating the Custom Adapter

This code handle the list of data that will be needed to display. First create a new java file namely ListHandler, then open the newly create file and extend the class by adding extends ArrayAdapter

. Next write these variables inside the ListHandler class
  1. String

    [

    ]

    name_list;
  2. int

    [

    ]

    laptop;
  3. Context

    context;

After assigning the variables we will now create a certain methods to handle each data that will be listed. To do that write these methods inside the class.
  1. public

    ListHandler(

    Context

    context, String

    [

    ]

    name_list, int

    [

    ]

    laptop)

    {
  2. super

    (

    context, R.layout

    .laptop_list

    )

    ;
  3. this

    .name_list

    =

    name_list;
  4. this

    .laptop

    =

    laptop;
  5. this

    .context

    =

    context;
  6. }

  7. @Override
  8. public

    int

    getCount(

    )

    {
  9. return

    name_list.length

    ;
  10. }

  11. @NonNull
  12. @Override
  13. public

    View

    getView(

    int

    position, View

    convertView, ViewGroup parent)

    {
  14. TagHolder tagHolder =

    new

    TagHolder(

    )

    ;
  15. if

    (

    convertView ==

    null

    )

    {
  16. LayoutInflater inflater =

    (

    LayoutInflater)

    context.getSystemService

    (

    Context

    .LAYOUT_INFLATER_SERVICE

    )

    ;
  17. convertView =

    inflater.inflate

    (

    R.layout

    .laptop_list

    , parent, false

    )

    ;
  18. tagHolder.iv_image

    =

    (

    ImageView)

    convertView.findViewById

    (

    R.id

    .iv_image

    )

    ;
  19. tagHolder.tv_list

    =

    (

    TextView)

    convertView.findViewById

    (

    R.id

    .tv_list

    )

    ;
  20. convertView.setTag

    (

    tagHolder)

    ;
  21. }

    else

    {
  22. tagHolder =

    (

    TagHolder)

    convertView.getTag

    (

    )

    ;
  23. }

  24. tagHolder.iv_image

    .setImageResource

    (

    laptop[

    position]

    )

    ;
  25. tagHolder.tv_list

    .setText

    (

    name_list[

    position]

    )

    ;
  26. return

    convertView;
  27. }

  28. static

    class

    TagHolder{
  29. ImageView iv_image;
  30. TextView tv_list;

  31. }

The Main Function

This code contains the main function of the application. This code will automatically call the list method to display it inside the resource layout. To start with first locate your MainActivity java file and open it, then write this variable inside the MainActivity class.
  1. ListView

    lv_content;

  2. String

    [

    ]

    name_list =

    {
  3. "ASUS VivoBook Max X441UR"

    ,
  4. "Acer Aspire One D270-268"

    ,
  5. "Lenovo Ideapad 100S 11"

    ,
  6. "Dell Inspiron 11-3162"

    ,
  7. "HP 11-D002TU"

    ,
  8. "MSI GV62 7RC"

    ,
  9. "Sony Vaio VPCW126AG"
  10. }

    ;

  11. int

    [

    ]

    laptop =

    {
  12. R.drawable

    .asus_vivobook_max_x441ur_l

    ,
  13. R.drawable

    .acer_aspire_one_d270_268_l

    ,
  14. R.drawable

    .dell_inspiron_11_3162_l

    ,
  15. R.drawable

    .lenovo_ideapad_100s_11_ph_l

    ,
  16. R.drawable

    .hp_11_d002tu_l

    ,
  17. R.drawable

    .msi_gv62_7rc_l

    ,
  18. R.drawable

    .sony_vpcw_126ag_l_5
  19. }

    ;

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

    (

    ListView

    )

    findViewById(

    R.id

    .lv_content

    )

    ;
  2. ListHandler listHandler =

    new

    ListHandler(

    MainActivity.this

    , name_list, laptop)

    ;
  3. listHandler.sort

    (

    new

    Comparator<

    String>

    (

    )

    {
  4. @Override
  5. public

    int

    compare(

    String

    lhs, String

    rhs)

    {
  6. return

    lhs.compareTo

    (

    rhs)

    ;
  7. }
  8. }

    )

    ;
  9. lv_content.setAdapter

    (

    listHandler)

    ;

Try to run the app and see if it worked.

There you have it we have created a Simple Custom List View With Image 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 the hidden content.
 

452,496

327,345

327,353

Top