Thứ Ba, 9 tháng 10, 2012

Adding menus to action bar in pre Honeycomb versions using Sherlock library

In the article titled “Adding Action items and Overflow menu items to Action Bar in Android“, we have seen how to add action items and overflow menu items to action bar in Android. Since action bar is introduced in Android 3.0 ( API Level 11 ) , we can not run that application in Android versions prior to Honeycomb. In order to overcome this limitation, we can make use Action bar Sherlock library which facilitates to create action bar in pre Honeycomb versions ( 2.x versions ).


 The application in this article is developed in Eclipse ( 3.7.2 ) with ADT plugin ( 20.0.2 ),  Android SDK ( R20.0.1 ) and Action bar Sherlock library ( 4.1.0 )

1. Setup Action bar Sherlock library in Eclipse IDE


To setup Action bar Sherlock library in Eclipse IDE, refer the article “Setting up Sherlock library for action bar in pre Honeycomb Android Applications“.


 2. Create new Android Application project namely “ActionBarSherlockMenu”


Figure 1 : Create new Android Application Project

3. Design application launcher


Figure 2 : Design Application Launcher

4. Create a Blank Activity




Figure 3 : Create a blank Activity

5. Enter MainActivity Details



Figure 4 : Enter Main Activity Details

6. Delete default Support library


By default, ADT plugin adds a support library ( lib/android-support-v4.jar ) to the project. This is not needed for our application since this is being added by Sherlock library. So in order to avoid the conflict, delete the default support library via Eclipse’s project explorer.

7. Add Sherlock library to this project


Open the properties window of this project ( by right clicking the application at project explorer ) , and select Android tab to add the Sherlock library to this project.
Add Sherlock actionbar library



Figure 5 : Add Sherlock actionbar library

8. Add menu icons to this project

From the given below links, download the files drawable-mdpi.zip, drawable-hdpi.zip and drawable-xhdpi.zip and extract to the folders drawable-mdpi, drawable-hdpi and drawable-xhdpi respectively


Download drawable-mdpi.zip
Download drawable-hdpi.zip
Download drawable-xhdpi.zip


9. Update res/values/strings.xml 

<resources>
    <string name="app_name">ActionBarSherlockMenu</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>
    <string name="phone">Phone</string>
    <string name="computer">Computer</string>
    <string name="gamepad">Gamepad</string>
    <string name="camera">Camera</string>
    <string name="video">Video</string>
    <string name="email">EMail</string>
</resources>

10. Update the menu file res/menu/activity_main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/Theme.Sherlock"
    >
    <item
        android:id="@+id/phone"
        android:title="@string/phone"
        android:icon="@drawable/phone"
        android:showAsAction="ifRoom|withText"
        style="@style/Theme.Sherlock"
    />

    <item
        android:id="@+id/computer"
        android:title="@string/computer"
        android:icon="@drawable/computer"
        android:showAsAction="ifRoom|withText"
        style="@style/Theme.Sherlock"
    />

    <item
        android:id="@+id/gamepad"
        android:title="@string/gamepad"
        android:icon="@drawable/gamepad"
        android:showAsAction="ifRoom|withText"
        style="@style/Theme.Sherlock"

    />

    <item
        android:id="@+id/camera"
        android:title="@string/camera"
        android:icon="@drawable/camera"
        android:showAsAction="ifRoom|withText"
    />

    <item
        android:id="@+id/video"
        android:title="@string/video"
        android:icon="@drawable/video"
        android:showAsAction="ifRoom|withText"
    />

    <item
        android:id="@+id/email"
        android:title="@string/email"
        android:icon="@drawable/email"
        android:showAsAction="ifRoom|withText"
    />
</menu>

11. Update the file src/in/wptrafficanalyzer/actionbarsherlockmenu/MainActivity.java

package in.wptrafficanalyzer.actionbarsherlockmenu;

import android.os.Bundle;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;

public class MainActivity extends SherlockActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
        getSupportMenuInflater().inflate(R.menu.activity_main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);

        switch(item.getItemId()){
            case R.id.phone:
                Toast.makeText(getBaseContext(), "You selected Phone", Toast.LENGTH_SHORT).show();
                break;

            case R.id.computer:
                Toast.makeText(getBaseContext(), "You selected Computer", Toast.LENGTH_SHORT).show();
                break;

            case R.id.gamepad:
                Toast.makeText(getBaseContext(), "You selected Gamepad", Toast.LENGTH_SHORT).show();
                break;

            case R.id.camera:
                Toast.makeText(getBaseContext(), "You selected Camera", Toast.LENGTH_SHORT).show();
                break;

            case R.id.video:
                Toast.makeText(getBaseContext(), "You selected Video", Toast.LENGTH_SHORT).show();
                break;

            case R.id.email:
                Toast.makeText(getBaseContext(), "You selected EMail", Toast.LENGTH_SHORT).show();
                break;

        }
        return true;
    }
}

12. Update AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.wptrafficanalyzer.actionbarsherlockmenu"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >

            <activity
                android:name=".MainActivity"
                android:label="@string/title_activity_main"
                android:theme="@style/Theme.Sherlock.ForceOverflow"
                android:uiOptions="splitActionBarWhenNarrow"
            >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    </application>
</manifest>



Download Source Code

Download Full Source Code

Reference

 http://developer.android.com/guide/index.html


Không có nhận xét nào:

Đăng nhận xét

Học lập trình web căn bản với PHP

Bài 1: Các kiến thức căn bản Part 1:  https://jimmyvan88.blogspot.com/2012/05/can-ban-lap-trinh-web-voi-php-bai-1-cac.html Part 2:  https://...