Spinner Drop-Down Customization - android

I'm using an ActionBar with a spinner (drop-down), but the thing is that the text on the selection area itself is not supposed to be one of the elements in the list. Moreover, I'm styling the elements in the spinner using a custom adapter, which also customizes the selection area.
Probably a screenshot would be best. This is what I want to achieve:
This is what I currently have:
A snippet from my custom adapter:
public class CategoriesDropDownAdapter extends BaseAdapter{
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.categories_drop_down_item, parent, false);
}
Category category = getCategory(position);
ImageView icon = (ImageView) convertView.findViewById(R.id.catIcon);
TextView title = (TextView) convertView.findViewById(R.id.catTitle);
//setting icon, title and background color
icon.setImageDrawable(category.getIcon());
title.setText(category.getTitle());
convertView.setBackgroundColor(category.getBackgroundColor());
//setting the width of the drop-down to take all the layout width
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics metrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(metrics);
convertView.setLayoutParams(new LayoutParams(metrics.widthPixels, 100));
return convertView;
}
}
How can I avoid customizing the selection area (the area on the bar itself) when using a custom adapter? I want that area to be customized differently (without that triangle on the bottom-right, for instance).

You have to apply style for Spinner component in styles.xml
<resources>
<style name="spinner_style">
// Change background
<item name="android:background">#drawable/img</item>
<item name="android:dropDownWidth">fill_parent</item>
<item name="android:showDividers">beginning|middle|end</item>
<item name="android:alignmentMode">alignBounds</item>
<item name="android:dividerHeight">2dp</item>
<item name="android:scrollbars">none</item>
<item name="android:scrollbarAlwaysDrawVerticalTrack">false</item>
<item name="android:scrollbarTrackVertical">#android:color/transparent</item>
<item name="android:dropDownSelector">#android:color/white</item>
<item name="android:requiresFadingEdge">none</item>
</style>
</resources>
In UI Xml file
<Spinner
android:id="#+id/ics_spinner"
style="#style/spinner_style"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="none" />

Related

Views in Custom ListView item don't have apptheme

Problem:
I have a custom adapter for my listview which currently contains a checkbox and an edittext. I haven't changed any themes in the res/values/styles folder. For some reason the checkbox and edittext have a different style/theme than the normal style/theme ("AppTheme").
Automatically generated theme:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
But the checkbox is white, and can only be seen when it is checked. (color is not the colorAccent set above) And the edittext has the same color the checkbox has when it is checked (green/blueish).
colors:
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
What have I tried?
I tried setting the theme/style of the container of the item and the item itself, with the xml attributes android:theme and style. Each to no avail.
Is this normal? If so, how do I change the style/theme of the listitem? If not, what could be wrong?
My listitem.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent" >
<CheckBox
android:id="#+id/cb_listitem"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<EditText
android:id="#+id/editText_listitem"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="9"
android:textColor="#color/black"/>
</LinearLayout>
getView in custom adapter:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.listitem_note_checkbox, null);
holder = new ViewHolder();
holder.checkBox = (CheckBox)convertView.findViewById(R.id.cb_listitem);
holder.editText = (EditText)convertView.findViewById(R.id.editText_listitem);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.editText.setText(items.get(position).note);
return convertView;
}
screenshots:
as we can see the radio buttons have the default layout, accentcolor "pink" and they have a grey outlining. but the checkbox outlining is white, and the accentcolor is blue/greenish.
You need to use the Activity's Context for anything that requires styles and attribute values from its theme. Since your Adapter's context field is initialized in its constructor, you need to instantiate your Adapter with the Activity's Context, not the Application's, so the LayoutInflater ends up with the correct one. For example:
MyAdapter adapter = new MyAdapter(MainActivity.this, list);

Dynamic Spinner with icons inheriting AppCompat theme

