Skip to content

Instantly share code, notes, and snippets.

@vvakame
Created December 11, 2011 13:18
Show Gist options
  • Save vvakame/1460581 to your computer and use it in GitHub Desktop.
Save vvakame/1460581 to your computer and use it in GitHub Desktop.
Android ActionProvider sample.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/pick_action_provider"
android:showAsAction="always" android:title="@string/pick_action"
android:actionProviderClass="net.vvakame.sapmle.AppPickActionProvider" />
</menu>
package net.vvakame.sapmle;
import java.util.List;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.util.Log;
import android.view.ActionProvider;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.SubMenu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class AppPickActionProvider extends ActionProvider implements
OnMenuItemClickListener {
static final int LIST_LENGTH = 3;
Context mContext;
public AppPickActionProvider(Context context) {
super(context);
mContext = context;
}
@Override
public View onCreateActionView() {
Log.d(this.getClass().getSimpleName(), "onCreateActionView");
TextView textView = new TextView(mContext);
textView.setText("Pick");
return null; // null を返してもいい
}
@Override
public boolean onPerformDefaultAction() {
Log.d(this.getClass().getSimpleName(), "onPerformDefaultAction");
return super.onPerformDefaultAction();
}
@Override
public boolean hasSubMenu() {
Log.d(this.getClass().getSimpleName(), "hasSubMenu");
return true;
}
@Override
public void onPrepareSubMenu(SubMenu subMenu) {
Log.d(this.getClass().getSimpleName(), "onPrepareSubMenu");
subMenu.clear();
PackageManager manager = mContext.getPackageManager();
List<ApplicationInfo> applicationList = manager
.getInstalledApplications(PackageManager.GET_ACTIVITIES);
for (int i = 0; i < Math.min(LIST_LENGTH, applicationList.size()); i++) {
ApplicationInfo appInfo = applicationList.get(i);
subMenu.add(0, i, i, manager.getApplicationLabel(appInfo))
.setIcon(appInfo.loadIcon(manager))
.setOnMenuItemClickListener(this);
}
if (LIST_LENGTH < applicationList.size()) {
subMenu = subMenu.addSubMenu(Menu.NONE, LIST_LENGTH, LIST_LENGTH,
"hoge");
for (int i = 0; i < applicationList.size(); i++) {
ApplicationInfo appInfo = applicationList.get(i);
subMenu.add(0, i, i, manager.getApplicationLabel(appInfo))
.setIcon(appInfo.loadIcon(manager))
.setOnMenuItemClickListener(this);
}
}
}
@Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(mContext, item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
}
package net.vvakame.sapmle;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
{
MenuInflater inflater = new MenuInflater(this);
inflater.inflate(R.menu.app_pick_action, menu);
}
return true;
}
}
@benhirashima
Copy link

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment