Monday, January 5, 2015

Android custom camera

Android custom camera library.

 Every manufacture of Android devices have a different approach to the camera and the worst part is: there is no guarantee that if you send your custom settings to the camera - the software will take them in consideration.
For example:
  1. Samsung device could care less about the resolution that we wanted - it always took the photos at the higher resolution.
  2. HTC devices would crop the photos. So if your device says that you have a a camera with a 5Mpx sensor, in reality the camera will produce a 4,4Mpx photo.
This is why i created the custom camera app. While developing i had to reinvent the wheal many times for different stuff since Google dose not provide basic functionality that Apple dose in its iOS. A good example is the accelerometer. In both system you can get the raw data, but Apple gives you something more. You can get the orientation of the device if this is all what you need.

Tuesday, November 25, 2014

Android Fragments

A Fragment is a piece of an application's user interface or behavior that can be placed in an Activity which enable more modular activity design. It will not be wrong if we say, a fragment is a kind of sub-acitivity. Following are important points about fragment:
  • A fragment has its own layout and its own behavior with its own lifecycle callbacks.
  • You can add or remove fragments in an activity while the activity is running.
  • You can combine multiple fragments in a single activity to build a multi-pane UI.
  • A fragment can be used in multiple activities.
  • Fragment life cycle is closely related to the lifecycle of its host activity which means when the activity is paused, all the fragments available in the acivity will also be stopped.
  • A fragment can implement a behavior that has no user interface component.

Fragment Life Cycle

Android fragments have their own life cycle very similar to an android activity. This section briefs different stages of its life cycle.
Phase I: When a fragment gets created, it goes through the following states:
  • onAttach()
  • onCreate()
  • onCreateView()
  • onActivityCreated()

Phase II: When the fragment becomes visible, it goes through these states:
  • onStart()
  • onResume()
Phase III: When the fragment goes into the background mode, it goes through these states:
  • onPaused()
  • onStop()
Phase IV: When the fragment is destroyed, it goes through the following states:
  • onPaused()
  • onStop()
  • onDestroyView()
  • onDestroy()
  • onDetach()

Tuesday, July 1, 2014



Remember that the GUI layer doesn't request data directly from the web; data is always loaded from a local database.
The service layer periodically updates the local database.

What is the risk in blocking the Main thread when performing a lengthy operation such as web access or heavy computation? Application_Not_Responding exception will be thrown which will crash and restart the application.

Why is List View not recommended to have active components? Clicking on the active text box will pop up the software keyboard but this will resize the list, removing focus from the clicked element.

For senior employees

Beyond a certain level of experience, the job interview questions cease to be "difference between abstract class and interface", and focus more on testing your technical acumen, collaboration and communication skills. A list of such questions, typically asked during interviews for senior positions is given below:

Explain the life cycle of an application development process you worked on previously.

What the interviewer looks for is communication of requirements, planning, modeling, construction and deployment on the back end.

Here's a hypothetical project. Explain how you would go about it.

They want to know how you would break your work down into tasks and how many weeks for each task. I'm really looking to find out about planning methods, their skill set and how quickly they can execute.

How do you respond to requirement changes in the middle of a cycle?

What type of methodology have you used in the past? What are its drawbacks?

What are different techniques for prototyping an application?

Similar question: Do you feel there is value in wireframing an application? Why?

How do you manage conflicts in Web applications when there are different people managing data?

Tell me something you learned from a team member in the last year.

What software testing procedures have you used to perform a QA?

Once the coding skills verified. Sample I

· The Activity life cycle is must. Ask about the different phases of Activity Life cycle. For example: when and how the activity comes to foreground?

· Check the knowledge on AndroidManifest file, For example: Why do we need this file, What is the role of this file in Android app development.

· Different Kinds of Intents

· Ask about different Kinds of context

· Ask about different Storage Methods in android

· Kinds of Log debugger and Debugger Configuration

· How to debug the application on real device.

· How do you ensure that the app design will be consistent across the different screen resolutions

· Thread concepts also plus points as we deal with the treads more.

