Monday 26 November 2012

Fragments


Fragments
A Fragments represents a behavior or a portion of user interface in an Avtivity.You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities).

For example, when the activity is paused, so are all fragments in it, and when the activity is destroyed, so are all fragments. However, while an activity is running (it is in the resumed lifecycle state), you can manipulate each fragment independently, such as add or remove them. When you perform such a fragment transaction, you can also add it to a back stack that's managed by the activity—each back stack entry in the activity is a record of the fragment transaction that occurred. The back stack allows the user to reverse a fragment transaction (navigate backwards), by pressing the Back button.

1.)  Create a new project by File-> New -> Android Project name it FragmentExample.
2.)  Write following into main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:padding="4dip"
    android:gravity="center_horizontal"
    android:layout_width="match_parent" android:layout_height="match_parent">
 <FrameLayout
            android:id="@+id/simple_fragment"
            android:layout_width="match_parent"
            android:layout_height="0px"
            android:layout_weight="1">
    </FrameLayout>
  <Button android:id="@+id/new_fragment"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_weight="0"
        android:text="New fragment">
        <requestFocus />
    </Button>
 </LinearLayout>

3.)  Create and write following into fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:gravity="center_vertical|center_horizontal"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="FragmentExample"/>

4.)  Create animator folder and write fragment left,right, entry and exit animation.
5.)  Run for output.

Tuesday 20 November 2012

MENU in Android


MENU in Android

Menus are a common user interface component in many types of applications. To provide a familiar and consistent user experience, you should use the Menu APIs to present user actions and other options in your activities.
Beginning with Android 3.0 (API level 11), Android-powered devices are no longer required to provide a dedicated Menu button. With this change, Android apps should migrate away from a dependence on the traditional 6-item menu panel and instead provide an action bar to present common user actions.


To define the menu, create an XML file inside your project's res/menu/ directory and build the menu with the following elements:

<menu>
Defines a Menu, which is a container for menu items. A <menu> element must be the root node for the file and can hold one or more <item> and <group> elements.

<item>
Creates a MenuItem, which represents a single item in a menu. This element may contain a nested <menu>element in order to create a submenu.

<group>
An optional, invisible container for <item> elements. It allows you to categorize menu items so they share properties such as active state and visibility.
Where the items in your options menu appear on the screen depends on the version for which you've developed your application:

If you've developed your application for Android 2.3.x (API level 10) or lower, the contents of your options menu appear at the bottom of the screen when the user presses the Menu button. When opened, the first visible portion is the icon menu, which holds up to six menu items. If your menu includes more than six items, Android places the sixth item and the rest into the overflow menu, which the user can open by selecting More.

If you've developed your application for Android 3.0 (API level 11) and higher, items from the options menu are available in the action bar. By default, the system places all items in the action overflow, which the user can reveal with the action overflow icon on the right side of the action bar (or by pressing the device Menu button, if available). To enable quick access to important actions, you can promote a few items to appear in the action bar by adding android:showAsAction="ifRoom" to the corresponding  <item> elements.

In this tutorial we are creating a simple menu with 6 menu items. On clicking on single menu item a simple Toast message will be shown.
1.    Create a new project File New Android Project and give activity name asAndroidMenusActivity.
2. Now create an XML file under res/layout folder and name it as menu.xml.
3. Open menu.xml file and type following code. In the following code we are creating a single menu with 6 menu items. Each menu item has an icon  and  title for display the label under menu icon. Also we have id for each menu item to identify uniquely.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
            <!-- Single menu item
                         Set id, icon and Title for each menu item
            -->
    <item android:id="@+id/menu_bookmark"
              android:icon="@drawable/icon_bookmark"
          android:title="Bookmark" />

    <item android:id="@+id/menu_save"
              android:icon="@drawable/icon_save"
          android:title="Save" />

    <item android:id="@+id/menu_search"
              android:icon="@drawable/icon_search"
          android:title="Search" />

    <item android:id="@+id/menu_share"
              android:icon="@drawable/icon_share"
          android:title="Share" />

    <item android:id="@+id/menu_delete"
              android:icon="@drawable/icon_delete"
          android:title="Delete" /> 

    <item android:id="@+id/menu_preferences"
              android:icon="@drawable/icon_preferences"
          android:title="Preferences" />
</menu>

4. Now open your main Activity class file (AndroidMenusActivity.java) and type following code. In the following code each menu item is identified by its ID in switch case statement.

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class AndroidMenusActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    // Initiating Menu XML file (menu.xml)
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.layout.menu, menu);
        return true;
    }

    /**
     * Event Handling for Individual menu item selected
     * Identify single menu item by it's id
     * */
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {

        switch (item.getItemId())
        {
        case R.id.menu_bookmark:
            // Single menu item is selected do something
            // Ex: launching new activity/screen or show alert message

            Toast.makeText(AndroidMenusActivity.this, "Bookmark is Selected", Toast.LENGTH_SHORT).show();
            return true;

        case R.id.menu_save:
            Toast.makeText(AndroidMenusActivity.this, "Save is Selected", Toast.LENGTH_SHORT).show();
            return true;

        case R.id.menu_search:
            Toast.makeText(AndroidMenusActivity.this, "Search is Selected", Toast.LENGTH_SHORT).show();
            return true;

        case R.id.menu_share:
            Toast.makeText(AndroidMenusActivity.this, "Share is Selected", Toast.LENGTH_SHORT).show();
            return true;

        case R.id.menu_delete:
            Toast.makeText(AndroidMenusActivity.this, "Delete is Selected", Toast.LENGTH_SHORT).show();
            return true;

        case R.id.menu_preferences:
            Toast.makeText(AndroidMenusActivity.this, "Preferences is Selected", Toast.LENGTH_SHORT).show();
            return true;

        default:
            return super.onOptionsItemSelected(item);
        }
    }   

}
5. Finally run your project by right clicking on your project folder Run As 1 Android Application to test your application. On Android Emulator click on Menu Button to launch menu.