How To Customize The Color Of The Checkmark Color In Android In A Dialog. : Android
Solution 1:
If you look at the styles.xml from android system, you will see that the checkbox style is defined as follows :
<stylename="Widget.CompoundButton.CheckBox"><itemname="android:background">@android:drawable/btn_check_label_background</item><itemname="android:button">@android:drawable/btn_check</item></style>
And If you search the resources of the system, you will see that btn_check is a drawable selector with 2 states (on/off) with the check colored green or not. So if you want to have your own color-drawable, here is what you should do : - create a styles.xml - define the 2 drawables to use - create the xml file supporting the selector
You can find the full documentation quite detailled on the android google doc.
Solution 2:
To change the checkbox inside a multi-choice dialog, you need a custom adapter for your dialog, so as to have access to the views of the list.
Then, you call method setCheckMarkDrawable
of class CheckedTextView.
Here is an example:
File default_checkbox.xml
inside res/drawable
<?xml version="1.0" encoding="utf-8"?><selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:state_checked="true"android:drawable="@drawable/checkbox_checked" /><!-- checked --><itemandroid:state_pressed="true"android:drawable="@drawable/checkbox_checked" /><!-- pressed --><itemandroid:drawable="@drawable/checkbox_default" /><!-- default --></selector>
File DialogUtil.java
package example.dialog;
import android.app.AlertDialog;
import android.content.Context;
import android.util.Log;
import android.view.*;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;
publicclassDialogUtil {
privateDialogUtil() {
}
publicstatic AlertDialog show(Context context) {
String[] items = {"text 1", "text 2", "text 3"};
AlertDialog.Builderbuilder=newAlertDialog.Builder(context);
builder.setTitle("Test")
.setPositiveButton("OK", null)
.setAdapter(newCustomAdapter(context, items), null);
AlertDialogdialog= builder.show();
ListViewlist= dialog.getListView();
list.setItemsCanFocus(false);
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
list.setOnItemClickListener(listener);
return dialog;
}
privatestaticOnItemClickListenerlistener=newOnItemClickListener() {
@OverridepublicvoidonItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("DialogUtil", "Clicked on " + view);
}
};
privatestaticclassCustomAdapterextendsArrayAdapter<String> {
publicCustomAdapter(Context context, String[] array) {
super(context, android.R.layout.simple_list_item_multiple_choice, array);
}
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
Viewview=super.getView(position, convertView, parent);
if (view instanceof CheckedTextView) {
CheckedTextViewcheckedView= (CheckedTextView) view;
checkedView.setCheckMarkDrawable(R.drawable.default_checkbox);
}
return view;
}
}
}
NOTE: If you simply use AlertDialog
, then before getting the ListView
, you call show
, firstly, like explained above.
However, if you use DialogFragment
and onCreateDialog
, then you get the ListView
, inside onStart
.
Solution 3:
You have to override checkbox widget styles with modified drawable resources. Here is good guide to start with styles/themes http://brainflush.wordpress.com/2009/03/15/understanding-android-themes-and-styles/
Post a Comment for "How To Customize The Color Of The Checkmark Color In Android In A Dialog. : Android"