· Can you able to build custom views and how?

· How to create flexible layouts, For example to place English, Chinese fonts.

· What is localization and how to achieve?

· What are 9-patch images

· How to avoid ANR status

· How to do Memory management

· Ask about IPC

· What is onCreate(Bundle savedInstanceState), Have you used savedInstanceState when and why?

· To check how updated the person is just ask about what are Fragments in an Activity

If this is an Android specific job, just ask the obvious stuff. Sample II

Application lifecycle

When to use a service

How to use a broadcast receiver and register it both in the manifest and in code

Intent filters

Stuff about what manifest attributes and tags mean

The types of flags to run an application
How to do data intensive calculations using threads

Passing large objects (that can't be passed via intents and shouldn't be serialized) via a service
Binding to a service and the service lifecycle

How to persist data (both savedInstanceState and more permanent ways)

Just go through and see what sounds like it's important. Hopefully you're an android developer and know what all those things are, otherwise you're just waiting your interviewee's time =P

Sunday, November 17, 2013

Android Enable GPS (Check and prompt user to enable GPS)

GPS Location

To check if the GPS have been enabled or not, the following code can be used:

String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

If it's empty, means the GPS have not been enabled. You can start activity with intentSettings.ACTION_SECURITY_SETTINGS, to switch to GPS setting page.


import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.Toast;

