kotlinAndroidLib


Source link: https://github.com/VolodymyrLykhonis/kotlinAndroidLib

Kotlin for Android

Helps not to write boilerplate code with instantiating abstract or interfaces instances. The purpose of the library is to save time of writing code using Android SDK wrapping as much as possible common functionality.

The library uses one of the coolest feature of the Kotlin language inline almost everywhere, that prevents overhead code.

Warning

This project is not relative to JetBrains company or their products. Use this project as is, no author nor anybody else is responsible for any damage.

Linking to Android project

  1. Create regular or use existing Android project in IDEA
  2. Select File from menu and click on Project Structure...
  3. Select Libraries section and add new Java library
  4. Choose the source src folder of kotlinAndroidLib and click Ok
  5. Choose category sources and click Ok
  6. Select and name the library kotlinAndroidLib
  7. Add new file to the library ^N and choose classes.jar of kotlinAndroidLib
  8. Add import to your kotlin files in the android project import com.vlad.android.kotlin.*

Usage

Small example of usage most of the functions and also some examples like how to create Parcelable. See Sample Activity

  • findViewById replacement for Activity and View:

      val myButton = findView<Button>(R.id.my_button) 
  • setOnClickListener for Views:

      myView?.setOnClickListener {
     /* code here */ 
    }
    
    myView?.setOnClickListener {
     view -> /* code here with view: View? argument */ 
    }
    
    val onClickListener = OnClickListener {
     view -> /* code here */ 
    }
    
     myView?.setOnTouchListener {
     view, event -> false /* with view: View? and event: MotionEvent? */ 
    }
    
    val onTouchListener = OnTouchListener {
     view, event -> false 
    }
     
  • runOnUiThread for Activities:

    It can be used without the library:

      runOnUiThread(runnable {
     /* action */
    }
    ) 

    Library just remove runnable call and can be used as:

      runOnUiThread {
     /* action */ 
    }
     
  • Short definition of BroadcastReceiver:

    Library just wraps onReceive call:

      val myBroadcastReceiver = BroadcastReceiver {
     context, intent ->
    
     /* handle intent here */
    
    }
     
  • setPositiveButton and setNegativeButton for AlertDialog.Builder:

      AlertDialog.Builder(this).setTitle("Hello")
    
     ?.setMessage("Want to say hello to world?")
    
     ?.setPositiveButton("Yes", {
     dialog, which -> /* hello */ 
    }
    )
    
     ?.setNegativeButton("No", {
     dialog, which -> /* no hello now */ 
    }
    )
    
     ?.create()
     val positiveButton = dialogOnClickListener {
     dialog, which -> 
    }
     
  • Short definition of OnEditorActionListener:

    Library just wraps onEditorAction call:

      val myEditorActionListener = OnEditorActionListener {
     v, actionId, event ->
    
     /* handle actionId and/or event */
    
     false
    
    }
    
     myEditText?.setOnEditorActionListener /* OnEditorActionListener */ {
     v, actionId, event ->
    
     false
    
    }
     
  • getSystemService with or without casting for known services:

    Library allows call with casting for specific name of the service or call directly for known services:

      val inputMethodManager = getSystemServiceAs<InputMethodManager>(Context.INPUT_METHOD_SERVICE)
    val inputMethodManager = getInputMethodService() 
  • AdapterView<out Adapter?>.setOnItemClickListener and AdapterView<out Adapter?>.setOnItemLongClickListener:

      listView.setOnItemClickListener {
     parent, view, position, id -> 
    }
    
    listView.setOnItemLongClickListener {
     parent, view, position, id -> false 
    }
     
  • Wrap calls without runnable() for Handler:

      val handler = Handler()
    handler.post {
     /* code here */ 
    }
    
    handler.postDelayed(100) {
     /* code here */ 
    }
    
    handler.postAtFrontOfQueue {
     /* code here */ 
    }
    
    handler.postAtTime(System.currentTimeMillis() + 100) {
     /* code here */ 
    }
     

    Also easy to create handler with callback to handle messages:

      val handler = Handler {
     message ->
    
     when (message.what) {
    
      1 -> handleMessage1(message.getData())
    
      else -> false
    
     
    }
    
    }
     
  • Wrap SQLiteDatabase functionality:

    • Wrap pattern: beginTransaction -> setTransactionSuccessful -> endTransaction with try and finally:

        sqliteDatabase.transaction {
      
       // call any method of SQLiteDatabase without . or ?.
      
       execSQL("ALTER TABLE table_1 RENAME TO table_2")
      
      }
       
    • Wrap pattern: cursor not null -> moveToFirst -> moveToNext in do-while loop -> close with try and finally:

        // query call with same arguments + last argument is a body in the loop
      val collection /* of String */ = sqliteDatabase.query<String>("people", array("first_name"), null, null, null, null, null, null) {
      
       /* Cursor namespace */
      
       getString(getColumnIndexOrThrow("first_name"))
      
      }
       

      Support also queryWithFactory, rawQuery and rawQueryWithFactory.

    • Wrap ContentValues for insert and update:

        sqliteDatabase?.insert("table_name", null) {
      
       // ContentValues namespace
      
       put("column_text", "Hello")
      
       put("column_int", 12.toInt())
      
       put("column_float", 13.toFloat())
      
      }
      
       sqliteDatabase?.update("table_name", "ID = ?", array("3")) {
      
       // ContentValues namespace
      
       put("column_text", "Hello World")
      
      }
       
  • Wrap Cursor pattern moveToFirst -> moveToNext in do-while loop:

    Possible to call all functions on Cursor?, if Cursor? == null, returns empty collection

      try {
    
     val collection /* Of String? */ = cursor.map {
    
      getString(getColumnIndexOrThrow("first_name"))
    
     
    }
    
    }
     finally {
    
     cursor?.close()
    
    }
     

    or

      val collection = LinkedList<String?>()
    try {
    
     cursor.mapTo(collection) {
    
      getString(getColumnIndexOrThrow("first_name"))
    
     
    }
    
    }
     finally {
    
     cursor?.close()
    
    }
     

    or

      val collection /* Of String? */ = cursor.mapAndClose {
    
     getString(getColumnIndexOrThrow("first_name"))
    
    }
     
  • ExecutorService:

      executorService.execute {
     /* code here */ 
    }
    
    val future = executorService.submit<Any?> {
     /* code here and no result */ 
    }
    
    val future = executorService.submit<String?> {
     /* code here */ "Result" 
    }
    
    val future = executorService.submit<String?>("Result") {
     /* code here and result */ 
    }
     
  • Wrap Intent, now easy to create new Intent without defining new variable:

      sendBroadcast(Intent() {
     setAction(Intent.ACTION_DEFAULT) 
    }
    )
    startActivity(Intent(Intent.ACTION_VIEW) {
     setDataAndType(Uri.parse("http://example.com/audio.mp3"), "audio/mpeg") 
    }
    ) 

    or make it even nicer if you have an action and it's a String value:

      "my.application.intent.action.ACTION_MINE" toIntent {
    
     /* namespace of Intent */
    
    }
     

    or just convert String to Intent:

      "my.application.intent.action.ACTION_MINE".toIntent() 
  • Wrap IntentFilter, now easy to create new IntentFilter without defining new variable:

      registerReceiver(broadcastReceiver, IntentFilter {
    
     addAction(ACTION_HELLO)
    
    }
    ) 
  • Wrap Bundle, now easy to create new Bundle without defining new variable:

      Bundle {
     putString("result", "Some result!") 
    }
     
  • Short definition of ResultReceiver:

    Library just wraps onReceiveResult call:

      val myResultReceiver = ResultReceiver(Hanlder()) {
     code, data ->
    
     /* handle result here */
    
    }
     

    or automatically create new Hanlder

      val myResultReceiver = ResultReceiver {
     code, data ->
    
     /* handle result here */
    
    }
     
  • Wrap 'CREATOR' for Parcelable. Example of usage:

    WARNING: It might wrong or not supported yet!

      class MyParcelable(val number: Int, val text: String?) : Parcelable {
    
      public override fun writeToParcel(p0: Parcel?, p1: Int) = if (p0 != null) {
    
      p0.writeInt(number)
    
      p0.writeString(text)
    
     
    }
    
      public override fun describeContents(): Int = 0
    
      class object {
    
    val CREATOR = CreateParcelable<MyParcelable> {
     MyParcelable(it.readInt(), it.readString()) 
    }
    
     
    }
    
    }
     
  • Wrap Thread, run and call start method:

      async {
    
     // some code here
    
    }
     

Resources

Proxy Cache Manager annotation that helps you to cache your repository method calls.

Features:

  • Fast implementation for each repository method.
  • Provides simple methods like isCached or isExpired.
  • Distinguish between different calls of the same method.
  • Provides the ability to store content on disk without the need of a database.

Android custom view to load an avatar or profile image with a progress indicator.

Android Material Avatar samples how to enhance a common profile avatar selection used in most of popular projects now a days.

Reactive extensions for SimpleNoSQL. Manipulate the entities using Observable and Completable.

A pretty triangle label view.

Material Bottom Navigation.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes