Showing posts with label example. Show all posts
Showing posts with label example. Show all posts

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.


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