I have an AppCompatActivity
with fragment
. Inside fragment
I have an ImageButton
. On a click on that button I want to show a dialog
with all the needed Intents
in system. Fragment
relevant code:
avatar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showPhotoUploadDialog();
}
});
private void showPhotoUploadDialog(){
ImagePickUpUtil.openMediaSelector(this.getActivity());
}
Where ImagePickUpUtil is a static class from Felix Garcia Borrego :
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class ImagePickUpUtil {
/**
* Detect the available intent and open a new dialog.
*
* @param context
*/
public static void openMediaSelector(Activity context) {
Intent camIntent = new Intent("android.media.action.IMAGE_CAPTURE");
Intent gallIntent = new Intent(Intent.ACTION_GET_CONTENT);
gallIntent.setType("image/*");
// look for available intents
List<ResolveInfo> info = new ArrayList<>();
List<Intent> yourIntentsList = new ArrayList<>();
PackageManager packageManager = context.getPackageManager();
List<ResolveInfo> listCam = packageManager.queryIntentActivities(camIntent, 0);
for (ResolveInfo res : listCam) {
final Intent finalIntent = new Intent(camIntent);
finalIntent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
yourIntentsList.add(finalIntent);
info.add(res);
}
List<ResolveInfo> listGall = packageManager.queryIntentActivities(gallIntent, 0);
for (ResolveInfo res : listGall) {
final Intent finalIntent = new Intent(gallIntent);
finalIntent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
yourIntentsList.add(finalIntent);
info.add(res);
}
// show available intents
openDialog(context, yourIntentsList, info);
}
/**
* Open a new dialog with the detected items.
*/
private static void openDialog(final Activity context, final List<Intent> intents, List<ResolveInfo> activitiesInfo) {
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setTitle(context.getString(R.string.choose_action));
ArrayAdapter arrayAdapter = buildAdapter(context, activitiesInfo);
dialog.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
System.out.println("clicked on " + id);
Intent intent = intents.get(id);
context.startActivityForResult(intent, 1);
}
});
dialog.setNeutralButton(context.getResources().getString(R.string.cancel),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog.show();
}
/**
* Build the list of items to show using the intent_listview_row layout.
*
* @param context
* @param activitiesInfo
* @return
*/
private static ArrayAdapter<ResolveInfo> buildAdapter(final Context context, final List<ResolveInfo> activitiesInfo) {
return new ArrayAdapter<ResolveInfo>(context, R.layout.view_contact_details_row, R.id.contact_details_label, activitiesInfo) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
ResolveInfo res = activitiesInfo.get(position);
TextView textview = (TextView) view.findViewById(R.id.contact_details_label);
textview.setText(res.loadLabel(context.getPackageManager()).toString());
return view;
}
};
}
}
Everything works fine until I try to click on a ListItem
in Dialog
, none of the list items clicks never triggers, although NegativeButton
is working fine. I suggest it has something to do with context
passed to the static class, but I can't resolve this by myself half a day. Thank you.
found a solution here . It was problem with the layouts. Adding android:descendantFocusability="blocksDescendants"
solved the issue.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.