Thursday 21 September 2017

Android Custom Toast Tutorial with Example using Kotlin

Android Custom Toast

Today we are going to learn how can we make our own custom Toast. Nowadays a lot of apps are using custom toast to show messages to clients. Sometimes it is requirement of design to make a custom toast. Don't worry, it is very simple and easy to create custom toast in android. But the special thing is today we will make the custom toast using kotlin language. Here is the final look of our Toast.
android-tech-point-android-custom-toast
Android Custom Toast with Kotlin - Android Tech Point
Let's start now.

Steps

As I have mentioned earlier that it is very easy to make a toast using the Toast as super class. As we are using kotlin, we can do this by using extended functions of kotlin.
  • Create custom_toast_layout.xml and define the layout for our custom toast.
  • Create a kotlin file and name it Util.kt.
  • Write an extended function createToast() to show the toast. You can pass different properties to change the behavior and layout of the toast.
  • Use your own custom toast anywhere in the project.

Implementation

Here are the simple steps in detail to create the custom toast. 
  • Creat the custom_toast_layout.xml.
  • Second step is to write the extended function for the Toast class.
  • Final step is to use this function. You can access this function from anywhere in your project. Here is how you can use this function.

Final Words

We can customize this function in any way we want according to our requirement. A=That's all. We are done with the custom toast. Happy Coding!! :) You can find the code for the complete project at GitHub. Feel free to discuss anything in the comments or contact me at farooqahamdkhan003@gmail.com.


Tuesday 12 September 2017

Android Recycler View Example with Kotlin

Now as google has officially announced kotlin as official language for Android Development, today I am writing a tutorial about creating Android Recycler View with kotlin. Though Android Studio 3.0 is not released yet, but you can get the beta release for android development in kotlin. Visit the link to download android studio 3.0 beta release (get the stable build if released).
Here is the final image of our recyclerview.
Recycler View with kotlin-Android Tech Point
Recycler View Using Kotlin - Android Tech Point

Steps to Create Recycler View

You can create your recycler view by following these simple steps
  • Add the recycler view in your xml.
  • Add a new layout xml file for the list item and add card view in it.
  • Create your data class.
  • Write your adapter class.
  • Set the adapter for the recycler view.
And that's all. Now we will see the implementation in a bit details with the actual code.

Implementation for Recycler View with Kotlin

  • Drag  and drop the recycler view, android studio will automatically add the dependency for recycler view in the Build,gradle(app).
    Android Tech Point - Recycler View
    Recycler View - Android Tech Point

    Here is the code for the recycler view in activity_main.xml
  • Create a new xml layout file and name it list_item.xml. Drag and drop the card view in the design section (Android Studio will automatically add the dependency for card view) create the design for a single row.
  • Now we will add the data class or you can say POJO class in kotlin. It is very very simple in kotlin. You don't have to write any boilerplate code.
  • Next step is to create our RecyclerViewAdapter.kt. It is pretty much same as we usually do in java.
  • Its time to use our custom adapter and display the list of items on the screen. In your main activity, set the adapter for the recycler view.

Final Words

You can get the complete tutorial at GitHub. Happy Coding!! :) For suggestions and help feel free to comment below or contact me at farooqahmadkhan003@gmail.com.


Sunday 10 September 2017

Android Sharing Image Using Share Intent

Today I am going to write about how you can share images or files or other content without saving them to external memory. YES, WITHOUT SAVING THEM TO EXTERNAL MEMORY. This will save you from a lot of unnecessary code and permission in you application.  The hack here is we will save them to cache and then share it. If you want to share you views/xml see the article here which explains how you can convert your view to bitmaps.

Why Avoid Using External Storage

Alot of users not do not grant permission for reading and writing of external storage due to some security concerns. And moreover why use external storage when you can do a task without using external storage. It is also valid point that caches are much much faster then the external storage. It will speed up the sharing process of your application.

Steps for sharing Without Saving to External Memory

We can share our content by following the simple three steps.
  • Add the provider tag in your AndroidManifest.xml file.
  • Defile the filepaths.xml file in xml directory.
  • Save the image in the cache and then share it.

Method to Share Images

Add this code in your application tag in your Androidmanifest.xml file. Here com.vd.viewtobitmap is my package name. Replace it with your package name and append it with .fileprovider.
Now you need to do one more thing, that is defining the paths for images to save them. You can do it by creating a filepaths,xml file in xml directory within your res folder.
Here is the method you can use to share your image files without saving them to external storage. You only need a little changes to make it work for the file extension you want. Learn how can you create the bitmap to pass it in this function.
What we have done here through a little hack is saving the bitmap which is passed in the function to the cache. And then reading the file URI for sharing and granting temporary permission for accessing this file for sharing. Pretty simple, right! That's it. You have successfully shared the file/image without saving it to external memory. Happy Coding!! In case of query please comment below or contact me at farooqahmadkhan003@gmail.com.