Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save harryhan24/530ec81c7c49a07055c3c0d8654b18f6 to your computer and use it in GitHub Desktop.

Select an option

Save harryhan24/530ec81c7c49a07055c3c0d8654b18f6 to your computer and use it in GitHub Desktop.
Kotlin Android Extension

Android Arsenal

Depracated - use Anko by jetbrains

Android Kotlin Extensions

A collection of Kotlin extensions for Android, based on KotlinAndroidLib and Android Kotlin Extensions.

Include this library in your project

Step 1. Add the JitPack repository to your build file

repositories {
	    maven {
	        url "https://jitpack.io"
	    }
	}

Step 2. Add the dependency in the form

dependencies {
	        compile 'com.github.yoavst:androidKotlin:1.5.1'
	}

Activities & Services

Example: Start an Activity

Java

Intent intent = new Intent(this, SuperPowersActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

Kotlin

val flags = flags(Intent.FLAG_ACTIVITY_NEW_TASK, Intent.FLAG_ACTIVITY_CLEAR_TOP)
start<SuperPowersActivity>(flags)

Example: Create an Intent

Java

Intent intent = new Intent(this, SuperPowersActivity.class);

Kotlin

val intent = intent<SuperPowersActivity>()

Api

Java

int sdk = Build.VERSION.SDK_INT;
if (sdk >= Build.VERSION_CODES.LOLLIPOP) {}
if (sdk < Build.VERSION_CODES.LOLLIPOP) {}
if (sdk >= Build.VERSION_CODES.KITKAT) {}
if (sdk < Build.VERSION_CODES.KITKAT) {}
if (sdk >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {}
if (sdk < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {}
if (sdk >= X) {}
if (sdk < X) {}

Kotlin

val sdk = currentVersion()
if (lollipopOrNewer()) {}
if (beforeLollipop()) {}
if (kitkatOrNewer()) {}
if (beforeKitkat()) {}
if (icsOrNewer()) {}
if (beforeIcs()) {}
if (versionOrNewer(x)) {}
if (beforeVersion(x)) {}

Async and main thread

Kotlin

async {

}

mainThread {

}

Broadcast Receiver

Java

new BroadcastReceiver() {
			@Override
			public void onReceive(Context context,Intent intent) {
			}
		};

Kotlin

broadcastReceiver { context,intent -> }

Layout Inflation

Java

LayoutInflater layoutInflater = LayoutInflater.from(this);
View awesomeView = layoutInflater.inflate(R.layout.my_awesome_layout, null);

Kotlin

val awesomeView = inflateLayout(R.layout.my_awesome_layout)

Logs

Example: Logging

Java

String tag = this.getClass().getName();
Log.i(tag, "Howdy! Info");
Log.d(tag, "Knock knock! Debug");
Log.e(tag, "Grim, Error");
Log.wtf(tag, "Damn! WTF"); // Nope, not what you think. Refer the docs :P

Kotlin

i("Howdy! Info")
d("Knock knock! Debug")
e("Grim, Error")
wtf("Damn! WTF")

Preferences

Example: Single Preference

Java

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
Editor editor = preferences.edit();
editor.putBoolean("KEY_WALKTHROUGH_COMPLETE", complete);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
  editor.commit();
} else {
  editor.apply();
}

Kotlin

getDefaultSharedPreferences().putBoolean("KEY_WALKTHROUGH_COMPLETE", complete)

Example: Bulk Preferences

Kotlin

getDefaultSharedPreferences()
  .bulk()
  .putBoolean("KEY_WALKTHROUGH_COMPLETE", complete)
  .putString("KEY_LAST_USED", lastUsedIso8601Date)
  .applyBulk()

Toasts

Example: Short Message

Java

Toast.makeText(this, R.string.welcome, Toast.LENGTH_SHORT).show();

Kotlin

toast(R.string.welcome)

Example: Long Message

Java

Toast.makeText(this, R.string.welcome, Toast.LENGTH_LONG).show();

Kotlin

longToast(R.string.welcome)

System Services

Example: System service on code

Java

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

Kotlin

val alarmManager = context.alarmManager()
val notificationManager = context.notificationManager()

Example: System service field (lazy)

Java

AlarmManager alarmManager;
NotificationManager notificationManager;
public void onCreate(Bundle savedInstanceState)
{
        super.onCreate(savedInstanceState);
        alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}

Kotlin

val alarmManager: AlarmManager by systemService()
val notificationManager: NotificationManager by systemService()

Resources

Example: Typeface from Assets

Java

AssetManager assetManager = getAssets();
Typeface sourceSansPro = Typeface.createFromAsset(assetManager, "fonts/source-sans-pro.ttf");

Kotlin

val sourceSansPro = typefaceFromAssets("fonts/source-sans-pro.ttf")

Example: Get resources

Java

int number = getResources().getInteger(R.integer.number);
int dimenPixelSize = getResources().getDimensionPixelSize(R.dimen.size);
int color = getResources().getColor(R.color.red);
int[] intArray = getResources().getIntArray(R.array.ids);
String[] stringArray = getResources().getColor(R.array.options);
Drawable drawable = getResources().getDrawable(R.drawable.background);

Kotlin

val number = intRes(R.integer.number)
val dimenPixelSize = dimenRes(R.dimen.size)
val color = colorRes(R.color.red)
val intArray = intArrayRes(R.array.ids)
val stringArray = stringArrayRes(R.array.options)
val drawable = drawableRes(R.drawable.background)

Example: Get resources as field (lazy)

Kotlin

val text by stringResource(R.string.text)
val number by intResource(R.integer.number)
val dimenPixelSize by dimenResource(R.dimen.size)
val color by colorResource(R.color.red)
val intArray by intArrayResource(R.array.ids)
val stringArray by stringArrayResource(R.array.options)
val drawable by drawableResource(R.drawable.background)

Example: DP and PX converter

Kotlin

val px = 40.toPx(context)
val dp = 180.toDp(activity)

More...

More extensions under development.

License

Copyright 2015 Yoav sternberg

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment