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


Tuesday, 8 August 2017

Android Custom Views with Examples

Android Custom Views

Some times we need very personalized views or you can say very personalized behavior of the android views. For this purpose android provides the support for the custom views. We can create our own views and define there behavior as our needs.
If we just need to make the little adjustments to the existing views like Button, EditText, ListView, Layouts or any other view, we can simply do that by extending the predefined view to our own classes and change there characteristics.

Use Case

Some of the example use cases for custom views are as follows:
  • A control with some predefined images.
  • Group of perdefined views to use throughout our app.
  • Change look and feel of predefined views.
  • Custom behavior on onClick(), onFocusChanged(), onLongPressed() etc. events.

@Override

You can override a list of method of the views after extending them to your own class you change there behavior. You can override
  • onDraw()
  • onMeasure()
  • onKeyDown()
  • setMeasuredDimension(int width, int height)
and many more methods. Now your methods will be called instead of the parent class on the specified events. All you have to do is use your own class name in the XML to use them and provide there attributes.

Example

For an example, we will create our own image view which shows the square images rather than the original dimension of the image. For this purpose, all we have to do is just extend the imageView class to our own class and define the custom behavior. following is the code for the class.

SquareImage.java
Now to use this class, all you have to do is
Similarly, if you want to change the font of all the EditTexts of your app, you just need to extend the already existing Edittext with you class and change the font style. You can find the class as follow:

CustomEditText.java

You can a sample application with two different custom components at Github Repository. All you need to do is create res/values/attr.xml. Then create the layout xml file with the the view in res/layout. And finally write the java class to inflate the view and set the values for the views. You are done! Just enjoy using the component.
If you still get the error feel free to comment below or contact me at farooqahmadkhan003@gmail.com. Happy Coding!!

Saturday, 11 February 2017

Sending POST Request to Internet Using Android Volley

android volley library - android tech point
Android Volley Library Tutorial

Android Volley is an library for network operations in android development. It is a very light weight framework and most importantly, very fast for network operations. It has facility to get the raw string, image or JSON data over the network efficently. One of the benifit that I like the most if you can easily use volley in your activity as it automatically sends the request in separate thread. And also maintains a queue for the requests.
Note that volley is not for large data downloads as it holds all the data in memory. For large downloading use the DownloadManager.
Here is the demo video for this app.

Dependency for android volley

Minimum requirement for android volley Android 2.2 (API Level 8) or higher. Now come towards tutorial for android volley.

Steps for using Volley

  • Add the uses-permission in Android Manifest.xml file.
    <uses-permission android:name="android.permission.INTERNET"/>
  • Add the dependency in build.gradle (Module: app)
    compile 'com.android.volley:volley:1.0.0'
  • Write code for sending request in MainActivity.java
  • Write the server side code. I am using xampp and sending request to localhost. Follow tutorial here to configure xampp for accessing from other devices.
You can get the complete project at Github Repository. Or you can get the code below in this post.

MainAcitivty.java

activity_main.xml

androidvolley.php

In case of error

If you get some error, the first thing you should check is permissions in android androidManifest.xml and build.gradle. If you still get the error feel free to comment below or contact me at farooqahmadkhan003@gmail.com.

Monday, 30 January 2017

Android App for Login Sign Up Using Android Volley and php

In this tutorial we are going code back-end for the Android App we build in other tutorial. For this we will use Android Volley to send request on server. Fortunately, Android studio does most of this part too. We only need to send the request for the login authentication or sign up from our android app.

Which is so simple using android volley and with piece of code, but I made some other changes too in the automatically generated code for our app. Read the comments in the code to understand the code completely. The main changes are:
  • Removed the Async Class as the android volley automatically send the network requests in threads. Android Studio add the async class for the network operations as no network operation can be performed in the main thread.
  • Remove all the relevant code to the async class.
  • Paste the url of your server in string.xml and also change the file name accordingly in the java files. You can make your laptop a server using xampp. Follow the tutorial here for further instructions if you get troubled.
I think that is pretty much all. The rest of code is same. Now move on the tutorial part. The step by step break down for the android app is as follows:
  • Add the uses-permission in Android Manifest file.
    <uses-permission android:name="android.permission.INTERNET"/>
  • Add the dependency for Android Volley in build.gradle (Module: app).
    compile 'com.android.volley:volley:1.0.0'
  • Write the code to send request to server for login or sign up.
  • Write the server side code. For this tutorial I am just sending success from the server for both login.php and signup.php.
    <?php echo "success"; ?>
And that all. You are done with the Android App for Login and Sign Up. You can get the complete android app code with back-end at Github Repository. Or you can get the code below in this post for login and sign up classes.

LoginActivity.java


SignUp.java


In case of error

If you get some error, the first thing you should check is permissions in android androidManifest.xml and build.gradle. If you still get the error feel free to comment below or contact me at farooqahmadkhan003@gmail.com.

Saturday, 28 January 2017

Android App for Login Sign Up Form Screen Designing

Every android application has login and registration procedure for authenticating or you can say tracking its users. Today's android tutorial is about designing the login and registration page for android application. Note that this tutorial is just about making the front end designs of the screen. We will cover the back-end coding, database management and sending requests to server in next tutorial. We will be building complete android app with fully functional login and signup with real time database in a step by step. Here is the demo Android App video.



This task is pretty easy now. You already have a template in Android Studio for login activity. And you can make changes according to your need like adding you logo, adding background image, and changing design, which we will learn in this tutorial. Now we should start with tutorial for our android app. Here is the final look of our Login/Signup screens.

sign up activity for android app
Sign Up Activity
login activity for android app
Login Activity

Lets start coding our android app. Step by step instructions are:

  • Create an android studio project.
  • Select the Login Activity template for launching activity of android app.
  • Create new activity named SignUp with Login Activity template.
  • Make necessary changes in the design according to your need.
You can get the complete android app code with back-end at Github Repository. Or you can get the code below in this post for login and sign up screens.

activity_sign_up.xml

activity_login.xml


Next Step

Now the next step is to make these screens functional. And fortunately, Android Studio does most of that part too. We just need to send the request for sign up and login to server and make authentication. For that part follow the next part of tutorial.

Monday, 16 January 2017

Android Expandable ListView / ExpandableListView

ExpandableListView is kind of advanced version of Listview. You can expand the list items. It is very useful when you have categories and sub categories of data. Here a tutorial using for ExpandableListView with event listener of parent and child list items.


All you need to do is to follow the simple steps
  • Create main_activity.xml.
  • Create list_group.xml
  • Create list_item.xml.
  • Create the ExpandableListAdapter.java.
  • Create MainActiviy.java.
In ExpandleListAdapter.java, you need to add the @Override some methods of BaseExpandableListAdapter class. important methods are getGroupView and getChildView. In these methods, you need to hook the (in our example) TextView and ImageView of the main list and sublist respectively.

The file main_activity.xml contain the ExpandableListView tag. list_item.xml and list_gruop.xml has the TextViews for list and sublist.

You can get the complete project at Github Repository. Or you can get the code below in this post.

Here is you main_activiy.xml
list_group.xml
list_item.xml
ExpandableListAdapter.java
MainActivity.java

Sunday, 15 January 2017

Android tutorial to get last known location using Google Fused Api

androdi-tech-point
This tutorial is about getting last known location using Google Fused Api is similar to getting the current geolocation of the device. But the advantage of using the last known location the if some data about location is already available, it does not request for the current geolocation. Which save the battery consumption and data usage of the device which is always a big problem for the developers.

Here is the demo video of the running app.

In this tutorial, we will do the following tasks:
  • Get the last known location using google fused api.
  • If no data is available, we'll request for the current geolocation updates.
  • Add check for enabling location services of the device.

But first things first. Set up environment for using google play services. For this you need to download the 'Google Play Services' form SDK Manager.

After that add the dependency in the build.gradle(App Module) file:
compile 'com.google.android.gms:play-services:10.0.1'

set up complete for using the getting current location using google fused api.
Add permissions in AndoidManifest,xml
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />  
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

You can get the complete project at Github Repository. Or you can get the code below in this post.
All done, just add the following code in your MainActivity.java

And the following code in your main_activity.xml