DEV Community

Chandrasekar Kuppusamy
Chandrasekar Kuppusamy

Posted on • Originally published at Medium on

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.

TakeoffAndroid/RecyclerViewTemplate

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.

https://medium.com/media/7c197f896ed30c4f56c40de7bccc78cf/href

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

https://medium.com/media/27d4798007fd0c681a67a36c9ada6a49/href

Fragment Class Name

https://medium.com/media/46d9807f2abbab39b6337c434448330d/href

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;
}
Enter fullscreen mode Exit fullscreen mode
  1. 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

https://medium.com/media/bd471d4894615bb9cb79772240e17fcb/href

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

https://medium.com/media/7284c6bd9f07b86113b3c8f9834cb690/href

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

https://medium.com/media/104f4f82ab23a0b1ca5b4773d4db6acf/href

RecyclerViewTemplate Styles and Types:

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

https://medium.com/media/0e4f748404ec110b5b1098fb2a912f95/href

Recommended Reading

Introducing AndroidStarters

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.


Top comments (0)