Live templates for Android Studio 2 – Cheat sheet

Hello, my two visitors and hello Google crawler!

While this is my first post (and possibly the only one, unless some miracle happens) so I should introduce my self…
Nah! You can manage without it, I’m sure!

So this is it – short and …well…just short!

At the top are those live templates, that are most important to me personally – the rest follow later.

Loggers:


Log.d(TAG, String)

logd
android.util.Log.d(TAG, "$METHOD_NAME$: $content$");

Log.e(TAG, String, Exception)

loge

android.util.Log.e(TAG, "$METHOD_NAME$: $content$", $exception$);


Android:


Define android style int constant

const
private static final int $name$ = $value$;

Create a new Toast

Toast
android.widget.Toast.makeText(
$className$.this, "$text$"Toast.LENGTH_SHORT).show();

findViewById with cast

fbc
($cast$) findViewById(R.id.$resId$);

get a String from resources

rgS
$resources$.getString(R.string.$stringId$)

——–

Key for a bundle

key
private static final String KEY_$value$ = "$value$";

String format

Sfmt
String.format("$string$", $params$);

Create a for each loop

foreach
for ($i$ : $data$) {
$cursor$
}

Set view visibility to GONE

gone
$VIEW$.setVisibility(android.view.View.GONE);

Set view visibility to VISIBLE

visible
$VIEW$.setVisibility(View.VISIBLE);

Creates an Intent with ACTION_VIEW

IntentView
android.content.Intent view = new Intent();
view.setAction(Intent.ACTION_VIEW);
view.setData(android.net.Uri.parse($url$));
startActivity(view);

create a new Fragment instance with arguments

newInstance
public static $fragment$ newInstance($args$) {
$nullChecks$
android.os.Bundle args = new Bundle();
$addArgs$
$fragment$ fragment = new $fragment$();
fragment.setArguments(args);
return fragment;
}

private empty constructor to prohibit instance creation

noInstance
private $class$() {
//no instance}

runOnUIThread

rouiT
getActivity().runOnUiThread(new java.lang.Runnable() {
@Override
public void run() {
$cursor$
}
});

Creates a static start(…) helper method to start an Activity

starter
public static void start(android.content.Context context) {
android.content.Intent starter =
new Intent(context,
$ACTIVITY$.class);
    starter.putExtra($CURSOR$);    
context.startActivity(starter);
}

Adds generic view constructors

ViewConstructors
public $class$(android.content.Context context) {
this(context, null);
}
public $class$(Context context, android.util.AttributeSet attrs) {
this(context, attrs, 0);
}
public $class$(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);  
$cursor$
}

Share if thou wilt!

Leave a comment

Your email address will not be published. Required fields are marked *

shares
%d bloggers like this: