Posts

Showing posts from November, 2012

How to update UI from a different thread

     Google has set it’s tone on some tough guidelines for developing android apps, basically there aren’t any strict rules but one very easy to misunderstand. there is a single Thread which is called the “main” thread, or “UI thread” on some places. this is the thread which is in charge on the views and interaction with the users gestures. based on the guidelines of any mobile platform at the moment, all the work that shouldn’t be done on the main thread should be done on background thread. this can be achieved in many ways, you can use AsyncTask, Or A Handler, Or a simple thread with an implementation of the   run() method within the declaration of the runnable. ok, so if I’m pulling some server data, or performing long calculations, pulling stuff from or to the Sqlite database I’ll probably use an AsyncTask. but let’s say for instance that I want to simply update the UI once every couple of seconds (update a TextView or an ImageView’s content etc..), here’s a simple example of i

How to maintain global application state

       In Android application, it’s sometimes important to keep a global application state. by global application state I mean to be able to have an entry point to the application which gives us a starter place to do stuff before the application starts. this is important in many aspects, for example, the application can start from different entry points, such as the launcher, a notification, or a service, or even by responding to a certain broadcast.     There are many approaches to achieve this goal, one of them which a prefer is the Application Class, once extending the application class, we are able to load resources, setting different contexts , or even starting a certain service before the first activity starts, this is done simply by overriding the onCreate() method. Example: public class MyApplication extends Application { boolean hasAgreed; @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); // get d

Installing the development environment for android is now easier then ever

  Developing for android can a complicated task. Some developers including myself had a hard time starting with the development due to a somewhat complicated process which should be done prior to development – Installing the development enjoinment. how we used to do it: Download the SDK Download the Eclipse Download the ADT – (Android development tools) Download the plugin for eclipse (by using https://dl.google.com/ …) now it’s easier: go to https://developer.android.com/sdk/index.html , download the package and it will include all of the above. this is probably perfect for starting to develop for the Android without having to deal with the messy installation process.

Fragments–applications made modular

  The Android SDK 3.0 (Honeycomb) introduced the Fragments API. The Fragments API is basically a set of tools which allows us to create, manage and re-use Chuncks of logic and UI. For example, let’s assume I’m starting to develop an Application, which should be compatible for both Smartphones and Tablet, as well as support both orientation(landscape, portrait). It’s fairly easy to say that the same “lisviews” or forms will be used on both versions in terms of business logic and behavior with some UI adjustments. the fragments API is exactly what we need in order to do it in an elegant manner. let’s check out the following sample: public class MainActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); Button btn = (Button) findViewById(R.id.button); btn.setOnClickListener( new OnClickListener() {