Thursday, February 17, 2011

DatePicker

By Magesh Kumar   Posted at  2:21 AM   Android No comments

A DatePicker is a widget that allows the user to select a month, day and year. java.lang.Object -> android.view.View -> android.view.ViewGroup -> android.widget.FrameLayout -> android.widget.DatePicker.
Layout file:
<?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="vertical">

<TextView android:id="@+id/dateDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""/>

<Button android:id="@+id/pickDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change the date"/>

</LinearLayout>
For the layout, we're using a vertical LinearLayout, with a TextView that will display the date and a Button that will initiate the DatePicker dialog. With this layout, the TextView will sit above the Button. The text value in the TextView is set empty, as it will be filled with the current date when our Activity runs.
Java code, myDatePicker.java:
package com.bogotobogo.myDatePicker;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

public class myDatePicker extends Activity {
       private TextView mDateDisplay;
       private Button mPickDate;

       private int mYear;
       private int mMonth;
       private int mDay;

       static final int DATE_DIALOG_ID = 0;

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);

              // capture our View elements
              mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
              mPickDate = (Button) findViewById(R.id.pickDate);

              // add a click listener to the button
              mPickDate.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                           showDialog(DATE_DIALOG_ID);
                    }
              });

              // get the current date
              final Calendar c = Calendar.getInstance();
              mYear = c.get(Calendar.YEAR);
              mMonth = c.get(Calendar.MONTH);
              mDay = c.get(Calendar.DAY_OF_MONTH);

              // display the current date
              updateDisplay();
       }
      
       @Override
       protected Dialog onCreateDialog(int id) {
              switch (id) {
                    case DATE_DIALOG_ID:
                           return new DatePickerDialog(this,
                                               mDateSetListener,
                                               mYear, mMonth, mDay);
              }
              return null;
       }
      
       // updates the date we display in the TextView
       private void updateDisplay() {
              mDateDisplay.setText(
                           new StringBuilder()
                           // Month is 0 based so add 1
                           .append(mMonth + 1).append("-")
                           .append(mDay).append("-")
                           .append(mYear).append(" "));
       }
      
       // the callback received when the user "sets" the date in the dialog
       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();
                    }
              };
}
Let's look at the code. 
We start by instantiating variables for our Views and date fields. The DATE_DIALOG_ID is a static integer that uniquely identifies the Dialog.
 
In the
 onCreate() method, we get prepared by setting the layout and capturing the View elements.
       mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
       mPickDate = (Button) findViewById(R.id.pickDate);
Then we create an on-click listener for the Button, so that when it is clicked it will show our DatePicker dialog. The showDialog() method will pop-up the date picker dialog by calling the onCreateDialog() callback method.
       mPickDate.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
                    showDialog(DATE_DIALOG_ID);
              }
       });
We then create an instance of Calendar and get the current year, month and day. Finally, we call updateDisplay()�our own method (defined later) that will fill the TextView.
       final Calendar c = Calendar.getInstance();
       mYear = c.get(Calendar.YEAR);
       mMonth = c.get(Calendar.MONTH);
       mDay = c.get(Calendar.DAY_OF_MONTH);

       updateDisplay();
Next, onCreateDialog() callback method. 
This method is called by
 showDialog() and it is passed the identifier we gave showDialog() and initializes the DatePicker to the date we retrieved from our Calendar instance.
       @Override
       protected Dialog onCreateDialog(int id) {
              switch (id) {
                    case DATE_DIALOG_ID:
                           return new DatePickerDialog(this,
                                               mDateSetListener,
                                               mYear, mMonth, mDay);
              }
              return null;
       }
The updateDisplay() method uses the member date values to write the date to our TextView.
       private void updateDisplay() {
              mDateDisplay.setText(
                    new StringBuilder()
                    // Month is 0 based so add 1
                    .append(mMonth + 1).append("-")
                    .append(mDay).append("-")
                    .append(mYear).append(" "));
       }
This OnDateSetListener() method listens for when the user is done setting the date (clicks the "Set" button). At that time, this fires and we update our member fields with the new date defined by the user and update our TextView by callingupdateDisplay().
       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();
                    }
              };

Time to run our application.
We'll get something like this:


  


Wednesday, February 16, 2011

Android Menu and Submenu Example

By Magesh Kumar   Posted at  5:16 AM   Android No comments

Create menu.xml file
New->Android Xml file
Filename -> menu.xml and select xml file type as menu.

menu.xml

