anand
anand

Reputation: 1751

Custom Layout AlertDialog not working?

I am working on a AlertDialog which should display a list of size 2 with image and text. But i am unable to get it working.

I followed this link
Icons in a List dialog
When I am putting getActivity() it's not recognizing so on the suggestion from the post i put getApplicationContext();

public MainActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_complaint);
        cameraButton = (ImageView) findViewById(R.id.cmp_camera);
        cameraButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                final String[] items = new String[]{"From Gallery", "From Camera"};
                final Integer[] icons = new Integer[]{R.drawable.ic_launcher, R.drawable.ic_drawer};
                ListAdapter adapter = new CameraPickAdapter(getApplicationContext(), items, icons);

                new AlertDialog.Builder(getApplicationContext()).setTitle("Select Image")
                        .setAdapter(adapter, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int item) {
                                Toast.makeText(getApplicationContext(), "Item Selected: " + item, Toast.LENGTH_SHORT).show();
                            }
                        }).show();
            }
        });
    }
}

the code below is the Adapter class

CameraPickAdapter

public class CameraPickAdapter extends ArrayAdapter<String> {

    private List<Integer> images;

    public CameraPickAdapter(Context context, List<String> items, List<Integer> images) {
        super(context, android.R.layout.select_dialog_item, items);
        this.images = images;
    }

    public CameraPickAdapter(Context context, String[] items, Integer[] images) {
        super(context, android.R.layout.select_dialog_item, items);
        this.images = Arrays.asList(images);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        TextView textView = (TextView) view.findViewById(android.R.id.text1);
        textView.setCompoundDrawablesWithIntrinsicBounds(images.get(position), 0, 0, 0);
        textView.setCompoundDrawablePadding(
                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 12, getContext().getResources().getDisplayMetrics()));
        return view;
    }
}

I dont know why I am not getting. I thought this would be simple but i am finding it a hard nut to crack

Upvotes: 0

Views: 507

Answers (3)

Giru Bhai
Giru Bhai

Reputation: 14398

Change getApplicationContext() to MainActivity.this i.e. Change

  new AlertDialog.Builder(getApplicationContext()).setTitle("Select Image")
                        .setAdapter(adapter, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int item) {
                                Toast.makeText(getApplicationContext(), "Item Selected: " + item, Toast.LENGTH_SHORT).show();
                            }
                        }).show();

to

new AlertDialog.Builder(MainActivity.this).setTitle("Select Image")
                    .setAdapter(adapter, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int item) {
                            Toast.makeText(MainActivity.this, "Item Selected: " + item, Toast.LENGTH_SHORT).show();
                        }
                    }).show();

Upvotes: 2

T_V
T_V

Reputation: 17580

Do like this-

new AlertDialog.Builder(MainActivity.this)

It might work for you.

Upvotes: 1

Omar HossamEldin
Omar HossamEldin

Reputation: 3111

I had the same problem before trying to apply custom layout on AlertDialog and when i changed it to Dialog it worked great Please have a look here at my Question

Upvotes: 0

Related Questions