Customize Dialog Which Has Single-choice List Items
Solution 1:
Steps for Creating customize dialog:
Create the dialog box layout files, like:
<RelativeLayoutxmlns:android = "http://schemas.android.com/apk/res/android"android:layout_width = "wrap_content"android:layout_height = "wrap_content"><!-- The Title Bar --><LinearLayoutandroid:id="@+id/dlg_priority_titlebar"android:orientation="horizontal"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_alignParentTop="true"><ImageViewandroid:src="@drawable/image"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="5dip" /><TextViewandroid:layout_width = "wrap_content"android:layout_height = "wrap_content"android:text = "Select Task Priority"android:layout_gravity = "center_vertical" /></LinearLayout><ListViewandroid:id="@+id/dlg_priority_lvw"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/dlg_priority_titlebar"android:background="@drawable/layout_home_bg"></ListView>
Because the layout in the
ListView
custom, so to create a layout file for theListView
:<LinearLayoutxmlns:android = "http://schemas.android.com/apk/res/android"android:orientation = "horizontal"android:layout_width = "fill_parent"android:layout_height = "fill_parent"><ImageViewandroid:id = "@+id/list_priority_img"android:layout_width = "wrap_content"android:layout_height = "wrap_content"android:layout_gravity = "center_vertical"android:layout_margin = "5dip" /><TextViewandroid:id = "@+id/list_priority_value"android:layout_width = "wrap_content"android:layout_height = "wrap_content"android:layout_gravity = "center_vertical"android:textsize = "28dip"android:textColor = "@drawable/ black" /></LinearLayout>
Create a custom
Dialog
classPriorityDlg
inherited fromDialog
publicclassPriorityDlgextendsDialog { privateContext context; privateListView dlg_priority_lvw = null; publicPriorityDlg(Context context) { super(context); this.context = context; // TODO Auto-generated constructor stub } publicPriorityDlg(Context context, int theme) { super(context, theme); this.context = context; } @OverrideprotectedvoidonCreate(Bundle savedInstanceState) { // TODO Auto-generated method stubsuper.onCreate(savedInstanceState); this.setContentView(R.layout.dlg_priority); dlg_priority_lvw = (ListView) findViewById(R.id.dlg_priority_lvw); // ListViewSimpleAdapter adapter = newSimpleAdapter(context, getPriorityList(), R.layout.lvw_priority, newString[] { "list_priority_img", "list_priority_value" }, new int[] { R.id.list_priority_img, R.id.list_priority_value }); dlg_priority_lvw.setAdapter(adapter); //ListView dlg_priority_lvw .setOnItemClickListener(newAdapterView.OnItemClickListener() { @OverridepublicvoidonItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { } }); } privateList<HashMap<String, Object>> getPriorityList() { List<HashMap<String, Object>> priorityList = newArrayList<HashMap<String, Object>>(); HashMap<String, Object> map1 = newHashMap<String, Object>(); map1.put("list_priority_img", R.drawable.priority_not_important); map1.put("list_priority_value", context.getResources().getString( R.string.dlg_priority_not_important)); priorityList.add(map1); HashMap<String, Object> map2 = newHashMap<String, Object>(); map2.put("list_priority_img", R.drawable.priority_general); map2.put("list_priority_value", context.getResources().getString( R.string.dlg_priority_general)); priorityList.add(map2); HashMap<String, Object> map3 = newHashMap<String, Object>(); map3.put("list_priority_img", R.drawable.priority_important); map3.put("list_priority_value", context.getResources().getString( R.string.dlg_priority_important)); priorityList.add(map3); HashMap<String, Object> map4 = newHashMap<String, Object>(); map4.put("list_priority_img", R.drawable.priority_very_important); map4.put("list_priority_value", context.getResources().getString( R.string.dlg_priority_very_important)); priorityList.add(map4); return priorityList; } }
To create a custom dialog box
PriorityDlgdlg=newPriorityDlg (SimpleTaskActivity.this, R.style.dlg_priority); dlg.show();
Where R.style.dlg_priority
set the dialog box uses the style file, just let the dialog box to remove the title bar, and of course you can code to complete this effect:
<? Xml version = "1.0" encoding = "utf-8"?><resources><stylename="dlg_priority"parent="@android:Theme.Dialog"><itemname = "android: windowNoTitle"> true </ item>
</ style>
</ resources>
Solution 2:
You basically will have to create your own ListAdapter
by
subclassing one of the available Adapter classes and supply that to
the dialog (using builder.setAdapter(...)
). If you have an array or
list of items/objects, subclassing ArrayAdapter
is probably what
you'll want to look into.
In your Adapter subclass, you override the getView(...)
method (amongst others) and fill the views of your custom layout with data
for the supplied position in the list. More specifically, you'll want
to set an image against an ImageView
and text to a TextView
.
Createclass`MySimpleArrayAdapter` extending from`ArrayAdapter`.
Using ListView.setOnItemClickListener()
method we get selected value of CheckTextView
#dialog.xml
<?xml version="1.0" encoding="UTF-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:background="@color/whitebox_bg"android:orientation="horizontal" ><CheckedTextViewandroid:id="@+id/textDialog"android:layout_width="fill_parent"android:layout_height="wrap_content"android:checkMark="?android:attr/listChoiceIndicatorSingle"android:gravity="center_vertical"android:maxLines="10"android:singleLine="false"android:text=""android:textAppearance="?android:attr/textAppearanceLarge"android:textColor="#000000" /></LinearLayout>
To create a custom dialog box
String [] view_location = {"Red", "Green", "Blue"};
TextView label = (TextView) findViewById(R.id.selected_dept);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Location");
ListView listview = new ListView(this);
listview.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
listview.setCacheColorHint(0);
listview.setBackgroundColor(Color.WHITE);
if (view_location != null) {
MySimpleArrayAdapter choice_arrayAdapter = new MySimpleArrayAdapter(this, view_location,label.getText().toString());
listview.setAdapter(choice_arrayAdapter);
builder.setView(listview);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
publicvoid onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
TextView label = (TextView) findViewById(R.id.selected_dept);
label.setText(view_location[arg2]);
survedObjectId = view_location_id[arg2];
dInterface.dismiss();
}
});
}
Dialog dialog = builder.create();
dInterface = dialog;
dialog.getWindow().setLayout(200, 400);
dialog.show();'
// `Extends From ArrayAdapter
'publicclassMySimpleArrayAdapterextendsArrayAdapter<String> {
privatefinal Context context;
privatefinalString[] values;
privatefinalString selectedText;
public MySimpleArrayAdapter(Context context, String[] values, String selectedText) {
super(context, R.layout.dialog_text, values);
this.context = context;
this.values = values;
this.selectedText = selectedText;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.dialog_text, parent, false);
CheckedTextView textView = (CheckedTextView) rowView.findViewById(R.id.textDialog);
textView.setText(values[position]);
if(textView.getText().toString().equals(selectedText))
{
textView.setChecked(true);
}
else
{
textView.setChecked(false);
}
return rowView;
}
}'
Solution 3:
You basically will have to create your own ListAdapter by subclassing one of the available Adapter classes and supply that to the dialog (using builder.setAdapter(...)
). If you have an array or list of items/objects, subclassing ArrayAdapter is probably what you'll want to look into.
In your Adapter subclass, you override the getView(...)
method (amongst others) and fill the views of your custom layout with data for the supplied position in the list. More specifically, you'll want to set an image against an ImageView and text to a TextView.
A fairly nice tutorial that demonstrates how to implement a custom ArrayAdapter, and happens to be close to what you're trying to accomplish, can be found here. It also shows how to use the ViewHolder/RowWrapper concept.
Post a Comment for "Customize Dialog Which Has Single-choice List Items"