Adding ActionProvider demo.

Change-Id: If0f540cd796ced27f392f1c31d20ab5ab34e5c3e
This commit is contained in:
Svetoslav Ganov
2011-07-14 16:25:57 -07:00
parent 7a88cc163b
commit ddece2d84d
5 changed files with 123 additions and 0 deletions

View File

@@ -799,6 +799,15 @@
</intent-filter>
</activity>
<activity android:name=".app.ActionBarActionProviderActivity"
android:label="@string/action_bar_action_provider"
android:enabled="@bool/atLeastIceCreamSandwich">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter>
</activity>
<!-- Application Updating Samples -->
<!-- BEGIN_INCLUDE(app_update_declaration) -->

View File

@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2011 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_item_share_action_provider_action_bar"
android:showAsAction="always"
android:title="@string/action_bar_share_with"
android:actionProviderClass="android.widget.ShareActionProvider" />
<item android:id="@+id/menu_item_share_action_provider_overflow"
android:showAsAction="never"
android:title="@string/action_bar_share_with"
android:actionProviderClass="android.widget.ShareActionProvider" />
</menu>

View File

@@ -23,4 +23,10 @@
API level. The default value is false; an alternative value
for Honeycomb MR2 is true. -->
<bool name="atLeastHoneycombMR2">false</bool>
<!-- This resource is true if running under at least IceCreamSandwich
API level. The default value is false; an alternative value
for IceCreamSandwich is true. -->
<bool name="atLeastIceCreamSandwich">true</bool>
</resources>

View File

@@ -722,6 +722,7 @@
<string name="action_bar_mechanics">App/Action Bar/Action Bar Mechanics</string>
<string name="action_bar_usage">App/Action Bar/Action Bar Usage</string>
<string name="action_bar_tabs">App/Action Bar/Action Bar Tabs</string>
<string name="action_bar_action_provider">App/Action Bar/Action Provider</string>
<string name="action_bar_search">Search</string>
<string name="action_bar_add">Add</string>
@@ -730,6 +731,7 @@
<string name="action_bar_sort">Sort</string>
<string name="action_bar_sort_alpha">Alphabetically</string>
<string name="action_bar_sort_size">By size</string>
<string name="action_bar_share_with">Share with...</string>
<string name="action_bar_display_options">App/Action Bar/Display Options</string>
<string name="toggle_home_as_up">DISPLAY_HOME_AS_UP</string>

View File

@@ -0,0 +1,78 @@
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.apis.app;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ShareActionProvider;
import java.io.File;
import com.example.android.apis.R;
/**
* This activity demonstrates how to use an {@link android.view.ActionProvider}
* for adding functionality to the Action Bar. In particular this demo is adding
* a menu item with ShareActionProvider as its action provider. The
* ShareActionProvider is responsible for managing the UI for sharing actions.
*/
public class ActionBarActionProviderActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate your menu.
getMenuInflater().inflate(R.menu.action_bar_action_provider, menu);
// Set file with share history to the provider and set the share intent.
MenuItem actionItem = menu.findItem(R.id.menu_item_share_action_provider_action_bar);
ShareActionProvider actionProvider = (ShareActionProvider) actionItem.getActionProvider();
actionProvider.setShareHistoryFileName(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
actionProvider.setShareIntent(createShareIntent());
// Set file with share history to the provider and set the share intent.
MenuItem overflowItem = menu.findItem(R.id.menu_item_share_action_provider_overflow);
ShareActionProvider overflowProvider =
(ShareActionProvider) overflowItem.getActionProvider();
overflowProvider.setShareHistoryFileName(
ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
overflowProvider.setShareIntent(createShareIntent());
return true;
}
/**
* Creates a sharing {@link Intent}.
*
* @return The sharing intent.
*/
private Intent createShareIntent() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
Uri uri = Uri.fromFile(new File(getFilesDir(), "SomeFileToShare"));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri.toString());
return shareIntent;
}
}