ZOFTINO.COM android and web dev tutorials

Android Action Provider Examples

The purpose of ActionProvider is to provide action on app bar. ActionProvider can be a button or a menu item. When ActionProvider button or menu item is clicked, ActionProvider provides further view and behavior.

ActionProviders are similar to ActionViews. Both of them add multipurpose functionality to app bar with differences in how behavior and layout is controlled. ActionView provides functionality within the app bar meaning action views are displayed within app bar for example SearchView, when clicked, displays search input box within app bar, whereas ActionProvider views are not restricted to app bar.

You can create your own ActionProvider with the behavior you want to have by extending ActionProvider class and implementing onCreateActionView method.

Android support library provides MediaRouteActionProvider and ShareActionProvider action providers. MediaRouteActionProvider is used for example to display audio play back buttons on the action bar. ShareActionProvider can be used to share your application content.

ShareActionProvider example

ShareActionProvider can be placed on app bar to provide share action in your app. To provide share functionality using ShareActionProvider, first you need to create menu item with actionProviderClass attribute of item set to ShareActionProvider. Then you need to override onCreateOptionsMenu method to inflate menu, create SEND intents with the data you want to share and add the intent to ShareActionProvider. Below is the code.

android share action provider example

Activity

 package com.zoftino.actionview;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.ShareActionProvider;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class ShareActionProviderActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share_action_provider);

        Toolbar myToolbar = (Toolbar) findViewById(R.id.sp_toolbar);
        setSupportActionBar(myToolbar);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_share_action_provider, menu);

        MenuItem shareItem = menu.findItem(R.id.action_share);
        ShareActionProvider myShareActionProvider =
                (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem);

        Intent txtIntent = new Intent(Intent.ACTION_SEND);
        txtIntent.setType("text/plain");
        txtIntent.putExtra(Intent.EXTRA_EMAIL, "support@zoftino.com");

        myShareActionProvider.setShareIntent(txtIntent);

        Intent imgIntent = new Intent(Intent.ACTION_SEND);
        imgIntent.setType("image/*");
        imgIntent.putExtra(Intent.EXTRA_STREAM, "imageUri");

        myShareActionProvider.setShareIntent(imgIntent);

        return true;
    }
}  

menu_share_action_provider.xml

 <!--?xml version="1.0" encoding="utf-8"?-->
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/action_share"
        android:title="@string/action_share"
        app:showAsAction="never"
        app:actionProviderClass="android.support.v7.widget.ShareActionProvider"></item>
</menu>
 

activity_share_action_provider.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_share"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.zoftino.actionview.MainActivity">
    <android.support.v7.widget.Toolbar android:id="@+id/sp_toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" android:elevation="4dp"></android.support.v7.widget.Toolbar>

</RelativeLayout>

Custom action provider example

Custom action provider can be created by extending ActionProvider class and implementing call back methods such as onCreateActionView and onPerformDefaultAction.

Below example shows how to create custom ActionProvider which displays a list of strings on clicking it. This custom ActionProvider can be used to display list of latest top items in your app. For example, the custom ActionProvider can be used to show top five latest coupons in coupons app.

The custom ActionProvider overrides onCreateActionView method to inflate layout which has one image button and attaches onclick event listener to the image button. On clicking the image button, a pop list, displaying items passed to the custom action provider, will be shown.

android custom action provider example

Custom action provider

package com.zoftino.actionview;


import android.content.Context;
import android.content.DialogInterface;
import android.support.v4.view.ActionProvider;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

public class TopListActionProvider extends ActionProvider {

    private final Context mContext;
    private List<String> topList = new ArrayList<String>();
    private String listTitle;

    public TopListActionProvider(Context context) {
        super(context);
        mContext = context;
    }

    @Override
    public View onCreateActionView() {
        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        View providerView =
                layoutInflater.inflate(R.layout.top_list_provider, null);

        ViewGroup providerViewGrp = (ViewGroup) providerView;
        View imgButton = providerViewGrp.getChildAt(0);

        imgButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                builder.setTitle(listTitle);
                builder.setItems(getTopListStringArray(), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int item) {
                        dialog.dismiss();
                    }
                });
                builder.create().show();
            }
        });

        return providerView;
    }

    public String[] getTopListStringArray() {
        return topList.toArray(new String[topList.size()]);
    }

    public void setTopList(List<String> topList) {
        this.topList = topList;
    }

    public Context getmContext() {
        return mContext;
    }

    public void setListTitle(String listTitle) {
        this.listTitle = listTitle;
    }
}

to_list_provider.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp"
        android:background="@color/colorPrimary"
        app:srcCompat="@drawable/comment"
        android:id="@+id/tplst"></ImageButton>
</LinearLayout>

Activity which uses above custom activity provider

package com.zoftino.actionview;


import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class CustomActionProviderActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_action_provider);

        Toolbar myToolbar = (Toolbar) findViewById(R.id.sp_toolbar);
        setSupportActionBar(myToolbar);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.custom_menu, menu);

        MenuItem topListItem = menu.findItem(R.id.action_toplist);
        TopListActionProvider topLstActProvider =
                (TopListActionProvider) MenuItemCompat.getActionProvider(topListItem);

        topLstActProvider.setListTitle("Top Five Latest Coupons");
        topLstActProvider.setTopList(CouponsData.arrayOfCoupons);

        return true;
    }
}

Menu

<!--?xml version="1.0" encoding="utf-8"?-->
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/action_toplist"
        android:title="@string/action_share"
        app:showAsAction="always"
        app:actionProviderClass="com.zoftino.actionview.TopListActionProvider" />
</menu>