<?xml version="1.0" encoding="utf-8"?>

            <menu
              xmlns:android="http://schemas.android.com/apk/res/android">
                <item android:id="@+id/Menu1"
                    android:title="Menu 1"
                    android:orderInCategory="1" />
                <item android:id="@+id/Menu2"
                    android:orderInCategory="2"
                    android:title="Menu 2" />
                <item android:id="@+id/Menu3"
                    android:orderInCategory="3"
                    android:title="Menu 3" />
                <item android:id="@+id/submenu"
                    android:title="Sub menu"
                    android:orderInCategory="4">
                    <menu>
                        <item android:id="@+id/submenu1"
                        android:title="Sub menu 1" />
                        <item android:id="@+id/submenu2"
                        android:title="Sub menu 2" />
                    </menu>
               </item>
            </menu>

MenuActivity.java
            public class MenuActivity extends Activity {
                /** Called when the activity is first created. */
                @Override
                public void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.main);
                }
                public boolean onCreateOptionsMenu(Menu menu) {
                    new MenuInflater(getApplication())
                            .inflate(R.menu.menu, menu);
                    return(super.onPrepareOptionsMenu(menu));
                }
             
                public boolean onOptionsItemSelected(MenuItem item) {
                     switch (item.getItemId()) {
                        case R.id.Menu1:
                            Toast.makeText(this, "Menu 1", Toast.LENGTH_SHORT).show();
                            break;
                        case R.id.Menu2:
                            Toast.makeText(this, "Menu 2", Toast.LENGTH_SHORT).show();
                            break;
                        case R.id.Menu3:
                            Toast.makeText(this, "Menu 3", Toast.LENGTH_SHORT).show();
                            break;
                        case R.id.submenu:
                            Toast.makeText(this, "Sub menu", Toast.LENGTH_SHORT).show();
                            break;
                    }
                    return(super.onOptionsItemSelected(item));
                }
            }

SnapShot:






Tuesday, February 15, 2011

Google unveils Android 3.0 Honeycomb SDK preview to developers

By Magesh Kumar   Posted at  4:01 AM   Android No comments

Android honeycomb

Google has released a preview of Android 3.0 Honeycomb SDK that enables developers to take advantage of non-final APIs and system image APIs optimized to run existing applications on tablets and the SDK is only for testing purposes. Google also released updates for SDK of Android developer tools: SDK Tools (r9), NDK (r5b), and ADT Plugin for Eclipse (9.0.0).
Android Developer blog states that apps developed with the Honeycomb preview cannot be published on the Android Market.  A final Android 3.0 Honeycomb SDK will be released in the coming weeks. Android 3.0 offers developers the flexibility to adapt existing apps to the new UI while maintaining compatibility with earlier platform versions and other form-factors
Major native Android applications like the Browser, Email, Contacts, Camera, and Gallery have received updates optimized to larger tablet-sized screens and similar devices. The Browser comes with a new “incognito” mode for private browsing, Contacts with a new two-pane UI, and the Email app also with a new two-pane UI for organizing messages. The redesigned Camera app with a new UI for large screen supports video chat. Honeycomb also brings with it an improved keyboard that includes a new Tab key and upgraded copy/paste functionality.
Honeycomb 3.0 offers improved multitasking to the recent apps provides rich multimedia experience to users which will allow them to preview applications that are currently running, and check its updates on what actions done on the application when they last viewed it.
Android 3.0 features a UI framework designed for creating apps on tablet-sized screens, a new animation framework, a built-in OpenGL renderer to accelerate 2D processing, a 3D graphics engine called Renderscript, and multicore support.
Features

  • UI framework enable developers to use a new UI components, new holographic themes, widgets, notifications, drag and drop, and more
  • New animation framework for high-performance 2D and 3D graphics allows developers to move between views swiftly and it adds great visual effects to apps.
  • Built-in GL renderer allows developers for GPU acceleration of 2D and 3D visual content, across the entire app or only in specific activities or views.Renderscript allows developers take advantage of a new 3D graphics engine for including rich 3D scenes.
  • Multicore processor support allows Android 3.0 to run on single- or dual-core processors for high performance of applications.
  • Rich multimedia features with HTTP Live streaming support, a pluggable DRM framework, and a media file transfer via MTP/PTP that developers can leverage for rich user interactive content
  • New connectivity options enable to sync media files with a desktop PC or camera with USB support. New APIs for Bluetooth A2DP and HSP enable applications to offer audio streaming and headset control. Support for Bluetooth insecure socket connection lets applications connect to simple devices that may not have a user interface.
  • Enterprise feature enhancements with new administrative policies for encrypted storage and password expiration helps enterprises to manage devices effectively
Check out the video below for Android 3.0 Honeycomb preview

Back to top ↑
Connect with Us

What they says

© 2013 MaGeSH 2 help. WP Mythemeshop Converted by BloggerTheme9
Blogger templates. Proudly Powered by Blogger.