Android Fragment Code Generator beta

A simple boilerplate code generator for Android Fragments
X
Select all

package {{packageName}};

import android.app.Fragment; // or import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.os.Bundle;

public class {{className}} extends Fragment {
  
private static final String KEY_PREFIX = "{{packageName}}.{{className}}.";

private static final String ARG_{{argument.name|uppercase}}_KEY = KEY_PREFIX + "{{argument.name|lowercase}}-key";
private {{argument.argType.qualifiedName}} {{argument.name|asField}}; public static {{className}} getInstance({{argumentList}}) { {{className}} fragment = new {{className}}(); Bundle bundle = new Bundle(); bundle.put{{argument.argType.name}}(ARG_{{argument.name|uppercase}}_KEY, {{argument.name}}); fragment.setArguments(bundle); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { {{argument.name|asField}} = getArguments().get{{argument.argType.name}}(ARG_{{argument.name|uppercase}}_KEY); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.{{layout|lowercase|stripExtension}}, container, false); return view; } public {{argument.argType.qualifiedName}} get{{argument.name|capitalize}}() { return {{argument.name|asField}}; } }