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.



Thursday 31 August 2017

Converting android View to Bitmap

Android View to Bitmap

Today we will learn how to convert the android view or XML to a bitmap. During android development sometimes we need to convert the view or xml to bitmap for different purposes. One use case could be to share the the bitmap using sharing intent provided by google. You can find the tutorial here how can you share the bitmap without saving it to external memory. Now let's get started with the tutorial to convert the xml to bitmap.

Implementation

Actually it is very easy to convert the xml to a bitmap. You can use the drawing class provided by the google android. 
The hack here is that as our view/xml is not visible, so it is most likely not inflated. What we have done is called the layout() and measure() methods of the view ourself which are actually called by the system when a view is set on the activity to set the width and height of the layout.

Calling This Method

We can call this method by passing our view in this method. But first we need to inflate that xml.
What we have done here is that inflating our xml are then passing it our method to create the bitmap from here. Pretty simple, right! That's it. You have successfully created the bitmap from xml. Happy Coding!! In case of query please comment below or contact me at farooqahmadkhan003@gmail.com.




Tuesday 29 August 2017

Android App Widgets tutorial with Example

Android App Widgets

Android widgets are views that can be placed on the home screen and are updated periodically. They could be a great options for frequent user interactions with your application without opening the application. They are basically the broad cast receivers which display some data and interacts with the users.
android app widget - android tech point
Resizable Android App Widget - Android Tech Point

Steps to create the widget

Creating android widgets is a simple task. You only need to follow simple steps:

  • Create the xml layout file.
  • Define the XML file (i.e AppWidgetProviderInfo) which includes properties for the widget like size, updating interval, minimum size, resizability etc.
  • Create the java file which will have the callback methods for the app widget.
  • Define the receiver for the widget in the AndroidManifest.xml.
  • (Optional) Configuration activity which is called when a new instance of the app widget is made.

Available view in Android App Widgets

Widgets has some limitations. You can only use few views in the xml of widgets. They are as follows:
  1. AnalogClock
  2. Button
  3. Chronometer
  4. ImageButton
  5. ImageView
  6. ProgressBar
  7. TextView
  8. ViewFlipper
  9. ListView
  10. GridView
  11. StackView
  12. AdapterViewFlipper
And the layout which are supported by the remote views are:
  1. FrameLayout
  2. LinearLayout
  3. RelativeLayout
  4. GridLayout
The subclasses of these views are not supported.

Creating Android App Widgets

Now let's dive into the code to create the widgets. You can get the complete project at Github Repository. Or you can get the code below in this post. We will follow the steps defined in the above portion.

  1. Create the xml layout file.
    Here we will define the layout for our widget just like the regular android xml layout.
  2. Define the XML file (i.e AppWidgetProviderInfo) which includes properties for the widget like size, updating interval, minimum size, resizability etc.
  3. Create the java file which will have the callback methods for the app widget. In some cases onAppWidgetOptionsChanged()  is not called on the creation of app widget as it should according to offical docs, so you need to set the UI of widget in onUpdate()  to be on safer side.
  4. Define the receiver for the widget in the AndroidManifest.xml. You need to define the receiver inside the activity tag. Also give the meta tag.

Important

That's all. Enjoy the resizable widget for your application. In case of any queries I am always here, comment below or contact me at farooqahmadkhan003@gmail.com.


Sunday 27 August 2017

Android O (Oreo) - What's New

Android O (Oreo) - What's New

Android has officially released the new operating system for android - android version 8.0 named Android Oreo.  And has claimed it to be faster, smarter and sweeter than ever. Now let's go some deep to see what's new in Android O. Here is the official release video for Android Oreo.

Speed

According to the official statement, android O is 2X faster as compare to other versions. They've claimed that the boot speed of the device will be two times faster. To make it more faster and effective, they have now minimized the background usage of the apps which you don't use. This could be a great optimization. It will also save a lot of battery, network usage and other useful resources of the mobile devices. 

Auto Fill

Forms auto fill is introduced in Android Oreo. According to me it is going to be the one of best upgrades. Now you don't have to fill in the form again and again. The OS will do it for you. All you have to do is give it permissions and it will remember you login credentials. Easy Life. :-p

