Creating Android studio IDE Template for RecyclerView’s

Why RecyclerViewTemplate?

I am an Android techie geek who loves to get things done in an elegant, standard and simple way like carving butter. As an early initiative, I thought of automating the workflow and tasks in Android since I realized that I have been doing repetitive tasks all these days. So here comes this Android studio template to generate boilerplate code for various RecyclerView styles and designs.

How RecyclerViewTemplate can be useful?

The RecyclerView is the most common View where we developers will often be using either as a form of Grid, list, headers, footer, sections, horizontal ScrollView, toggle, checkbox and radio lists. So this repo helps to generate boilerplate code with an ease by just entering and checking the fields in the wizard. (No coding skills required :-))

Behind the scenes?

RecyclerViewTemplate primarily uses Textfield, Spinner, Checkboxes for UI presentation. Please refer my previous article if you are not familiar with Android Studio Template UI creation https://medium.com/androidstarters/mastering-android-studio-templates-ed8fdd98cb78

It’s time to check core logic and template window structure.

  1. How to achieve toggle behaviour similar to the UI shown below?

The below code snippet is used to create Need Fragment code instead of Activity? CheckBox UI component.

Next comes changing the state of TextField based on isFragment logic. Let’s take Activity Name and Fragment Class Name as a example.

Activity Name

Fragment Class Name

Let’s break this into simple logic as follows:

if(isFragment){ //If Need Fragment code instead of Activity is checked
SHOW_FRAGMENT_UI;
}else{ //If Need Fragment code instead of Activity is unchecked
SHOW_ACTIVITY_UI;
}

2. How code generation works based on isFragment true/false states?

This can be achieved through simple if-else logic from a recipes.xml.ftl file using following expression

Where,

${className} = Fragment Class Name entered in the text field

${activityClass} = Activity Name entered in the text field

(i.e) ${YOUR_TEXTFIELD_ID} gets the value from text field similar to the behavior of edittext.getText().toString() in Android.

How to load exact layouts and java class files according to user selected option?

Let’s see how adapter layouts are handled for different styles

item_recycler_list.xml.ftl

Similarly, the layout manager behavior can be handled through following checks in RecyclerViewActivity.java or RecyclerViewFragment.java classes as stated below

RecyclerViewTemplate Styles and Types:

Below are the possible RecyclerView designs and styles that can be generated using RecyclerViewTemplate.

Recommended Reading

If you liked this post, hit the 👏 . Stay tuned for the next one!

Also share AndroidStarters and give us some feedback in the comments below.