Android Studio Group File Template, Custom Path For Child File

638 Views Asked by At

Am trying to create a Group File Template which has to child files

  • FragmentKotlinClass.kt
    • ViewModel.kt
    • fragmetn_layout.mxl

But the problem is that all three files created in the same directory
The question is How to specify the directory name in the File Name Template for the .xml file to put it in res/layout/

enter image description here

1

There are 1 best solutions below

0
On

You can create file in upper directory by using upper directory prefix ../fragment.xml or ../../fragment.xml

So the challange is finding how many times we should go upper. I used $PACKAGE_NAME variable and some regex to solve this problem. First I go up until project root, then I go down to /res/layout/ and finally added my fragment file in snake case format.

So you can try this one-liner solution in your template's File Name:

#set( $LAYOUT_FILE = $PACKAGE_NAME.replaceAll("\.", "/").replaceAll("\w+","\.\.").concat("/../res/layout/").concat($NAME.replaceAll("([a-z])([A-Z]+)", "$1_$2").toLowerCase()) )${LAYOUT_FILE}