Skip to content

Instantly share code, notes, and snippets.

@cyrsis
Forked from bolot/android.xml
Created January 6, 2017 07:56
Show Gist options
  • Save cyrsis/a3a3b88712ba6e2ea726f19354b7e28d to your computer and use it in GitHub Desktop.
Save cyrsis/a3a3b88712ba6e2ea726f19354b7e28d to your computer and use it in GitHub Desktop.
A few live templates for Android Studio. On my Mac, this file lives in `~/Library/Preferences/AndroidStudioPreview1.3/templates/android.xml`. The location of the file depends on the version of Android Studio and the type of the operating system.
<templateSet group="android">
<template name="focvb" value="@Override&#10;public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {&#10; View view = inflater.inflate(R.layout.fragment_$END$, container, false);&#10; ButterKnife.inject(this, view);&#10; return view;&#10;}&#10;" description="Fragment onCreateView with ButterKnife" toReformat="true" toShortenFQNames="true">
<context>
<option name="JAVA_CODE" value="false" />
<option name="JAVA_STATEMENT" value="false" />
<option name="JAVA_EXPRESSION" value="true" />
<option name="JAVA_DECLARATION" value="true" />
<option name="JAVA_COMMENT" value="false" />
<option name="JAVA_STRING" value="false" />
<option name="COMPLETION" value="false" />
</context>
</template>
<template name="fni" value="public static $CLASS_NAME$ newInstance() {&#10; return new $CLASS_NAME$();&#10;}&#10;" description="Fragment newInstance" toReformat="false" toShortenFQNames="true">
<variable name="CLASS_NAME" expression="className" defaultValue="" alwaysStopAt="false" />
<context>
<option name="JAVA_DECLARATION" value="true" />
</context>
</template>
<template name="fnia" value="private static final String $ARG_PARAM$ = &quot;$CLASS_NAME$.$ARG_PARAM$&quot;;&#10;&#10;private $ARG_CLASS_DITTO$ m$INST_VAR$;&#10;&#10;public static $CLASS_NAME$ newInstance($ARG_CLASS$ $ARG_VAR$) {&#10; $CLASS_NAME$ fragment = new $CLASS_NAME$();&#10; Bundle args = new Bundle();&#10; args.put$ARG_CLASS$($ARG_PARAM$, $ARG_VAR$);&#10; fragment.setArguments(args);&#10; return fragment;&#10;}&#10;&#10;@Override&#10;public void onCreate(Bundle savedInstanceState) {&#10; super.onCreate(savedInstanceState);&#10; m$INST_VAR$ = getArguments().get$ARG_CLASS$($ARG_PARAM$);&#10;}&#10;" description="Fragment newInstance with arguments" toReformat="true" toShortenFQNames="true">
<variable name="ARG_CLASS" expression="typeOfVariable(VAR)" defaultValue="" alwaysStopAt="true" />
<variable name="ARG_VAR" expression="suggestVariableName()" defaultValue="" alwaysStopAt="true" />
<variable name="ARG_CLASS_DITTO" expression="ARG_CLASS" defaultValue="" alwaysStopAt="false" />
<variable name="ARG_PARAM" expression="capitalizeAndUnderscore(ARG_VAR)" defaultValue="" alwaysStopAt="false" />
<variable name="CLASS_NAME" expression="className" defaultValue="" alwaysStopAt="false" />
<variable name="INST_VAR" expression="capitalize(ARG_VAR)" defaultValue="" alwaysStopAt="false" />
<context>
<option name="JAVA_DECLARATION" value="true" />
</context>
</template>
<template name="@sn" value="@SerializedName(&quot;$VAR$&quot;)&#10;private $TYPE$ m$INSTVAR$;" description="@SerializedName inst var" toReformat="false" toShortenFQNames="true">
<variable name="VAR" expression="" defaultValue="" alwaysStopAt="true" />
<variable name="TYPE" expression="" defaultValue="" alwaysStopAt="true" />
<variable name="INSTVAR" expression="capitalize(VAR)" defaultValue="" alwaysStopAt="false" />
<context>
<option name="JAVA_DECLARATION" value="true" />
</context>
</template>
<template name="ani" value="public static Intent newIntent(Context context) {&#10; return new Intent(context, $CLASS_NAME$.class);&#10;}&#10;" description="Activity newIntent" toReformat="false" toShortenFQNames="true">
<variable name="CLASS_NAME" expression="className()" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_DECLARATION" value="true" />
</context>
</template>
<template name="ania" value="private static final String $EXTRA_PARAM$ = &quot;$CLASS_NAME$.$EXTRA_PARAM$&quot;;&#10;&#10;public static Intent newIntent(Context context, $EXTRA_CLASS$ $EXTRA_VAR$) {&#10; Intent intent = new Intent(context, $CLASS_NAME$.class);&#10; intent.putExtra($EXTRA_PARAM$, $EXTRA_VAR$);$END$&#10; return intent;&#10;}&#10;&#10;@Override&#10;protected Fragment getFragment() {&#10; $EXTRA_CLASS$ $EXTRA_VAR$ = getIntent().get$EXTRA_CLASS$Extra($EXTRA_PARAM$);&#10; return $FRAGMENT_CLASS$.newInstance($EXTRA_VAR$);&#10;}&#10;" description="Activity newIntent with arguments" toReformat="false" toShortenFQNames="true">
<variable name="EXTRA_CLASS" expression="typeOfVariable(VAR)" defaultValue="" alwaysStopAt="true" />
<variable name="EXTRA_VAR" expression="suggestVariableName()" defaultValue="" alwaysStopAt="true" />
<variable name="CLASS_NAME" expression="className" defaultValue="" alwaysStopAt="false" />
<variable name="EXTRA_PARAM" expression="capitalizeAndUnderscore(EXTRA_VAR)" defaultValue="" alwaysStopAt="false" />
<variable name="FRAGMENT_CLASS" expression="groovyScript(&quot;_1.replaceAll('Activity','Fragment')&quot;, CLASS_NAME)" defaultValue="" alwaysStopAt="false" />
<context>
<option name="JAVA_DECLARATION" value="true" />
</context>
</template>
<template name="toast" value="Toast.makeText($context$, &quot;$text$&quot;, Toast.LENGTH_SHORT).show();" description="Toast in fragment or activity" toReformat="false" toShortenFQNames="true">
<variable name="context" expression="groovyScript(&quot;_1.endsWith('Activity') ? _1+'.this' : 'getActivity()'&quot;, className())" defaultValue="" alwaysStopAt="false" />
<variable name="text" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_STATEMENT" value="true" />
</context>
</template>
</templateSet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment