tingnapianhai / KAndroid

Kotlin library for Android

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Download Android Arsenal

KAndroid

Kotlin library for Android providing useful extensions to eliminate boilerplate code in Android SDK and focus on productivity. Library is currently compatible with latest Kotlin M12 build.

Download

Download latest version with Gradle:

repositories {
    jcenter()
}

dependencies {
    compile 'com.pawegio.kandroid:kandroid:0.1.13@aar'
}

Usage

Binding views

// instead of findViewById(R.id.textView) as TextView
val textView = find<TextView>(R.id.textView)

SearchView extensions

/* instead of:
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
    override fun onQueryTextChange(q: String): Boolean {
        update(q)
        return false
    }
    
    override fun onQueryTextSubmit(q: String): Boolean {
        return false
    }
}) */
searchView.onQueryChange { query -> update(query) }

/* instead of:
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
    override fun onQueryTextChange(q: String): Boolean {
        return false
    }
    
    override fun onQueryTextSubmit(q: String): Boolean {
        update(q)
        return false
    }
}) */
searchView.onQuerySubmit { query -> update(query) }

Accessing Activity methods from Fragment

// instead of (getActivity() as SampleActivity).foo()
getActivity<SampleActivity>().foo()

Using system services

// instead of getSystemService(Context.WINDOW_SERVICE) as WindowManager
getWindowManager()
// instead of getSystemService(Context.POWER_SERVICE) as PowerManager
getPowerManager()
// instead of getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
getNotificationManager()
// instead of getSystemService(Context.USER_SERVICE) as UserManager
getUserManager()
// etc.

Toast messages

longToast("I'm long toast message!")
toast("Hi, I'm short one!")

Layout inflater

// instead of LayoutInflater.from(context).inflate(R.layout.some_layout, null, false)
context.inflateLayout(R.layout.some_layout)
// or
context.inflateLayout(R.layout.some_layout, attachToRoot = true)

Using Intents

// instead of Intent(this, javaClass<SampleActivity>())
val intent = IntentFor<SampleActivity>(this)

Logging

// using javaClass.getSimpleName() as a TAG
v("Verbose log message")
d("Debug log message")
i("Info log message")
w("Warn log message")
e("Error log message")
wtf("WTF log message")
// or with custom TAG
v("CustomTag", "Verbose log message with custom tag") 

Threading

// instead of Thread(Runnable { /* long execution */ }).start()
runAsync {
    // long execution
}

// delayed run (e.g. after 1000 millis)
// equals Handler().postDelayed(Runnable { /* delayed execution */ }, delayMillis)
runDelayed(1000) {
    // delayed execution
}

// run on Main Thread outside Activity
// equals Handler(Looper.getMainLooper()).post(Runnable { /* UI update */ })
runOnUiThread {
    // UI update
}

// delayed run on Main Thread
// equals Handler(Looper.getMainLooper()).postDelayed(Runnable { /* delayed UI update */ }, delayMillis)
runDelayedOnUiThread(5000) {
    // delayed UI update
}

More

Under development so expect soon. License

Copyright 2015 Paweł Gajda

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.

About

Kotlin library for Android

License:Apache License 2.0


Languages

Language:Kotlin 97.4%Language:Java 2.6%