I am trying to achieve two things:
Make my Spinners inherit from the AppCompat themes.
Add icons to the spinner elements, as is possible in the Toolbar popup menus.
Since I am not able to achieve the first point I am focusing on that, but I also want to add icons later on. As it is now, my Toolbar popup menus inherit the AppCompat theme, but the Spinners do not, as shown in the pictures below. The first image shows the (proper) popup menu from the Toolbar, while the second shows the popup menu from a Spinner. This is an example of a Spinner that is not inheriting the style. Or should this popup menu style be expected?
I have tried loads of things, so there are likely multiple duplicates of this question, but I am not able to make it work. What can be wrong in the code below? After the theme is inherited correctly, how can I add icons later on? Minimum SDK version is 16, target is 23.
themes.xml:
<resources>
<style name="MyTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="colorPrimary">#color/my_brown</item>
<item name="colorPrimaryDark">#color/my_dark_gray</item>
<item name="colorAccent">#color/my_green</item>
<!-- This is just a test, it makes no difference. -->
<item name="android:spinnerStyle">#style/MySpinnerStyle</item>
</style>
<style name="MyTheme" parent="MyTheme.Base"></style>
<!--
ActionBar style, applied directly to XML elements
-->
<style name="MyActionBarStyle" parent="#style/Widget.AppCompat.ActionBar">
<item name="theme">#style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
<item name="popupTheme">#style/ThemeOverlay.AppCompat.Light</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:background">#color/my_brown</item>
<item name="android:minHeight">?attr/actionBarSize</item>
</style>
<!--
Spinner style, for testing. Also tried applied directly to xml Spinners.
-->
<style name="MySpinnerStyle" parent="#style/Widget.AppCompat.Spinner">
<item name="popupTheme">#style/ThemeOverlay.AppCompat.Light</item>
<item name="android:popupMenuStyle">#style/Widget.AppCompat.Light.PopupMenu</item>
</style>
</resources>
The Spinner is set up as simply as:
ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item, mCategories);
mSpinner.setAdapter(adapter);
where mSpinner is the inflated Spinner and mCategories is a String array. In XML the Spinner is defined as
<Spinner
android:id="#+id/my_spinner"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
I have tried adding various styles directly to the Spinner, but it does not work.
In my AndroidManifest.xml I have added the following to the Application tag:
android:theme="#style/MyTheme"
I managed to do it (or to be very close to it) by changing themes and styles like that:
Just add to your theme:
<item name="android:spinnerDropDownItemStyle">#style/MySpinnerItem</item>
Then create a style for MySpinnerItem that inherits from Widget.AppCompat.DropDownItem.Spinner:
<style name="MySpinnerItem" parent="#style/Widget.AppCompat.DropDownItem.Spinner">
<item name="android:textColor">#color/your_text_color</item>
<item name="android:textSize">16sp</item>
<item name="android:paddingLeft">16dp</item>
<item name="android:paddingStart" tools:targetApi="jelly_bean_mr1">16dp</item>
<item name="android:paddingRight">16dp</item>
<item name="android:paddingEnd" tools:targetApi="jelly_bean_mr1">16dp</item>
</style>
That's it for the AppCompat theme.
Finally, if you want to add icons to list items, you have to create a custom layout and set it programmatically. You can follow this tutorial http://android-er.blogspot.sg/2010/12/custom-arrayadapter-for-spinner-with.html that explains it.
Basically you have to:
create a custom layout
create a custom adapter that inherits from ArrayAdapter
implements getCutomView() methods to set your different images for each item
finally set your adapter to the spinner with MyCustomAdapter.createFromResource(this, R.array.my_data, R.layout.my_cutom_item_layout);
The cause of my problem was in the line
ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item, mCategories);
Here I am using android.R.layout.simple_spinner_item which is probably meant for the Spinner itself. Using android.R.layout.simple_spinner_dropdown_item instead gives the desired look for the dropdown items.
The difference between those are described in some detail in this question, though the images are for older Android versions.
For reference, here are the two layouts taken directly from the Android source code.
simple_spinner_item
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:textAlignment="inherit"/>
simple_spinner_dropdown_item
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="?android:attr/dropdownListPreferredItemHeight"
android:ellipsize="marquee"/>
Adding icons to the dropdown items
Now, for the icon part, I followed the steps from #euitam and ended up with the following:
MyAdapter:
public class MyAdapter extends ArrayAdapter<String>
{
private String[] mCategories;
private int[] mIcons;
public CategoryDropDownAdapter(Context context, int layoutResourceId, String[] categories)
{
super(context, layoutResourceId, categories);
mCategories = categories;
// Add the same icon to all items, just for testing.
mIcons = new int[mCategories.length];
for (int i = 0; i < mIcons.length; i++)
{
mIcons[i] = R.drawable.my_icon;
}
}
/**
* View for a dropdown item.
* */
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
{
View rowView = convertView;
if (rowView == null)
{
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
rowView = inflater.inflate(R.layout.my_spinner_categories_dropdown_item, parent, false);
}
TextView categoryText = (TextView) rowView.findViewById(R.id.my_spinner_dropdown_item_text);
categoryText.setText(mCategories[position]);
ImageView icon = (ImageView) rowView.findViewById(R.id.my_spinner_dropdown_item_icon);
icon.setImageResource(mIcons[position]);
return rowView;
}
/**
* The Spinner View that is selected and shown in the *Spinner*, i.e. not the dropdown item.
* */
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View spinnerView = convertView;
if (spinnerView == null)
{
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
spinnerView = inflater.inflate(R.layout.my_spinner_categories_spinner_item, parent, false);
}
TextView categoryText = (TextView) spinnerView.findViewById(R.id.my_spinner_item_text);
categoryText.setText(mCategories[position]);
return spinnerView;
}
}
my_spinner_categories_dropdown_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/my_spinner_dropdown_item_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!-- Stolen from android.R.layout.simple_spinner_dropdown_item -->
<TextView
android:id="#+id/my_spinner_dropdown_item_text"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="marquee" />
</LinearLayout>
my_spinner_categories_spinner_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<!-- Stolen from android.R.layout.simple_spinner_dropdown_item -->
<TextView
android:id="#+id/my_spinner_item_text"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="1"
android:ellipsize="marquee" />
</LinearLayout>
And finally, setting the adapter:
MyAdapter adapter = new MyAdapter(getContext(), android.R.layout.simple_spinner_dropdown_item, mCategories);
mSpinner.setAdapter(adapter);

How to change the text color of Spinner drop down items?

I would like to change the text color of the Spinner drop down items to black.
I have a custom Spinner that lists different languages. It has an ImageView and a TextView. The Spinner is shown on a blue background with white text and it looks fine.
The problem is when it is clicked, the drop down shows up but the text is barely visible because of the light gray color of the drop down and the white color of the text.
This is the code for the Spinner in the activity_main.xml:
<Spinner
android:id="#+id/languageSpinner"
style="#android:style/Widget.Material.Light.DropDownItem.Spinner"
android:layout_width="140dp"
android:layout_height="40dp"
android:prompt="#string/selectLanguageSpinner"
android:spinnerMode="dropdown" />
This is the spinner_row_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:orientation="horizontal">
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
<TextView
android:id="#+id/language"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:layout_marginEnd="10dp"
android:layout_marginStart="5dp"
android:gravity="center_vertical"
android:lineSpacingExtra="20dp"
android:minHeight="40dp"
android:textColor="#color/white"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
Here the text color is set to white, because I need it to be white when nothing is selected.
This is the code in styles.xml:
<!-- Base application theme. -->
<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/myAppPrimaryColor</item>
<item name="colorPrimaryDark">#color/myAppPrimaryDarkColor</item>
<item name="colorAccent">#color/myAppAccentColor</item>
<item name="android:dropDownListViewStyle">#style/TestSpinnerStyle</item>
<item name="android:spinnerItemStyle">#style/mySpinnerItemStyle</item>
</style>
<style name="mySpinnerItemStyle">
<item name="android:paddingLeft">5dp</item>
<item name="android:paddingBottom">3dp</item>
<item name="android:textSize">20sp</item>
<item name="android:textColor">#color/white</item>
</style>
<!-- ^^ Here I need to set the color to white, because I also have default
spinner on the activity that needs to have a default white color.
But when the default spinner is clicked, the color of the
drop down items is black. How can I do that with my custom spinner? -->
<style name="TestSpinnerStyle" parent="android:style/Widget.ListView.DropDown">
<item name="android:textSize">22sp</item>
<item name="android:textColor">#color/black</item>
<item name="android:height">20dp</item>
<item name="android:divider">#color/gray</item>
<item name="android:dividerHeight">1dp</item>
<item name="android:dividerPadding">5dp</item>
<item name="android:background">#color/default_gray</item>
</style>
<!-- ^^ The text color property here is not reflected.. -->
Does anybody have any suggestions how to do that?
EDIT: After the help from the comments and the answers, I managed to do it. I thought that it could be done in XML...Anyways in the java code behind, this is what I did:
When the custom adapter is defined, we need to override the getDropDownView method, which is fired when the spinner is clicked and the drop down shows up. The getView method should also be overridden, so we can handle the default color when the Spinner is not clicked. In other words, here is the code:
public class CustomSpinnerAdapter extends ArrayAdapter<String> {
public CustomSpinnerAdapter(Context context, int textViewResourceId, String[] objects, TypedArray image){
super(context, textViewResourceId, objects);
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent){
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.spinner_row_layout, parent, false);
TextView label = (TextView) row.findViewById(R.id.language);
label.setText(languages_list[position]);
label.setTextColor(getResources().getColor(android.R.color.black));
ImageView icon = (ImageView) row.findViewById(R.id.icon);
icon.setImageDrawable(icons_list.getDrawable(position));
return row;
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.spinner_row_layout, parent, false);
TextView textView = (TextView) row.findViewById(R.id.language);
textView.setText(languages_list[position]);
textView.setTextColor(getResources().getColor(android.R.color.white));
ImageView icon = (ImageView) row.findViewById(R.id.icon);
icon.setImageDrawable(icons_list.getDrawable(position));
return row;
}
}
Notice that the color of the TextView in getDropDownView is set to black, while in getView it is set to white.
I've done that thing like that, but in BaseAdapter
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.spinner_item, null);
}
TextView txtTestText = (TextView) view.findViewById(R.id.txtSpinnerRowText);
txtTestText.setText(list.get(position).getText());
if (getSelectedPosition() == position) {
txtTestText.setTextColor(context.getResources().getColor(android.R.color.holo_blue_dark));
} else {
txtTestText.setTextColor(context.getResources().getColor(android.R.color.primary_text_light));
}
return view;
}
try this I hope is't work.
((Spinner)findViewById(R.id.spinnergender)).setAdapter(adapter);
((Spinner)findViewById(R.id.spinnergender)).setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.e("Value",""+position);
((TextView) ((Spinner)findViewById(R.id.spinnergender)).getSelectedView()).setTextColor(getResources().getColor(R.color.cyan));
if(parent.getItemAtPosition(position).toString().equals(getResources().getString(R.string.Gender))) {
((TextView) ((Spinner)findViewById(R.id.spinnergender)).getSelectedView()).setTextColor(getResources().getColor(R.color.Text));
}
}
You should create custom adapter (that extends BaseAdapter) for spinner and on getDropDownView inflate custom spinner_item_dropdown.xml, which is set to your wishes.

