Download eclipse project here : ActionBarCompatBasic.zip
After importing or creating project you need to add support_v7 library.
strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ActionBarCompat Basic</string>
<string name="hello_world">Hello world!</string>
<string name="menu_refresh">Refresh</string>
<string name="menu_location">Location</string>
<string name="menu_settings">Settings</string>
<string name="intro_message">
<![CDATA[
This sample shows you how to use ActionBarCompat to create a basic Activity which
displays action items. It covers inflating items from a menu resource, as well as adding
an item in code. Items that are not shown as action items on the Action Bar are
displayed in the action bar overflow.
]]>
</string>
</resources>
<resources>
<string name="app_name">ActionBarCompat Basic</string>
<string name="hello_world">Hello world!</string>
<string name="menu_refresh">Refresh</string>
<string name="menu_location">Location</string>
<string name="menu_settings">Settings</string>
<string name="intro_message">
<![CDATA[
This sample shows you how to use ActionBarCompat to create a basic Activity which
displays action items. It covers inflating items from a menu resource, as well as adding
an item in code. Items that are not shown as action items on the Action Bar are
displayed in the action bar overflow.
]]>
</string>
</resources>
Create ids.xml under values folder and include following code:
ids.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Generate an id which can be used when the location menu item is added in MainActivity -->
<item name="menu_location" type="id"/>
</resources>
<resources>
<!-- Generate an id which can be used when the location menu item is added in MainActivity -->
<item name="menu_location" type="id"/>
</resources>
Styles.xml:
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
</resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
</resources>
template-dimens.xml:
<resources>
<!-- Define standard dimensions to comply with Holo-style grids and rhythm. -->
<dimen name="margin_tiny">4dp</dimen>
<dimen name="margin_small">8dp</dimen>
<dimen name="margin_medium">16dp</dimen>
<dimen name="margin_large">32dp</dimen>
<dimen name="margin_huge">64dp</dimen>
<!-- Semantic definitions -->
<dimen name="horizontal_page_margin">@dimen/margin_medium</dimen>
<dimen name="vertical_page_margin">@dimen/margin_medium</dimen>
</resources>
<!-- Define standard dimensions to comply with Holo-style grids and rhythm. -->
<dimen name="margin_tiny">4dp</dimen>
<dimen name="margin_small">8dp</dimen>
<dimen name="margin_medium">16dp</dimen>
<dimen name="margin_large">32dp</dimen>
<dimen name="margin_huge">64dp</dimen>
<!-- Semantic definitions -->
<dimen name="horizontal_page_margin">@dimen/margin_medium</dimen>
<dimen name="vertical_page_margin">@dimen/margin_medium</dimen>
</resources>
template-styles.xml:
<resources>
<!-- Activity themes -->
<style name="Theme.Base" parent="android:Theme.Light" />
<style name="Theme.Sample" parent="Theme.Base" />
<style name="AppTheme" parent="Theme.Sample" />
<!-- Widget styling -->
<style name="Widget" />
<style name="Widget.SampleMessage">
<item name="android:textAppearance">?android:textAppearanceMedium</item>
<item name="android:lineSpacingMultiplier">1.1</item>
</style>
<style name="Widget.SampleMessageTile">
<item name="android:background">@drawable/tile</item>
<item name="android:shadowColor">#7F000000</item>
<item name="android:shadowDy">-3.5</item>
<item name="android:shadowRadius">2</item>
</style>
</resources>
<!-- Activity themes -->
<style name="Theme.Base" parent="android:Theme.Light" />
<style name="Theme.Sample" parent="Theme.Base" />
<style name="AppTheme" parent="Theme.Sample" />
<!-- Widget styling -->
<style name="Widget" />
<style name="Widget.SampleMessage">
<item name="android:textAppearance">?android:textAppearanceMedium</item>
<item name="android:lineSpacingMultiplier">1.1</item>
</style>
<style name="Widget.SampleMessageTile">
<item name="android:background">@drawable/tile</item>
<item name="android:shadowColor">#7F000000</item>
<item name="android:shadowDy">-3.5</item>
<item name="android:shadowRadius">2</item>
</style>
</resources>
Create menu folder under res folder and create main.xml under menu folder and include following code:
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<!--
As we're using ActionBarCompat, any action item attributes come from ActionBarCompat's XML
namespace instead of the android namespace. Here we've added a new support namespace added to
the menu element allowing us to use the 'showAsAction' attribute in a backwards compatible way.
Any other action item attributes used should be referenced from this namespace too
(actionProviderClass, actionViewClass, actionLayout).
-->
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:support="http://schemas.android.com/apk/res-auto" >
<!--
Here we create an item, setting support:showAsAction to display the item as an action if
there's room on the compatible Action Bar.
-->
<item
android:id="@+id/menu_refresh"
android:icon="@drawable/ic_action_refresh"
android:title="@string/menu_refresh"
support:showAsAction="ifRoom"/>
<!-- Location item is added in onCreateOptionsMenu() -->
<!--
Here we set the settings item to always be in the overflow menu, by setting
support:showAsAction to never, so it is never displayed as an action item on the compatible
Action Bar.
-->
<item
android:id="@+id/menu_settings"
android:icon="@drawable/ic_action_settings"
android:title="@string/menu_settings"
support:showAsAction="never"/>
</menu>
<!--
As we're using ActionBarCompat, any action item attributes come from ActionBarCompat's XML
namespace instead of the android namespace. Here we've added a new support namespace added to
the menu element allowing us to use the 'showAsAction' attribute in a backwards compatible way.
Any other action item attributes used should be referenced from this namespace too
(actionProviderClass, actionViewClass, actionLayout).
-->
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:support="http://schemas.android.com/apk/res-auto" >
<!--
Here we create an item, setting support:showAsAction to display the item as an action if
there's room on the compatible Action Bar.
-->
<item
android:id="@+id/menu_refresh"
android:icon="@drawable/ic_action_refresh"
android:title="@string/menu_refresh"
support:showAsAction="ifRoom"/>
<!-- Location item is added in onCreateOptionsMenu() -->
<!--
Here we set the settings item to always be in the overflow menu, by setting
support:showAsAction to never, so it is never displayed as an action item on the compatible
Action Bar.
-->
<item
android:id="@+id/menu_settings"
android:icon="@drawable/ic_action_settings"
android:title="@string/menu_settings"
support:showAsAction="never"/>
</menu>
layout folder files:
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
style="@style/Widget.SampleMessageTile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
style="@style/Widget.SampleMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/vertical_page_margin"
android:layout_marginLeft="@dimen/horizontal_page_margin"
android:layout_marginRight="@dimen/horizontal_page_margin"
android:layout_marginTop="@dimen/vertical_page_margin"
android:text="@string/intro_message" />
</LinearLayout>
</LinearLayout>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
style="@style/Widget.SampleMessageTile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
style="@style/Widget.SampleMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/vertical_page_margin"
android:layout_marginLeft="@dimen/horizontal_page_margin"
android:layout_marginRight="@dimen/horizontal_page_margin"
android:layout_marginTop="@dimen/vertical_page_margin"
android:text="@string/intro_message" />
</LinearLayout>
</LinearLayout>
sample_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="16dp"
android:text="@string/intro_message" />
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="16dp"
android:text="@string/intro_message" />
MainActivity.java:
package com.ram.actionbarcompatbasic;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
/**
* This sample shows you how to use ActionBarCompat to create a basic Activity
* which displays action items. It covers inflating items from a menu resource,
* as well as adding an item in code.
*
* This Activity extends from {@link ActionBarActivity}, which provides all of
* the function necessary to display a compatible Action Bar on devices running
* Android v2.1+.
*/
public class MainActivity extends ActionBarActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.sample_main);
}
// BEGIN_INCLUDE(create_menu)
/**
* Use this method to instantiate your menu, and add your items to it. You
* should return true if you have added items to it and want the menu to be
* displayed.
*/
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate our menu from the resources by using the menu inflater.
getMenuInflater().inflate(R.menu.main, menu);
// It is also possible add items here. Use a generated id from
// resources (ids.xml) to ensure that all menu ids are distinct.
MenuItem locationItem = menu
.add(0, R.id.menu_location, 0, R.string.menu_location);
locationItem.setIcon(R.drawable.ic_action_location);
// Need to use MenuItemCompat methods to call any action item related
// methods
MenuItemCompat.setShowAsAction(locationItem, MenuItem.SHOW_AS_ACTION_IF_ROOM);
return true;
}
// END_INCLUDE(create_menu)
// BEGIN_INCLUDE(menu_item_selected)
/**
* This method is called when one of the menu items to selected. These items
* can be on the Action Bar, the overflow menu, or the standard options
* menu. You should return true if you handle the selection.
*/
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_refresh:
// Here we might start a background refresh task
return true;
case R.id.menu_location:
// Here we might call LocationManager.requestLocationUpdates()
return true;
case R.id.menu_settings:
// Here we would open up our settings activity
return true;
}
return super.onOptionsItemSelected(item);
}
// END_INCLUDE(menu_item_selected)
}
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
/**
* This sample shows you how to use ActionBarCompat to create a basic Activity
* which displays action items. It covers inflating items from a menu resource,
* as well as adding an item in code.
*
* This Activity extends from {@link ActionBarActivity}, which provides all of
* the function necessary to display a compatible Action Bar on devices running
* Android v2.1+.
*/
public class MainActivity extends ActionBarActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.sample_main);
}
// BEGIN_INCLUDE(create_menu)
/**
* Use this method to instantiate your menu, and add your items to it. You
* should return true if you have added items to it and want the menu to be
* displayed.
*/
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate our menu from the resources by using the menu inflater.
getMenuInflater().inflate(R.menu.main, menu);
// It is also possible add items here. Use a generated id from
// resources (ids.xml) to ensure that all menu ids are distinct.
MenuItem locationItem = menu
.add(0, R.id.menu_location, 0, R.string.menu_location);
locationItem.setIcon(R.drawable.ic_action_location);
// Need to use MenuItemCompat methods to call any action item related
// methods
MenuItemCompat.setShowAsAction(locationItem, MenuItem.SHOW_AS_ACTION_IF_ROOM);
return true;
}
// END_INCLUDE(create_menu)
// BEGIN_INCLUDE(menu_item_selected)
/**
* This method is called when one of the menu items to selected. These items
* can be on the Action Bar, the overflow menu, or the standard options
* menu. You should return true if you handle the selection.
*/
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_refresh:
// Here we might start a background refresh task
return true;
case R.id.menu_location:
// Here we might call LocationManager.requestLocationUpdates()
return true;
case R.id.menu_settings:
// Here we would open up our settings activity
return true;
}
return super.onOptionsItemSelected(item);
}
// END_INCLUDE(menu_item_selected)
}
0 comments:
Post a Comment