Customize spinner dialog: popupbackground setting for dialog - android

I have a spinner as shown below. While I can style the row("raspberrypi-0"), I am unable to change anything for the rest of the dialog.
I would like to change the popup background color. If possible the color of "Select a device for the sensor" should also be changed. Below is my spinner's xml defined inside a linear layout(not shown).
<fr.ganfra.materialspinner.MaterialSpinner
android:id="#+id/attached_device_value_dropdown"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:spinnerMode="dialog"
app:ms_alignLabels="false"
app:ms_arrowSize="#dimen/spinner_arrow_size"
app:ms_hint="#string/attached_device_select"
app:ms_enableErrorLabel="true"
app:ms_multiline="true"
app:ms_floatingLabelColor="#color/black"
app:ms_hintColor="#color/light_grey"
app:ms_baseColor="#color/black"
app:ms_highlightColor="#color/black"
app:ms_arrowColor="#color/black"/>
A similar setting: popupBackground exist when one sets spinner mode to "dropdown". I am looking for something similar to this but for dialog.
In Java, this is how I set my spinner:
CustomArrayAdapter customArrayAdapter = new CustomArrayAdapter(parentActivity, R.layout.spinner_dropdown_item, devices);
customArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
registeredDevicesDropdown.setAdapter(customArrayAdapter);
where spinner_dropdown_item is just a text view.
Also MaterialSpinner just extends Spinner, so most of the things should apply. Here is more info about it on github.

I solved it by hacking the adapter code for the MaterialSpinner. In your overridden getCustomView function, set the color for background and text.
public View getCustomView(int position, View convertView, ViewGroup parent, boolean isDropDownView) {
//TODO: use convert view.
LayoutInflater inflater = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final int resid = isDropDownView ? android.R.layout.simple_spinner_dropdown_item : android.R.layout.simple_spinner_item;
TextView row = (TextView) inflater.inflate(resid, parent, false);
row.setText(devices.get(position).getDeviceLabel());
//Hack: Cannot set the dialogs color from XML and text color from XML
row.setTextColor(ContextCompat.getColor(parentActivity, R.color.black)); //set Text Color for the row content
parent.setBackgroundColor(ContextCompat.getColor(parentActivity, R.color.colorSecondary)); //set backgraound color ofr skeleton.
return row;
}

Related

How to change the textColor of all items on the popup of an Android spinner?

I'm working on an app that presents a user with two graphic objects. Each object is associated with a corresponding spinner from which the user can select one of a handful of attributes. Touching one spinner deactivates the other (This is handled outside of spinner.setEnabled.)
The view contains a single seekbar from which the user can control the range of values applied to the most recently selected attribute.
Color is one of the attributes. As the seekbar moves, I can change the background color of the popup items. I need to set the text color of all items black for light background colors and to white for all dark colors.
if ( pos == object.COLOR_INDEX) {
//change spinner Background and Text color
spinner.setBackgroundColor(Colors.BACKGROUND[objectCurrent.getParams(pos)]);
TextView v ; int ct ;
for(int i=0; i<(ct=spinner.getChildCount()); ++i) {
v= (TextView)spinner.getChildAt(i);
v.setTextColor(Colors.FOREGROUND[objectCurrent.getParams(pos)]);
}
ColorDrawable drawable=(ColorDrawable) spinner.getBackground() ;
spinner.setPopupBackgroundDrawable(drawable);
spinner.setSelection(0); spinner.setSelection(pos);
}
I have not found a way to do this. Looping through spinner.getChildAt(i) affects only the currently displayed item and not those hidden on the popup.
I will appreciate any suggestions.
This apparently does the trick:
Resources res=getResources();
final List<String> spinnerItems=new ArrayList<String>(Arrays.asList(res.getStringArray(R.array.spin_settings)));
ArrayAdapter<String> aa=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,spinnerItems){
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
View v = super.getDropDownView(position, convertView,
parent);
((TextView) v).setTextColor(Colors.FOREGROUND[obj.getParams(param.COLOR_INDEX)]);
((TextView) v).setBackgroundColor(Colors.BACKGROUND[obj.getParams(param.COLOR_INDEX)]);
return v;
}
};
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerLeft.setAdapter(aa);
spinnerRight.setAdapter(aa);
The getDropDownView event seems to get raised for every item on the drop down list.