Picture in Picture Mode

They have also made improvements in already available picture in picture mode released in Android 7.0 or Android N. Now you can easily use and see two apps simultaneously. That's cool! Everyone knows now the humans are also multitasker. Here is a preview from of picture in picture mode.
android tech point picture in pictre mode
Android picture in picture mode from official reference. 


Android Instant Apps

A new feature introduced in android O is android instant apps. No downloading, no installation, no memory usage. Now you can use the android applications right from your browsers. They have also provided the notification dots for easy access of the application from the notification tray and land to any page in the application.
android tech point notification dots
Android Notification Dots from official reference


Security Updates

In Android Oreo, a lot of security updates have also been added. According to official release docs
Google Play Protect statement is
Working to keep your device and data safe from misbehaving apps by scanning over 50 billion apps per day, even the ones you haven't installed yet!

Final Words

You can  also limit the background location updates for the applications which will save a lot of battery. For developers: Android has introduced auto resize text input TextView fields which will resize their font size according to device size and available space. Tool tips support is also introduced for menus and icons. Notifications are also improved very much in this update. Notification channels are introduced with which user can snooze or disable notifications of specific type for and application. For high performance audio, native support for C/C++ is also introduced in this release.
After so many optimizations and new features in Android Oreo, now android claims to be a battery saver and is making you battery strong and full of life. Upgrade now to Android O now and enjoy the amazing updates. Cheers!






Sunday 13 August 2017

Android Activity Life Cycle Tutorial with example


Android Activity Life Cycle

Importance of Activity's Life cycle

Understanding Android Activity Life cycle is one of the most important things for android developers. Using the activity life cycle correctly can avoid your application from:
  • Losing the user data when the screen orientation changes
  • Crashing of application while switching between applications
  • Consuming valuable resources of the phone while the user is not using you app
  • Losing the users data when user leaves you app and return later
Here an important point is, the activity is recreated with the phone's orientation is changes. So to give your users best experience, you need to take care of this too.

Activity Life cycle Callbacks

To give the users of your application best user experience, it is very important to understand the activity life cycle. To manage you activities, android provides various callbacks on different stages of activity life cycle. They are listed below:
  1. onCreate()
  2. onStart()
  3. onResume()
  4. onPause()
  5. onStop()
  6. onRestart()
  7. onDestroy()
They all are called on different stages of activity life. To understand which callback is called when, see the table and image from the official documentation of android below carefully. And also see the demo project running given at the end of the tutorial.
Android Activity Life Cycle


MethodDescription
onCreatecalled when activity is first created. You must implement this callback.
onStartWhen the activity enters the Started state, the system invokes this callback. The system prepares to enter the activity to foreground.
onResumecalled when activity will be in visible form to the user. This is the state in which the app interacts with the user.
onPausecalled when activity is not in focus. It is the first indication that the user is leaving the activity. It does not mean that activity is going to destroy. When a new dialog(popup) or multi window in android 7.0 and higher is made, it is also called as the app loses the focus.  
onStopcalled when activity is no longer visible to the user. It usually happens when a new activity is launched.
onRestartcalled after your activity is stopped, prior to start.
onDestroycalled before the activity is destroyed. After that your activity will be removed from the memory.

Save the State of your App

To save the state of your application, android provides two callbacks onSaveInstanceState(Bundle savedInstanceState) and  onRestoreInstanceState(Bundle savedInstanceState). From these two callbacks, you can save and restore the data and make the user experience better. These callbacks are called when the orientation of the app is changed or app is brought to foreground again. onSaveInstanceState(Bundle savedInstanceState) is called after onPause() and similarly onRestoreInstanceState(Bundle savedInstanceState) is called onStart() method.

Demo Project

To understand the activity life cycle more clearly, run the demo project at  Github Repository. Here you will see all the callbacks in action and also learn how you can save the user data and make the better user experience. If you want to know anything about it or having some confusing, feel free to comment or contact me at farooqahmadkhan003@gmail.com