Discuss IT and Design a variety of tutorials and design it. let's read that are useful to you. thank you for visiting my humble blog

New Features for Application Development - Adding an action bar

Adding an action bar
After Ice Cream Sandwich, Android doesn't require the menu button to reach the options menu. The best practice is to use action bar instead of the menu button. It is very easy to migrate from the options menu to the action bar. Now we are going to create a menu and then migrate that menu to the action bar.
Firstly, create an Android project and then add a menu that contains Settings and About as menu items. The
resulting menu XML file should look like the following code block:
You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/settings"
    android:title="Settings"></item>
    <item android:id="@+id/about" android:title="About"></item>
</menu>
The layout XML for this sample is a LinearLayout layout with a TextView
component in it as shown in the following code block:
<?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:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
</LinearLayout>

Implement the onCreateOptionsMenu and onOptionsItemSelected methods as shown in the following code block, in order to show the menu items:
package com.chapter1;
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 Chapter1Activity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
            //Inflate the menu.xml of the android project
             //in order to create menu
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
    }
    @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    //According to selection, show the Toast message
    //of the selected button
    switch (item.getItemId()) {
    case R.id.settings:
      Toast.makeText(this, "Settings options menu button
      is pressed", Toast.LENGTH_LONG).show();
      return true;
    case R.id.about:
      Toast.makeText(this, "About options menu button is pressed",
      Toast.LENGTH_LONG).show();
      return true;
      default:
      return super.onOptionsItemSelected(item);
    }
  }
}

In order to display the action bar, the Android applications should target a minimum of API Level 11 in the AndroidManifest.xml file as shown in the following code block:
<?xml version="1.0" encoding="utf-8"?>
<!—set targetSDKversion to 11 because Action Bar is
     available since API Level 11-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.chapter1"
    android:versionCode="1"
    android:versionName="1.0" >
    < uses-sdk android:minSdkVersion="5"
               android:targetSdkVersion="11"  />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".Chapter1Activity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.
                LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

With this configuration, when the application runs on devices that have Android 3.0 or greater, the action bar will be displayed.
Share:

No comments:

Post a Comment

Popular Posts

Ismail Maha Putra. Powered by Blogger.
twitterfacebookgoogle pluslinkedinrss feedemail
SEO Reports for kendariit.blogspot.com

Followers

Blog Archive

Poll

Facebook Page Like

Translate

Get ThisWidget
Blogger Widgets

submit your site

http://smallseotools.com/google-pagerank-checker

Sonicrun

Google Search

Popular Posts

Tutorials, IT and Design

Blog Archive

Recent Posts

About Metro