Pages

Friday, 16 December 2011

Difference Between Android 2.1 vs 2.2


Difference Between Android 2.1 vs 2.2

1. Android 2.2 has more speed and performance optimizations
2. Android 2.2 has USB tethering and Wi-Fi hotspot functionality not found in 2.1
3. Android 2.2 allows for app installation to the memory card while 2.1 does not
4. Android 2.2 adds Flash 10.1 support absent in 2.1
5. Android 2.2 has a lot of modified and added APIs
6. Android 2.2 improves support for Microsoft Exchange over 2.1

Windows Mobile vs Google Android


Windows Mobile vs Google Android

Windows Mobile and Android are two smartphone operating systems that are quite popular nowadays for very different reasons. Windows Mobile, from Microsoft, is a very established operating system that has been around for a considerable length of time. It’s a tried and tested operating systems that people are quite familiar with and know how to operate. Google introduced their operating system rather recently and as such, it is still in its infancy and suffers from a lot of instability that is being addressed.

The most major difference between these two is in the licensing. Where Windows Mobile is a proprietary software that hardware manufacturers need to pay for, Android is open source software that uses Linux at its core. The Android’s licensing also allows other entities to create software for Android without releasing their own source, letting them keep their modifications within their own line of phones. Google does sell some applications that come with the OS, and it is the only way for them to make money out of it.

Because of the gap in maturity of the two, there is a wide margin in terms of market share. Windows Mobile is installed in a wide array of phones from a lot of manufacturers. Google’s Android operating system is only running on less than 10 types of smartphones at the moment and is expected to improve to fewer than 20 at the end of 2009. The same is also true when it comes to third party software. There are a lot more software that can be bought for Windows Mobile compared to Android.

Although, for the meantime, you can only get handsets that have a specific operating system installed, there might come a time when you would be allowed to choose which one you want with the model you like. Google’s Android might be the underdog for now but there is a very real possibility that it would become a contender. Especially when you consider the huge community that are often formed around open source software.
Summary:
 
1. Windows Mobile is from Microsoft while Android was developed by Google
2. Windows Mobile is proprietary while Android is open source
3. Windows Mobile is relatively old and pretty established while the Android is pretty new
4. There are a lot of phones that uses Windows Mobile while there are only a handful running Android
5. There are a lot more programs available for Windows Mobile compared to Android

Android analogclock and digitalclock


1. AnalogClock and DigitalClock

Open “res/layout/main.xml” file, add AnalogClock and DigitalClock in XML.
File : res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Analog Clock"
        android:textAppearance="?android:attr/textAppearanceLarge" />
 
    <AnalogClock
        android:id="@+id/analogClock1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
 
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Digital Clock"
        android:textAppearance="?android:attr/textAppearanceLarge" />
 
    <DigitalClock
        android:id="@+id/digitalClock1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="DigitalClock" />
 
</LinearLayout>

2. Code Code

No idea what can i do with AnalogClock or DigitalClock.
File : MyAndroidAppActivity.java
package com.mkyong.android;
 
import android.app.Activity;
import android.os.Bundle;
import android.widget.AnalogClock;
import android.widget.DigitalClock;
 
public class MyAndroidAppActivity extends Activity {
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
 
  AnalogClock ac = (AnalogClock) findViewById(R.id.analogClock1);
  //what can i do with AnalogClock?
 
  DigitalClock dc = (DigitalClock) findViewById(R.id.digitalClock1);
  //what can i do with DigitalClock also? for display only
 
 }
 
}

3. Demo

Run the application.
1. This is how AnalogClock and DigitalClock look like :
android analogclock and digitalclock demo

Using android Time Picker


1. TimePicker

Open “res/layout/main.xml” file, add time picker, label and button for demonstration.
File : res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <Button
        android:id="@+id/btnChangeTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Change Time" />
 
    <TextView
        android:id="@+id/lblTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Current Time (H:M): "
        android:textAppearance="?android:attr/textAppearanceLarge" />
 
    <TextView
        android:id="@+id/tvTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceLarge" />
 
    <TimePicker
        android:id="@+id/timePicker1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
 
</LinearLayout>
P.S The “TimePickerDialog” is declare in code, not XML.

2. Code Code

Read the code’s comment, it should be self-explanatory.
File : MyAndroidAppActivity.java
package com.mkyong.android;
 
import java.util.Calendar;
import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
 
public class MyAndroidAppActivity extends Activity {
 
 private TextView tvDisplayTime;
 private TimePicker timePicker1;
 private Button btnChangeTime;
 
 private int hour;
 private int minute;
 
 static final int TIME_DIALOG_ID = 999;
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
 