Android: Why does the background of an AlertDialog change when I set the background of the items in its list

My situation is this: I have an v7.AlertDialog which is being populated by a list using AlertDialog.Builder.setAdapter. When I have no background on the items in my list, the title and buttons on the adapter have a white background like so.
However, once I add a color to the items in the list (via setting a background attribute on the adapter items), the background of the title and buttons change to the window background color like so:
I pulled up the view in hierarchyviewer, and I noticed that this was happening because the title section and buttons on an AlertDialog have no background. However, why does this cause them to have a white background when there is no background on the items in my Adapter? Shouldn't they still be colored with the window background?
I was able to solve this by setting the window background, but it still just seems like curious behavior to me.
The code for my adapter and view is pretty uninteresting. The View is just
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/some_shade_of_gray">
<!-- A LinearLayout with some TextViews -->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:src="#drawable/arrow_graphic"
android:contentDescription="#null"/>
</RelativeLayout>
And the getView method in the adapter looks like.
public View getView(final int position, final View convertView, final ViewGroup parent) {
View toReturn = convertView;
if (toReturn == null) {
toReturn = LayoutInflater.from(mContext).inflate(
R.layout.address_view, parent, false);
// Initialize a ViewHolder with a bunch of fields.
}
ViewHolder holder = (ViewHolder) toReturn.getTag();
// set some text in TextViews
return toReturn;
}

Change text color of android.R.layout.simple_list_item_multiple_choice

I am trying to figure out how I can change the text color of android.R.layout.simple_list_item_multiple_choice
I know you can create your own listview with checkboxes but I really do not want to have to do that as the android.R.layout.simple_list_item_multiple_choice works perfectly, I would just like to know how to change the Text color.
I have come accross this question here but I do not understand how I can use the most voted up answer.
How can I override android.R.layout.simple_list_item_multiple_choice textview so I can change the color of it. Thank you.
You can change color easily.
You just have to override the getView() method.
And inflate the android.R.layout.simple_list_item_multiple_choice in particular view. now you can access the whole template.
So that you can get the TextView used in this template by finding using id android.R.id.text1. after that you can change the behavior of textview.
ArrayAdapter<String> list = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, your_data_container) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = LayoutInflater.from(InvitationFlowActivity.this).inflate(android.R.layout.simple_list_item_multiple_choice, null);
TextView tv = (TextView) v.findViewById(android.R.id.text1);
tv.setTextColor("Your_color");
}
};
And if you are using custome adapter then you can also override the same getView() method and you can customize it as per your requirement.
Just make custom layout - simple_list_item_multiple_choice.xml like this -
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:textColor="#FF0000"
/>
and use it in place of android.R.layout.simple_list_item_multiple_choice.xml
There are two ways to do this.
1. Override the getView method
2. Use custom layout instead of standard layout.
No. 2 is better as you have more control on the same and you can edit at your ease.
Alt+Click will clone the required layout to your res/layout folder.
Make changes to the new layout and use it in the adapter.
ArrayAdapter aa = new ArrayAdapter(getApplicationContext(),R.layout.text_change,listItems);
listView.setAdapter(aa);
Here listItems is the String array of list.
text_change.xml is the updated layout xml
listView is your listview in the main layout.
In order to dynamically update color of content use the following code
AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position==0)
{
//Intent intent = new Intent(CoffeeShop.this,DrinkCategoryActivity.class);
//startActivity(intent);
TextView txtV = (TextView)view;
txtV.setTextColor(Color.BLUE);
}
else
{
TextView txtV = (TextView)view;
txtV.setTextColor(Color.YELLOW);
Toast toast = Toast.makeText(getApplicationContext(),(CharSequence)"These Methods are yet to be made public", Toast.LENGTH_SHORT);
toast.show();
}
}
};
ListView listView = (ListView)findViewById(R.id.list_opt);
listView.setOnItemClickListener(itemClickListener);
Adding onclicklistner for the view will allow updating the color of the text upon clicking. You can add respective logic as to which color should the text be displayed.