public class AndroidEnableGPS extends Activity {
   /** Called when the activity is first created. */
   public void onCreate(Bundle savedInstanceState) {
   private void CheckEnableGPS(){
    String provider = Settings.Secure.getString(getContentResolver(),
           //GPS Enabled
        Toast.makeText(AndroidEnableGPS.this, "GPS Enabled: " + provider,
        Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);


Android Detect Battery Info.

The app simple detect battery level, voltage, temperature, technology, charging status, and health of the battery.


 public class MainActivity extends Activity {

    private TextView batteryLevel, batteryVoltage, batteryTemperature,
            batteryTechnology, batteryStatus, batteryHealth;

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {

        batteryLevel = (TextView) findViewById(;
        batteryVoltage = (TextView) findViewById(;
        batteryTemperature = (TextView) findViewById(;
        batteryTechnology = (TextView) findViewById(;
        batteryStatus = (TextView) findViewById(;
        batteryHealth = (TextView) findViewById(;

        this.registerReceiver(this.myBatteryReceiver, new IntentFilter(

    private BroadcastReceiver myBatteryReceiver = new BroadcastReceiver() {

        public void onReceive(Context arg0, Intent arg1) {
            // TODO Auto-generated method stub

            if (arg1.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {
                batteryLevel.setText("Level: "
                        + String.valueOf(arg1.getIntExtra("level", 0)) + "%");
                        .setText("Voltage: "
                                + String.valueOf((float) arg1.getIntExtra(
                                        "voltage", 0) / 1000) + "V");
                batteryTemperature.setText("Temperature: "
                        + String.valueOf((float) arg1.getIntExtra(
                                "temperature", 0) / 10) + "c");
                batteryTechnology.setText("Technology: "
                        + arg1.getStringExtra("technology"));

                int status = arg1.getIntExtra("status",
                String strStatus;
                if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
                    strStatus = "Charging";
                } else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING) {
                    strStatus = "Dis-charging";
                } else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING) {
                    strStatus = "Not charging";
                } else if (status == BatteryManager.BATTERY_STATUS_FULL) {
                    strStatus = "Full";
                } else {
                    strStatus = "Unknown";
                batteryStatus.setText("Status: " + strStatus);

                int health = arg1.getIntExtra("health",
                String strHealth;
                if (health == BatteryManager.BATTERY_HEALTH_GOOD) {
                    strHealth = "Good";
                } else if (health == BatteryManager.BATTERY_HEALTH_OVERHEAT) {
                    strHealth = "Over Heat";
                } else if (health == BatteryManager.BATTERY_HEALTH_DEAD) {
                    strHealth = "Dead";
                } else if (health == BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE) {
                    strHealth = "Over Voltage";
                } else if (health == BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE) {
                    strHealth = "Unspecified Failure";
                } else {
                    strHealth = "Unknown";
                batteryHealth.setText("Health: " + strHealth);



XML activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:text="Battery Info."
    android:text="Battery Level:"
    android:text="Battery Voltage:"
    android:text="Battery Temperature:"

Android Fundamentals

From a developer's perspective, the fundamental building blocks / components of Android are:
1. Activities
2. Services
3. Broadcast Receivers
4. Content Providers.

The means of communication between the above mentioned components is through
1. Intents
2. Intent Filters

The User Interface elements are by using what are called:
2. Notifications

Now, having broadly classified the basics, I would like to give a simple definition for each of them, before we can appreciate the need for each of them.

Activity is the basic building block of every visible android application. It provides the means to render a UI. Every screen in an application is an activity by itself. Though they work together to present an application sequence, each activity is an independent entity.

Service is another building block of android applications which does not provide a UI. It is a program that can run in the background for an indefinite period.

Broadcast Receiver is yet another type of component that can receive and respond to any broadcast announcements.

Content Providers are a separate league of components that expose a specific set of data to applications.

While the understanding and knowledge of these four components is good enough to start development, the knowledge of the means of communication between the components is also essential. The platform designers have introduced a new conpect of communication through intents and intent filters.

Intents are messages that are passed between components. So, is it equivalent to parameters passed to API calls? Yes, it is close to that. However, the fundamental differences between API calls and intents' way of invoking components is

1. API calls are synchronous while intent-based invocation is asynchronous (mostly)
2. API calls are bound at compile time while intent-based calls are run-time bound (mostly)

It is these two differences that take Android platform to a different league.

NOTE: Intents can be made to work exactly like API calls by using what are called explicit intents, which will be explained later. But more often than not, implicit intents are the way to go and that is what is explained here.

One component that wants to invoke another has to only express its' "intent" to do a job. And any other component that exists and has claimed that it can do such a job through "intent-filters", is invoked by the android platform to accomplish the job. This means, both the components are not aware of each other's existence and can still work together to give the desired result for the end-user.

This dotted line connection between components is achieved through the combination of intents, intent-filters and the android platform.

This leads to huge possibilities like:

1. Mix and match or rather plug and play of components at runtime
2. Replacing the inbuilt android applications with custom developed applications
3. Component level reuse within and across applications
4. Service orientation to the most granular level, if I may say

Now that the concept of intent has been introduced, let me get down to a more formal definition of Intent.

Intent is a bundle of information, a passive data structure that holds an abstract description of the operation to be performed. (or in the case of broadcasts, a description of an event that has happened and is being announced).

There are 2 types of intents which I intend to detail in the next part of this series. Before winding up part 1, I would finally also give you a formal definition of Intent filters.

Intent filters are the means through which a component advertizes its own capabilities to handle specific job/operations to the android platform.

Android Notifications

Android allows to put notification into the titlebar of your application. The user can expand the notification bar and by selecting the notification the user can trigger another activity.

Notifications in Android are represented by the Notification class.

The Notification.Builder provides an builder interface to create an Notification object. 
  private static final int MY_NOTIFICATION_ID = 1;
   private NotificationManager notificationManager;
    private Notification myNotification;
    private final String myBlog = ""; 

    buttonSend.setOnClickListener(new Button.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                myNotification = new Notification(R.drawable.ic_launcher,
                        "Notification!", System.currentTimeMillis());
                Context context = getApplicationContext();
                String notificationTitle = "Exercise of Notification!";
                String notificationText = "";
                Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri
                PendingIntent pendingIntent = PendingIntent.getActivity(
                        MainActivity.this, 0, myIntent,
                myNotification.defaults |= Notification.DEFAULT_SOUND;
                myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
                myNotification.setLatestEventInfo(context, notificationTitle,
                        notificationText, pendingIntent);
                notificationManager.notify(MY_NOTIFICATION_ID, myNotification);


<uses-permission android:name="android.permission.VIBRATE"/>