Two ways to use an action provider:
- Set the action provider on a MenuItem directly by calling setActionProvider (ActionProvider)
- Declare the action provider in an XML menu resource. e.g.
<item
android:id="@+id/action_search"
android:title="Search"
android:icon="@drawable/search_icon"
android:showasaction="ifRoom"
android:actionproviderclass="com.my.CustomActionProvider" />
Making a custom action provider is straightforward, simply extend ActionProvider class and implement its callback methods as appropriate. It should be implemented like below code example:
<item
android:id="@+id/action_search"
android:title="Search"
android:icon="@drawable/search_icon"
android:showasaction="ifRoom"
android:actionproviderclass="com.my.CustomActionProvider" />
Making a custom action provider is straightforward, simply extend ActionProvider class and implement its callback methods as appropriate. It should be implemented like below code example:
public class MyCustomActionProvider extends ActionProvider { private Context mContext; private EditText mEditText; public MyCustomActionProvider (Context context) { super(context); mContext = context; } @override public View onCreateActionView() { // Make & return View that will be shown when action bar icon pressed // Here we have a custom layout that contains EditText for Search LayoutInflator layout = LayoutInflator.from(mContext); View view = layout.inflate(R.layout.search_layout, null); mEditText = (EditText) view.findViewById(R.id.edit_search); ................... } ................ }
public class MainActivity extends Activity { private MyCustomActionProvider mActionProvider; ........... @override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.items, menu); MenuItem item = menu.findItem(R.id.action_search); mActionProvider = (MyCustomActionProvider) item.getActionProvider(); .............. } ........... }
Android also provides an implementation of ActionProvider for Share actions: ShareActionProvider, which facilitates a share action by showing a list of possible apps for sharing directly in the action bar.
To add a share action with ShareActionProvider, just define the tag for an <item> android:actionproviderclass="android.widget.ShareActionProvider".
The last step left to do is to define the Intent what we want to use for sharing. How we do it? just modify onCreateOptionMenu() method.
// In Activity#onCreateOptionsMenu public boolean onCreateOptionsMenu(Menu menu) { // Get the menu item. MenuItem menuItem = menu.findItem(R.id.my_menu_item); // Get the provider and hold onto it to set/change the share intent. mShareActionProvider = (ShareActionProvider) menuItem.getActionProvider(); // Set history different from the default before getting the action // view since a call to MenuItem.getActionView() calls // onCreateActionView() which uses the backing file name. Omit this // line if using the default share history file is desired. mShareActionProvider.setShareHistoryFileName("custom_share_history.xml"); . . . } // Somewhere in the application. public void doShare(Intent shareIntent) { // When you want to share set the share intent. mShareActionProvider.setShareIntent(shareIntent); }
댓글 없음:
댓글 쓰기