Change Spinner style programmatically

I need to implement function of changing the application theme up on button click.
I have already changed successfully style of buttons, edittexts and textviews. But faced the problem of changing style for Spinner.
The only successful change is change of background color:
spinnerRoutes.setBackgroundResource(R.drawable.dark_theme_spinner_background);
BUT only background of title row changed. I need also to change textColor of rows and items, and background of spinner rows.
I have already tried alot of similar stackoverflow solutions but none of them worked.
Is it even possible to change style of spinner programatically?
If u want to give customize the drop down menu of the spinner u need to override the getView function in adapter class. And also if u want to control the text that appears on the Spinnner u need to this spinner.onItemSelectedListener() and modify the text view in the call back method
sample code for customizing spinner drop down menu:
#Override
public View getDropDownView (int position, View convertView, ViewGroup parent) {
/**The Adapter's view to be supplied for the spinner when constructing a spinner. */
View view = super.getView(position, convertView, parent);
TextView listView = (TextView) view.findViewById(android.R.id.text1);
listView .setBackgroundColor(Color.parseColor(fontBack_BgnColor));
listView .setHeight((int) heightPixels);
listView .setGravity(Gravity.CENTER);
listView .setTextSize(TypedValue.COMPLEX_UNIT_PX, 20);
listView .setTextColor(Color.parseColor(fontColor));
listView .setText(super.getItem(position));
return view;
}
Customizing spinner text(text that appears on top of the spinner):
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
((TextView) view).setTextSize(TypedValue.COMPLEX_UNIT_PX, 25);
((TextView) view).setTextColor(Color.parseColor(spinnerTextColor));
}
The other guys are right when it comes to fully customizing the items in the spinner.
But if the only thing you need to change is the style of the text in the items (meaning you still want a text view but with say different font, color, gravity) then you only need to pass the adapter a custom item layout:
This is how your adapter should look like:
spinner.setAdapter(new ArrayAdapter<String>(getActivity(), R.layout.custom_item, data)
where custom_item is an xml layout file that you create as follow:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center"
android:textColor="#android:color/red"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:minHeight="?android:attr/listPreferredItemHeightSmall" />
Just make sure that the text view is the one and only view in that xml, and that its id is as specified above, so the default adapter can recognize it.
Hope that helped.

How to set style in spinner

How to set style on spinner ? I have style with font size and family for custom edit text class and I need same for spinner. I tried to add style tag in spinner xml but it is ignored, I tried in adapter but it doesn't work.
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, tempList) {
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
Typeface externalFont = Typeface.createFromAsset(getAssets(), "fonts/CANDARA.TTF");
((TextView) v).setTypeface(externalFont);
return v;
}
}
Instead of using the simple spinner layout android.R.layout.simple_spinner_item, create your own layout file containing a TextView and style it the way you'd like. Then, pass a reference to that layout when you instantiate the ArrayAdapter.
You are passing the default android layout for the spinner items, instead create your own layout like:
<RelativeLayout
android:id="#+id/rel"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/txtSpinnerItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
stlye="#style/txtViewStyleHere"/>
</RelativeLayout>
Pass this layout to the adapter, in that case, you will have to create your own adapter class to access the textView.
you want to add customize spinner
for that see these two link it might be help you .
Link 1 normal as you want just changing text and all that,
and for hover effect on your spinner look this link . Link2

Categories

Resources