これは私が少し前にした共有のオプションの一例です
アップデート:
public class CustomShareDialogActivity extends Activity {
private ArrayList< AppToSendOption > appsOptions = new ArrayList< AppToSendOption >();
@Override
protected void onCreate( Bundle arg0 ) {
super.onCreate( arg0 );
setContentView( R.layout.show_share_dialog );
final Button button = (Button)findViewById( R.id.button1 );
button.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick( View v ) {
getListOfShareApps();
showShareDialog();
}
} );
}
private void getListOfShareApps() {
if( !appsOptions.isEmpty() ){ return; }
Intent sendOption = new Intent();
sendOption.setType( "application/*" );
sendOption.setAction( Intent.ACTION_SEND_MULTIPLE );
List< ResolveInfo > ris = getPackageManager().queryIntentActivities( sendOption, 0 );
for ( ResolveInfo ri : ris ) {
Drawable icon = ri.loadIcon( getPackageManager() );
String appname = ( String ) ri.loadLabel( getPackageManager() );
String packagename = ri.activityInfo.packageName;
String classname = ri.activityInfo.name;
appsOptions.add( new AppToSendOption( icon, appname, packagename, classname ) );
}
}
private void showShareDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder( this );
ArrayAdapter< AppToSendOption > adapter01 = new SendOptionsAdapter( this, appsOptions );
builder.setTitle( "Options" )
.setSingleChoiceItems( adapter01, -1, new OnClickListener() {
@Override
public void onClick( DialogInterface dialog, int which ) {
AppToSendOption app = appsOptions.get( which );
String packagename = app.getPackagename();
String classname = app.getClassname();
// Right here, check the package name to see which app is selected, and do the appropriate
// action.
Toast.makeText( getApplicationContext(), packagename + ", " + classname, Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
} ).setNegativeButton( "Cancel", null ).show();
}
private class AppToSendOption {
Drawable icon;
String appname;
String packagename;
String classname;
public AppToSendOption( Drawable icon, String appname, String packagename, String classname ) {
this.icon = icon;
this.appname = appname;
this.packagename = packagename;
this.classname = classname;
}
Drawable getIcon() {
return icon;
}
String getAppname() {
return appname;
}
String getPackagename() {
return packagename;
}
String getClassname() {
return classname;
}
}
public class SendOptionsAdapter extends ArrayAdapter< AppToSendOption > {
private List< AppToSendOption > apps;
private LayoutInflater inflater;
private static final int RESOURCE = R.layout.send_option_dialog;
class ViewHolder {
TextView text;
ImageView icon;
}
public SendOptionsAdapter( Context context, List< AppToSendOption > objects ) {
super( context, RESOURCE, objects );
inflater = LayoutInflater.from( context );
apps = objects;
}
@Override
public View getView( int position, View convertView, ViewGroup parent ) {
ViewHolder holder;
if ( convertView == null ) {
holder = new ViewHolder();
convertView = inflater.inflate( RESOURCE, null );
holder.text = ( TextView ) convertView.findViewById( R.id.textView_appname );
holder.text.setTextColor( Color.BLACK );
holder.icon = ( ImageView ) convertView.findViewById( R.id.imageView_appicon );
holder.icon.setAdjustViewBounds( true );
holder.icon.setScaleType( ScaleType.CENTER_INSIDE );
convertView.setTag( holder );
} else {
holder = ( ViewHolder ) convertView.getTag();
}
holder.icon.setImageDrawable( apps.get( position ).getIcon() );
holder.text.setText( apps.get( position ).getAppname() );
return convertView;
}
}
}
そして、これがsend_option_dialogのxmlファイルです。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/imageView_appicon"
android:layout_width="42dp"
android:layout_height="42dp"
android:layout_gravity="center"
android:layout_marginBottom="15dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="5dp"
android:layout_marginTop="15dp" >
</ImageView>
<TextView
android:id="@+id/textView_appname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="2dp"
android:lines="1"
android:textColor="#fff"
android:textSize="20sp" >
</TextView>
完璧ではありません。代わりにDialogFragmentを使用することを検討してください。ただし、これにより、ダイアログの作成方法がわかるようになることを願っています。