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!!