Coloring issue when setting Theme.AppCompat.Light.NoActionBar

i've a main activity xml which shows 2 button and 1 EditText and another layout with listview. The problem is my buttons and listview texts are white. I don't know how to resolve this.
styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:colorPrimary">#009688</item>
<item name="colorPrimaryDark">#00796b</item>
<item name="android:textColorPrimary">#FFF</item>
<item name="android:textColorSecondary">#FFF</item>
</style>
activity_main.xml (Design view)
You can see here, button isn't visible. But, if i give background as black it shows,
So, i don't want to give the background for each element because that's not a good solution.
Another activity with listactivity :
Here, the same problem.
For your ListView, you can overrider the getView method of the Adapter, then set the TextView color in code.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1, myList) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView text = (TextView) view.findViewById(android.R.id.text1);
text.setTextColor(Color.BLACK);
return view;
}
};
And for your button
Button btn = (Button)findViewById(R.id.btn);
btn.setTextColor(Color.BLACK);
or in xml:
<Button android:id="#+id/mybtn"
android:text="text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000000" /> <-- set color here -->
Alternatively, your could try using a dark theme such as
Theme.AppCompat.NoActionBar

custom style on dialog checkbox

I'm trying to style an AlertDialog to please a customer. They like the Blue title bar on
Theme.Holo.Light.Dialog but like the green checkboxes from another theme.
this is what I want to produce:
So, I've got a style definitions like this:
<style name="MyDialogTheme" parent="#android:Theme.Holo.Light.Dialog">
<item name="android:listChoiceIndicatorMultiple">#drawable/checkbox_green</item>
</style>
<style name="mycheckbox" parent="#android:style/Widget.CompoundButton.CheckBox">
<item name="android:button">#drawable/checkbox_box</item>
</style>
and I've got a definition for checkbox_green as follows which are just PNG files:
<item android:state_checked="false"
android:drawable="#drawable/checkbox_unchecked" />
<item android:state_checked="true"
android:drawable="#drawable/checkbox_checked"/>
</selector>
and I create my dialog builder with a specific theme in Java like so:
ContextThemeWrapper ctw = new ContextThemeWrapper( mContext, R.style.MyDialogTheme);
AlertDialog.Builder builder= new AlertDialog.Builder( ctw );
But I cannot get the dialog to display green checkboxes instead of the blue in this theme.
I get this:
I could go ahead and create an entire layout and then use that like this:
AlertDialog shareDialog = new AlertDialog.Builder(mContext).create();
LayoutInflater inflater = MainActivity.this.getLayoutInflater();
View dialogView = null;
dialogView = inflater.inflate(R.layout.share, (ViewGroup) getCurrentFocus());
shareDialog.setView(dialogView);
but that requires styling all of the dialog, not just the checkboxes.It seems so much simpler to just re-style the checkboxes but I'm not able to make that work.
what must I do, other than to create a complete layout and use to get the green checkboxes rather than the blue?
Actually, you CAN 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"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="#drawable/checkbox_checked" /> <!-- checked -->
<item android:state_pressed="true"
android:drawable="#drawable/checkbox_checked" /> <!-- pressed -->
<item android: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;
public class DialogUtil {
private DialogUtil() {
}
public static AlertDialog show(Context context) {
String[] items = {"text 1", "text 2", "text 3"};
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Test")
.setPositiveButton("OK", null)
.setAdapter(new CustomAdapter(context, items), null);
AlertDialog dialog = builder.show();
ListView list = dialog.getListView();
list.setItemsCanFocus(false);
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
list.setOnItemClickListener(listener);
return dialog;
}
private static OnItemClickListener listener = new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("DialogUtil", "Clicked on " + view);
}
};
private static class CustomAdapter extends ArrayAdapter<String> {
public CustomAdapter(Context context, String[] array) {
super(context, android.R.layout.simple_list_item_multiple_choice, array);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
if (view instanceof CheckedTextView) {
CheckedTextView checkedView = (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.
it should go as below...
first design your checkboxes as you want in any compatible format(jpeg,png)...and place them in drawable folder...
then make seperate xml for buttons and select images you designed for selected and unselected checkboxes...and place this file in drawable folder of your project with proper name say here customchk.xml...
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="#drawable/chk2" /> //custom image
<item android:state_checked="true" android:drawable="#drawable/chk1" /> //custom image
<item android:drawable="#drawable/chk2" /> <!-- default -->
</selector>
then make your checkbox:button set to your xml file...as below
<CheckBox
android:id="#+id/notifs"
android:layout_width="150dp"
android:button="#drawable/customchk" //your xml
android:layout_height="wrap_content"
/>
i guess it should work you do not need to change the all the dialog...
As far as I can tell, there's no way to just style parts of the dialog without creating a custom View object.
Create a style like:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AlertDialogCustom" parent="#android:style/AlertDialog">
<item name="android:textColor">#00FF00</item>
<item name="android:typeface">monospace</item>
<item name="android:textSize">10sp</item>
</style>
</resources>
Inflate view like:
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AlertDialogCustom));
Here are attributes for checkboxes to be used with your custom AlertDialog style.

Categories

Resources