Showing posts with label list. Show all posts
Showing posts with label list. Show all posts

Tuesday, 12 September 2017

Android Recycler View Example with Kotlin

Now as google has officially announced kotlin as official language for Android Development, today I am writing a tutorial about creating Android Recycler View with kotlin. Though Android Studio 3.0 is not released yet, but you can get the beta release for android development in kotlin. Visit the link to download android studio 3.0 beta release (get the stable build if released).
Here is the final image of our recyclerview.
Recycler View with kotlin-Android Tech Point
Recycler View Using Kotlin - Android Tech Point

Steps to Create Recycler View

You can create your recycler view by following these simple steps
  • Add the recycler view in your xml.
  • Add a new layout xml file for the list item and add card view in it.
  • Create your data class.
  • Write your adapter class.
  • Set the adapter for the recycler view.
And that's all. Now we will see the implementation in a bit details with the actual code.

Implementation for Recycler View with Kotlin

  • Drag  and drop the recycler view, android studio will automatically add the dependency for recycler view in the Build,gradle(app).
    Android Tech Point - Recycler View
    Recycler View - Android Tech Point

    Here is the code for the recycler view in activity_main.xml
  • Create a new xml layout file and name it list_item.xml. Drag and drop the card view in the design section (Android Studio will automatically add the dependency for card view) create the design for a single row.
  • Now we will add the data class or you can say POJO class in kotlin. It is very very simple in kotlin. You don't have to write any boilerplate code.
  • Next step is to create our RecyclerViewAdapter.kt. It is pretty much same as we usually do in java.
  • Its time to use our custom adapter and display the list of items on the screen. In your main activity, set the adapter for the recycler view.

Final Words

You can get the complete tutorial at GitHub. Happy Coding!! :) For suggestions and help feel free to comment below or contact me at farooqahmadkhan003@gmail.com.


Monday, 16 January 2017

Android Expandable ListView / ExpandableListView

ExpandableListView is kind of advanced version of Listview. You can expand the list items. It is very useful when you have categories and sub categories of data. Here a tutorial using for ExpandableListView with event listener of parent and child list items.


All you need to do is to follow the simple steps
  • Create main_activity.xml.
  • Create list_group.xml
  • Create list_item.xml.
  • Create the ExpandableListAdapter.java.
  • Create MainActiviy.java.
In ExpandleListAdapter.java, you need to add the @Override some methods of BaseExpandableListAdapter class. important methods are getGroupView and getChildView. In these methods, you need to hook the (in our example) TextView and ImageView of the main list and sublist respectively.

The file main_activity.xml contain the ExpandableListView tag. list_item.xml and list_gruop.xml has the TextViews for list and sublist.

You can get the complete project at Github Repository. Or you can get the code below in this post.

Here is you main_activiy.xml
list_group.xml
list_item.xml
ExpandableListAdapter.java
MainActivity.java