Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a kotlin live template for newInstance Fragments using Android studio

I'm searching for a way to create a new Kotlin live code template so that whenever I type newIns.... it and hit tab it will be able to print the following as a live template choice:

companion object {
    fun newInstance(b: Bundle): DetailsFragment {
        val frag = DetailsFragment()
        frag.arguments = b
        return frag
    }
}

In Java, it was done the same way and there is already a "newInstance" abbreviation and a live template exists in Android Studio. I want the same thing for Kotlin. Let me show you a photo :

enter image description here

Notice that, Java Android already has newInstance template. I want this for Kotlin. Here is what I have so far:

enter image description here

and the template code I have so far looks like this:

companion object { 
    fun newInstance($args$:Bundle):$fragment$ {
        $nullChecks$
        android.os.Bundle args = Bundle();
        $addArgs$
        $fragment$ fragment = $fragment$();
        fragment.setArguments(args);
        return fragment;
    }
}

but when I exit the settings and in Kotlin type the first few words of the abbreviation and hit tab or ctrl + spacebar on mac nothing happens. I guess I have the syntax wrong, I'm not sure. Anyone of suggestions?

like image 859
j2emanue Avatar asked Jul 23 '18 07:07

j2emanue


People also ask

How do I create an instance of fragments in Kotlin?

The best practice on Android for creating a Fragment is to use a static factory method and pass arguments in a Bundle via setArguments() . This makes sense to support interop with Java so it can still be called via MyFragment.

What is Live Templates in Android Studio?

Live templates are a feature that JetBrains thoughtfully included in IntelliJ, and by extension, Android Studio. By using them, you can quickly and intelligently add frequently used code patterns and constructs to your code, letting the IDE do the tedious work for you.

What is the process of starting a new fragment in Android?

Use replace() to replace an existing fragment in a container with an instance of a new fragment class that you provide. Calling replace() is equivalent to calling remove() with a fragment in a container and adding a new fragment to that same container. transaction. commit();

How fragments can be created?

To create a blank Fragment , expand app > java in Project: Android view, select the folder containing the Java code for your app, and choose File > New > Fragment > Fragment (Blank).


2 Answers

Step 1:

Go to Live Templates section in Android Studio.

For Windows:

File > Settings > Editor > Live Templates

For Mac:

Android Studio > Preferences > Editor > Live Templates

Step 2:

Select Kotlin template group. Then tap on + present at the top-right corner of the popup. Select Live Template.

Step 3:

Now you can add your live template. Check at the bottom of the popup.

Add abbreviation: newInstance

Add description: Creates an instance of the fragment with arguments

Add template text:

companion object {
    fun newInstance(args: Bundle): $fragment$ {
        val fragment = $fragment$()
        fragment.arguments = args
        return fragment
    }
}

Add applicable context. Tap on Define. Select Kotlin from the list.

Select Reformat according to style

Step 4:

Tap on Edit Variables below the description.

Now tap on Expression for the variable name fragment. Tap on down arrow. You can see a list of expressions. From there select kotlinClassName().

Tap on OK of the Edit Templates Variable

Now tap on Apply and OK of the Live Templates.

To check type newInstance in a Fragment written in Kotlin.

like image 106
Avijit Karmakar Avatar answered Oct 09 '22 13:10

Avijit Karmakar


A slightly simplified version (and more idiomatic) would be:

fun newInstance($args$) : $fragment$ = 
    $fragment$().apply {
        arguments = Bundle().apply {
            $addArgs$
        }
    }

or

fun newInstance(args: Bundle) : $fragment$ = 
    $fragment$().apply {
        arguments = args
    }
like image 30
jt-gilkeson Avatar answered Oct 09 '22 13:10

jt-gilkeson