Reputation: 3501
I want to create and dialog, which appears and a person can choose one of eight options. So i found, that the best will be class 'Alert Dialog'. I know, how to create an object with this class, which displays a list of text options to choose from. But I decided, to now show text option, but images, so I created something like this:
AlertDialog.Builder builder = new AlertDialog.Builder(StartGameActivity.this);
builder.setTitle(R.string.pickColor);
builder.setItems(R.array.colorArray, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
it's not important what is inside this function
}
}
But when I run my application and the alert dialog appears - i see only text, which i think is the path to the folder where is my image (it's something like: res/drawable-xhpi/image_name.jpeg)
I was looking for a solution to this problem, but, this which I found were useless. I have read, that I should maybe use an ListAdapter or LayoutInflater, but I not sure how I can use this. Can anybody explain me how make a list of images inside the AletDialog?
Upvotes: 1
Views: 1211
Reputation: 6438
Assuming your array is a list of resource ids...
AlertDialog.Builder builder = new AlertDialog.Builder(StartGameActivity.this);
builder.setTitle(R.string.pickColor);
ListView lv = new ListView(getActivity());
lv.setAdapter(new ArrayAdapter<String>(getActivity(), -1) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Resources res = getResources();
TypedArray colorArray = res.obtainTypedArray(R.array.colorArray);
Drawable drawable = colorArray.getDrawable(position);
ImageView v = new ImageView(getActivity());
v.setImageDrawable(drawable);
return v;
}
@Override
public int getCount() {
Resources res = getResources();
TypedArray colorArray = res.obtainTypedArray(R.array.colorArray);
return colorArray.length();
}
});
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// do click stuff
}
});
builder.setView(lv);
Upvotes: 1