Posts

Showing posts from February, 2017

Fragments in android

Image
A  Fragment  is a piece of an activity which enable more modular activity design. It will not be wrong if we say, a fragment is a kind of  sub-activity . Following are important points about fragment − A fragment has its own layout and its own behaviour with its own life cycle 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-plane UI. A fragment can be used in multiple activities. Fragment life cycle is closely related to the life cycle of its host activity which means when the activity is paused, all the fragments available in the activity will also be stopped. A fragment can implement a behaviour that has no user interface component. Fragments were added to the Android API in Honeycomb version of Android which API version 11. You create fragments by extending  Fragment  class and You can insert a fragment into your activ...

Asynchronous Tasks in Android

AsyncTask  is an abstract class provided by Android which helps us to use the UI(User Interface) thread properly. This class allows us to perform long/background operations and show its result on the UI(User Interface) thread without having to manipulate threads. Android implements single thread model and whenever an android application is launched, a thread is  created.Let  on any event like, on button click a request would be made to the server and response will be awaited. Due to single thread model of android, till the time response is awaited our screen is non-responsive. So we should avoid performing long running operations on the UI(User Interface) thread. AsyncTask has four main methods to perform certain operations: doInBackground :  Code performing long running operation goes in this method.  When onClick method is executed on click of button, it calls execute method which accepts parameters and automatically calls doInBackground ...

Android intent/filters

Image
An Android  Intent  is an abstract description of an operation to be performed. It can be used with  startActivity  to launch an Activity,  broadcastIntent  to send it to any interested BroadcastReceiver components, and  startService(Intent)  or  bindService(Intent, ServiceConnection, int)  to communicate with a background Service. The intent itself, an Intent object, is a passive data structure holding an abstract description of an operation to be performed. For example, let's assume that you have an Activity that needs to launch an email client and sends an email using your Android device. For this purpose, your Activity would send an ACTION_SEND along with appropriate  chooser , to the Android Intent Resolver. The specified chooser gives the proper interface for the user to pick how to send your email data. Intent email = new Intent ( Intent . ACTION_SEND , Uri . parse ( "mailto:" )); email . putExtra ( Intent . EXTRA...

Alert Dialog Boxes

Sometimes the application you are creating,  want to ask the user about taking a decision between yes or no in response of any particular action taken by the user, by remaining in the same activity and without changing the screen, you can use Alert Dialog. In order to make an alert dialog, you need to make an object of AlertDialogBuilder class which an inner class of AlertDialog. The syntax to be followed is given below. AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); Now  to set the positive, negative and neutral buttons using the object of the AlertDialogBuilder class. Its syntax is alertDialogBuilder.setPositiveButton( CharSequence  text ,   DialogInterface . OnClickListener  listener ); alertDialogBuilder.setNegativeButton( CharSequence  text ,   DialogInterface . OnClickListener  listener ); alertDialogBuilder.setNeutralButton( CharSequence  text ,   DialogInterface . OnClickListener ...

Services in android

Image
A  service  is a component that runs in the background to perform long-running operations without needing to interact with the user and it works even if application is destroyed. A service can essentially take two states − Sr.No. State & Description 1 Started A service is  started  when an application component, such as an activity, starts it by calling  startService() . Once started, a service can run in the background indefinitely, even if the component that started it is destroyed. 2 Bound A service is  bound  when an application component binds to it by calling  bindService() . A bound service offers a client-server interface that allows components to interact with the service, send requests, get results, and even do so across processes with interprocess communication (IPC). A service has life cycle callback methods that you can implement to monitor changes in the service's state and you can perform work at the appropriate...

Android Broadcast reciever

In Android, a  broadcast receiver  is an Android component which allows you to register for system or application events. All registered receivers for an event are notified by the Android run time once this event happens. For example, applications can register for the  ACTION_POWER_CONNECTED  system event which is fired once the Android system has completed the boot process. A receiver can be registered via the  AndroidManifest.xml  file. Alternatively to this static registration, you can also register a receiver dynamically via the Context.registerReceiver()  method. The implementing class for a receiver extends the  BroadcastReceiver  class. If the event for which the broadcast receiver has registered happens, the  onReceive()  method of the receiver is called by the Android system.

Android Activity

Image
If you have worked with C, C++ or Java programming language then you must have seen that your program starts from  main()  function. Very similar way, Android system initiates its program with in an  Activity  starting with a call on  onCreate()  callback method. There is a sequence of callback methods that start up an activity and a sequence of callback methods that tear down an activity as shown in the below Activity life cycle diagram: ( image courtesy : android.com  ) The Activity class defines the following call backs i.e. events. You don't need to implement all the callbacks methods. However, it's important that you understand each one and implement those that ensure your app behaves the way users expect. Sr.No Callback & Description 1 onCreate() This is the first callback and called when the activity is first created. 2 onStart() This callback is called when the activity becomes visible to the user. 3 onResume() This is ...

Android Event handling

Events are a useful way to collect data about a user's interaction with interactive components of Applications.  Like button presses or screen touch etc. The Android framework maintains an event queue as first-in, first-out (FIFO) basis. You can capture these events in your program and take appropriate action as per requirements. There are following three concepts related to Android Event Management − Event Listeners  − An event listener is an interface in the View class that contains a single callback method. These methods will be called by the Android framework when the View to which the listener has been registered is triggered by user interaction with the item in the UI. Event Listeners Registration  − Event Registration is the process by which an Event Handler gets registered with an Event Listener so that the handler is called when the Event Listener fires the event. Event Handlers  − When an event happens and we have registered an event listener...