A collection of Kotlin extensions for Android, based on KotlinAndroidLib and Android Kotlin Extensions.
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.0.2'
}
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)
startActivity<SuperPowersActivity>(flags)
Java
Intent intent = new Intent(this, SuperPowersActivity.class);
Kotlin
val intent = intent<SuperPowersActivity>()
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)) {}
Kotlin
async {
}
mainThread {
}
Java
new BroadcastReceiver() {
@Override
public void onReceive(Context context,Intent intent) {
}
};
Kotlin
broadcastReceiver { context,intent -> }
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)
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")
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)
Kotlin
getDefaultSharedPreferences()
.bulk()
.putBoolean("KEY_WALKTHROUGH_COMPLETE", complete)
.putString("KEY_LAST_USED", lastUsedIso8601Date)
.applyBulk()
Java
Toast.makeText(this, R.string.welcome, Toast.LENGTH_SHORT).show();
Kotlin
toast(R.string.welcome)
Java
Toast.makeText(this, R.string.welcome, Toast.LENGTH_LONG).show();
Kotlin
longToast(R.string.welcome)
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()
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()
Java
AssetManager assetManager = getAssets();
Typeface sourceSansPro = Typeface.createFromAsset(assetManager, "fonts/source-sans-pro.ttf");
Kotlin
val sourceSansPro = typefaceFromAssets("fonts/source-sans-pro.ttf")
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)
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)
Kotlin
val px = 40.toPx(context)
val dp = 180.toDp(activity)
More extensions under development.
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.