• Latest Code...

    Action Bar Demo in Android

    The action bar is a window feature that identifies the user location, and provides user actions and navigation modes. Using the action bar offers your users a familiar interface across applications that the system gracefully adapts for different screen configurations.
    Lessons

    Setting Up the Action Bar
    Learn how to add a basic action bar to your activity, whether your app supports only Android 3.0 and higher or also supports versions as low as Android 2.1 (by using the Android Support Library).
    Adding Action Buttons
    Learn how to add and respond to user actions in the action bar.
    Styling the Action Bar
    Learn how to customize the appearance of your action bar.
    Overlaying the Action Bar
    Learn how to overlay the action bar in front of your layout, allowing for seamless transitions when hiding the action bar.
    Here is the example of Action Bar demo App

    first define menu in   res/menu/main.xml:

    <menu xmlns:android="http://schemas.android.com/apk/res/android" >

        <item
            android:id="@+id/refresh"
            android:alphabeticShortcut="r"
            android:icon="@drawable/ic_action_refresh"
            android:orderInCategory="100"
            android:showAsAction="always"/>
        <item
            android:id="@+id/share"
            android:actionProviderClass="android.widget.ShareActionProvider"
            android:icon="@drawable/ic_action_share"
            android:orderInCategory="1"
            android:showAsAction="collapseActionView"
            android:title="Share"/>
        <item
            android:id="@+id/update"
            android:icon="@drawable/db_update_24"
            android:orderInCategory="100"
            android:showAsAction="collapseActionView"
            android:title="Update"/>

    </menu>

    Now define for refresh icons with circling with progress in to: res/layout/actionbar_indeterminate_progress.xml 

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_height="wrap_content"
       android:layout_width="56dp"
       android:minWidth="56dp">
        <ProgressBar android:layout_width="32dp"
           android:layout_height="32dp"
           android:layout_gravity="center"/>
    </FrameLayout>

    another xml file defined in layout: res/layout/main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/relativeLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#00FFFF" >

    </RelativeLayout>


    Now finally add your project MainActivity java file with full code:

    package com.example.searchdemo;

    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.os.StrictMode;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.Toast;
    import android.annotation.TargetApi;
    import android.os.Build;
    import android.view.ViewConfiguration;
    import android.widget.ShareActionProvider;

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public class MainActivity extends Activity {
    private int toast_time = 100;
    private Menu optionsMenu;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    getOverflowMenu();
    }

    // check strictmode and ignore it
    private void strictMode() {
    if (android.os.Build.VERSION.SDK_INT > 9) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
    .permitAll().build();
    StrictMode.setThreadPolicy(policy);
    }
    }


    // inflate for action bar
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    this.optionsMenu = menu;
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    }

    // handle click events for action bar items
    @SuppressLint("NewApi")
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

    case R.id.refresh:
    // showToast(getResources().getString(R.string.refresh));
    setRefreshActionButtonState(true);
    Intent intent = getIntent();
    finish();
    startActivity(intent);
    // setRefreshActionButtonState(false);
    return true;

    case R.id.share:
    // showToast("Share was clicked.");
    String shareBody="Sharing Message";
    ShareActionProvider myShareActionProvider = (ShareActionProvider) item
    .getActionProvider();

    Intent myIntent = new Intent();
    myIntent.setAction(Intent.ACTION_SEND);
    myIntent.putExtra(Intent.EXTRA_TEXT, shareBody);
    myIntent.setType("text/plain");

    myShareActionProvider.setShareIntent(myIntent);

    return true;
    case R.id.update:
    showToast("Update");
    return true;
    default:
    return super.onOptionsItemSelected(item);
    }
    }


    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @SuppressLint("NewApi")
    public void setRefreshActionButtonState(final boolean refreshing) {
    if (optionsMenu != null) {
    final MenuItem refreshItem = optionsMenu.findItem(R.id.refresh);
    if (refreshItem != null) {
    if (refreshing) {
    refreshItem
    .setActionView(R.layout.actionbar_indeterminate_progress);
    } else {
    refreshItem.setActionView(null);
    }
    }
    }
    }



    // put the other two menu on the three dots (overflow)
    private void getOverflowMenu() {

    try {

    ViewConfiguration config = ViewConfiguration.get(this);
    java.lang.reflect.Field menuKeyField = ViewConfiguration.class
    .getDeclaredField("sHasPermanentMenuKey");
    if (menuKeyField != null) {
    menuKeyField.setAccessible(true);
    menuKeyField.setBoolean(config, false);
    }
    } catch (Exception e) {
    e.printStackTrace();
    }

    }

    // so that we know something was triggered
    public void showToast(String msg) {
    Toast.makeText(this, msg, toast_time).show();
    }

    }

    here is the output of above demo app snapshots:






    Hope, this demo helpful for developing apps.

    Do you want more about action bar go following links:
    http://developer.android.com/guide/topics/ui/actionbar.html

    https://developer.android.com/training/basics/actionbar/index.html

    https://www.youtube.com/watch?v=4BOE9TeUY1w

    http://developer.android.com/design/patterns/actionbar.html

    http://www.androidhive.info/2013/11/android-working-with-action-bar/

    http://jgilfelt.github.io

    Happy Coding !!!





    Circle

    Most recent Elastic Search tutorials for beginner, intermediate and advance level developer, Yubraj Pokharel, in CodingWorkspace