  setCurrentTimeOnView();
  addListenerOnButton();
 
 }
 
 // display current time
 public void setCurrentTimeOnView() {
 
  tvDisplayTime = (TextView) findViewById(R.id.tvTime);
  timePicker1 = (TimePicker) findViewById(R.id.timePicker1);
 
  final Calendar c = Calendar.getInstance();
  hour = c.get(Calendar.HOUR_OF_DAY);
  minute = c.get(Calendar.MINUTE);
 
  // set current time into textview
  tvDisplayTime.setText(
                    new StringBuilder().append(pad(hour))
                                       .append(":").append(pad(minute)));
 
  // set current time into timepicker
  timePicker1.setCurrentHour(hour);
  timePicker1.setCurrentMinute(minute);
 
 }
 
 public void addListenerOnButton() {
 
  btnChangeTime = (Button) findViewById(R.id.btnChangeTime);
 
  btnChangeTime.setOnClickListener(new OnClickListener() {
 
   @Override
   public void onClick(View v) {
 
    showDialog(TIME_DIALOG_ID);
 
   }
 
  });
 
 }
 
 @Override
 protected Dialog onCreateDialog(int id) {
  switch (id) {
  case TIME_DIALOG_ID:
   // set time picker as current time
   return new TimePickerDialog(this, 
                                        timePickerListener, hour, minute,false);
 
  }
  return null;
 }
 
 private TimePickerDialog.OnTimeSetListener timePickerListener = 
            new TimePickerDialog.OnTimeSetListener() {
  public void onTimeSet(TimePicker view, int selectedHour,
    int selectedMinute) {
   hour = selectedHour;
   minute = selectedMinute;
 
   // set current time into textview
   tvDisplayTime.setText(new StringBuilder().append(pad(hour))
     .append(":").append(pad(minute)));
 
   // set current time into timepicker
   timePicker1.setCurrentHour(hour);
   timePicker1.setCurrentMinute(minute);
 
  }
 };
 
 private static String pad(int c) {
  if (c >= 10)
     return String.valueOf(c);
  else
     return "0" + String.valueOf(c);
 }
}
P.S The “TimePickerDialog” example above, is referenced from Google Android time picker example, with some minor change.

3. Demo

Run the application.
1. Result, “time picker” and “textview” are set to current time.
android timepicker demo1
2. Click on the “Change Time” button, it will prompt a time picker component in a dialog box via TimePickerDialog.
android timepicker demo2
3. Both “time picker” and “textview” are updated with selected time.
android timepicker demo3

Using android Date Picker




This is a sample activity which shows how to use Date picker control.
Underlying Algorithm:
Basic description of algorithm in step by step form:
1.) Create a Project DatePickerExample.
2.) Create a date_picker.xml in res/layout.
3.) Put the following code in date_picker.xml :
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:orientation="horizontal">
 <TextView android:id="@+id/dateDisplay"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:paddingLeft="4dip"
           android:text="@string/hello"/>
        <Button android:id="@+id/pickDate"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:paddingLeft="4dip"
           android:text="@string/hello"/>    
</LinearLayout>
4.) To create a date picker control we need the following imports :
import java.util.Calendar;
import android.app.DatePickerDialog;
import android.widget.DatePicker;
5.) Run the application.
Steps to Create:
1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. DatePickerExample. Enter following information:
Project name: DatePickerExample
Build Target: Google APIs
Application name: DatePickerExample
Package name: com.sample.DatePickerExample
Create Activity: DatePickerExample
On Clicking Finish DatePickerExample code structure is generated with the necessary Android Packages being imported along with DatePickerExample.java. DatePickerExample class will look like following:
package com.sample.DatePickerExample;
import android.app.Activity;
import java.util.Calendar;
import android.app.DatePickerDialog;
import android.widget.DatePicker;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class DatePickerExample extends Activity {
        private TextView mDateDisplay;
        private int mYear;
        private int mMonth;
        private int mDay;
        static final int DATE_DIALOG_ID = 1;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.date_picker);
                mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
                Button pickDate = (Button) findViewById(R.id.pickDate);
                pickDate.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                                showDialog(DATE_DIALOG_ID);
                        }
                });
                final Calendar c = Calendar.getInstance();
                mYear = c.get(Calendar.YEAR);
                mMonth = c.get(Calendar.MONTH);
                mDay = c.get(Calendar.DAY_OF_MONTH);
                updateDisplay();
        }
        @Override
        protected Dialog onCreateDialog(int id) {
                switch (id) {
                case DATE_DIALOG_ID:
                        return new DatePickerDialog(this,
                                mDateSetListener,
                                mYear, mMonth, mDay);
                }
                return null;
        }
        protected void onPrepareDialog(int id, Dialog dialog) {
                switch (id) {
                case DATE_DIALOG_ID:
                        ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
                        break;
                }
        }  
        private void updateDisplay() {
                mDateDisplay.setText(
                        new StringBuilder()
                        // Month is 0 based so add 1
                        .append(mMonth + 1).append("-")
                        .append(mDay).append("-")
                        .append(mYear).append(" "));
        }
        private DatePickerDialog.OnDateSetListener mDateSetListener =
                new DatePickerDialog.OnDateSetListener() {
                public void onDateSet(DatePicker view, int year, int monthOfYear,
                                int dayOfMonth) {
                        mYear = year;
                        mMonth = monthOfYear;
                        mDay = dayOfMonth;
                        updateDisplay();
                }
        };
}
Output –The final output: