Style android spinner - android

I'm trying to get my android app a bit more stylish and have made some progress but the spinner dropdown are giving me trouble. I've got a screenshot to show you the problem:
What I want is the white box in the background to be transparent, as in the same grey overlay over the back screen as the rest of the screen outside the dropdown.
Android 4.0, API 15 if I recall correctly.
As requested here's how I do it so far. These are snippets I think are relevant but I might have missed something.
Here's the xml for spinner:
<Spinner
android:id="#+id/edit_countrySpinner"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:entries="#array/edit_countryArray"
android:gravity="center"
android:prompt="#string/country_prompt" />
In my values/style.xml. If I change the color of the background here the background in the dialog do change, but all the rest of the backgrounds as well. I haven't figure out how to jus change the background in the dropdown dialog.
<style name="appcin" parent="#android:style/Theme.NoTitleBar.Fullscreen">
<item name="android:spinnerStyle">#style/spinnerStyle</item>
<item name="android:spinnerItemStyle">#style/SpinnerItem</item>
<item name="android:spinnerDropDownItemStyle">#style/SpinnerDropDownItem</item> -->
<item name="android:background">#FFFFFF</item>
</style>
<style name="spinnerStyle">
<item name="android:background">#drawable/pink_white_dropdown</item>
<item name="android:clickable">true</item>
</style>
<style name="SpinnerItem">
<item name="android:textColor">#993399</item>
<item name="android:background">#drawable/pink_white_dropdown</item>
<item name="android:maxHeight">10dip</item>
</style>
<style name="SpinnerDropDownItem">
<item name="android:textColor">#993399</item>
<item name="android:background">#FFFFFF</item>
</style>
I've tried adding this to the app theme but none of them made any difference, the background was still white.
<...theme....>
<item name="android:dropDownListViewStyle">#style/DropDownStyle</item>
<item name="android:dropDownSpinnerStyle">#style/DropDownStyle</item>
<item name="android:dropDownSelector">#style/DropDownStyle</item>
</... theme ...>
<style name="DropDownStyle">
<item name="android:background">#FFF000</item>
<item name="android:popupBackground">#FFF000</item>
<item name="android:cacheColorHint">#FFF000</item>
</style>
In drawable/pink_white_dropdown, just showing the same image for all the cases. pink_white_arrow is a 9patch image I've made. There are lots of guides, here's one I found by 30sec on google.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="#drawable/pink_white_arrow"/>
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="#drawable/pink_white_arrow"/>
<item android:state_pressed="true"
android:drawable="#drawable/pink_white_arrow"/>
<item android:state_pressed="false"
android:drawable="#drawable/pink_white_arrow"/>
<item android:state_focused="true" android:state_enabled="true"
android:drawable="#drawable/pink_white_arrow"/>
<item android:state_enabled="true"
android:drawable="#drawable/pink_white_arrow"/>
<item android:state_focused="true"
android:drawable="#drawable/pink_white_arrow"/>
<item
android:drawable="#drawable/pink_white_arrow"/>
</selector>
Somewhere in these files I figure something should be made transparent, but I don't know where.

Remove the SpinnerStyle background attribute.
Remove this:
<style name="spinnerStyle">
<item name="android:background">#drawable/whitish_rectangle</item>
</style>
which is what draws the background white rectangle.
Building on the code in your question, here is a basic example on how you can style your Spinner:
The styles.xml file sets styles for the SpinnerItem and SpinnerDropDownItem:
<resources>
<style name="customtheme" parent="#android:style/Theme.Light">
<item name="android:spinnerItemStyle">#style/SpinnerItem</item>
<item name="android:spinnerDropDownItemStyle">#style/SpinnerDropDownItem</item>
</style>
<style name="SpinnerItem">
<item name="android:textColor">#993399</item>
<item name="android:background">#drawable/my_rectangle</item>
</style>
<style name="SpinnerDropDownItem">
<item name="android:textColor">#993399</item>
<item name="android:background">#drawable/my_rectangle</item>
</style>
</resources>
For testing, I've created a bright-colored drawable shape, called my_rectangle.xml. Replace this with your own drawable:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#ff0000" />
<stroke
android:width="1dp"
android:color="#888888" />
</shape>
Add the Spinner to the Activity's layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="40dip">
<Spinner
android:id="#+id/edit_countrySpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/location_names"/>
</LinearLayout>
This produces:
with no white square in the background.

add
<item name="android:popupBackground">#null</item>
to your spinner style.

style android spinner
Spinner Widget requires two different custom layouts, one for the view to be displayed and another for the drop down list!
In this example blog on Custom Spinner Android, we will guide you how to create a custom spinner in android studio which will have:
Customized Background for Selected Item
Custom Selected Item Text Color
Text Color for Dropdown
Background for Drop down List
Spinner Animation
Link here: https://androiddvlpr.com/custom-spinner-android/

I had the same problem, and the below code worked for me:
<item name="android:popupBackground">#null</item>

Related

Android text selection handles have white background (instead of transparent)

The following screenshot shows my problem:
The text selection handles have a white background and overlay other UI elements. How can I make the background transparent?
Or the better question is actually: what could I possibly have done to make the background white?
Here is the style applied to the EditText:
<style name="TextInputLayout" parent="AppTheme">
<item name="android:background">#color/grey_50</item>
<item name="colorControlNormal">#color/grey_500</item>
<item name="colorControlActivated">#color/grey_900</item>
<item name="colorControlHighlight">#color/grey_900</item>
<item name="backgroundTint">#color/grey_900</item>
<item name="colorAccent">#color/grey_900</item>
<item name="colorPrimary">#color/grey_900</item>
<item name="android:textCursorDrawable">#null</item>
</style>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/grey_500</item>
<item name="colorPrimaryDark">#color/grey_500</item>
<item name="colorAccent">#color/grey_700</item>
<item name="android:colorBackground">#color/grey_50</item>
<item name="android:colorForeground">#color/grey_900</item>
<item name="android:textColorPrimary">#color/grey_900</item>
<item name="android:textColorPrimaryInverse">#color/grey_50</item>
<item name="android:textColorSecondary">#color/grey_900</item>
<item name="android:textColorSecondaryInverse">#color/grey_50</item>
<item name="android:windowBackground">#color/grey_50</item>
<item name="android:textColorHint">#color/grey_700</item>
<item name="android:colorActivatedHighlight">#color/grey_900</item>
<item name="colorControlNormal">#color/grey_700</item>
<item name="colorControlActivated">#color/grey_900</item>
<item name="colorControlHighlight">#color/grey_900</item>
<item name="android:textColorHighlight">#color/grey_500</item>
<item name="android:colorControlNormal" tools:targetApi="lollipop">#color/grey_700</item>
<item name="android:colorControlActivated" tools:targetApi="lollipop">#color/grey_900</item>
<item name="android:colorControlHighlight" tools:targetApi="lollipop">#color/grey_900</item>
<item name="android:stateListAnimator" tools:targetApi="lollipop">#null</item>
<item name="android:statusBarColor" tools:targetApi="lollipop">#color/grey_900</item>
<item name="android:navigationBarColor" tools:targetApi="lollipop">#color/grey_900</item>
</style>
Edit #1
The EditText's XML (note that I see the same behaviour with EditTexts and AppCompatEditTexts):
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_home_autocomplete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_horizontal_margin_small"
android:layout_marginTop="#dimen/activity_horizontal_margin_small"
android:layout_weight="3.5"
android:background="#null"
android:hint="#string/location"
android:padding="#dimen/activity_vertical_margin_medium"
android:theme="#style/TextInputLayout"
app:hintAnimationEnabled="true"
app:hintEnabled="true"
app:hintTextAppearance="#style/TextAppearance.TextInputLayout">
<AutoCompleteTextView
android:id="#+id/user_home_autocomplete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3.5"
android:dropDownWidth="match_parent"
android:inputType="textAutoComplete"
android:maxLines="1"
android:paddingTop="#dimen/activity_horizontal_margin_small" />
</android.support.design.widget.TextInputLayout>
The Phone I used for the screenshot runs Android 6.0.1 with OxygenOS 3.2.7
Getting rid of all #null elements did not work.
This could happen if you set a background color in your main theme , something like this
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
<item name="android:background">#color/white</item>
...
</style>
remove this property solve this issue for me , if you want to setup a background for all your screens set windowBackground instead.
<item name="android:windowBackground">#color/white</item>
As per the android docs, the theme is applied to views used in the layouts of entire activity or application. For individual views in the layout , we need to apply styles.
Ref: https://developer.android.com/guide/topics/ui/themes.html
The theme is applied as a common style for all the views in an activity or application.
Remove this from your theme <item name="android:popupBackground">????</item>
This happens because you set the background as part of the style, and declare android:theme="#style/TextInputLayout" as theme and not as style.
This makes that the children views of that view have the same behaviour including the text selection handler.
Move this line <item name="android:background">#color/grey_50</item> out of the style.
please set the background for the view :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="YOUR_CUSTOM_COLOR" />
<item android:state_selected="true" android:color="#80000000" />
<item android:color="YOUR_CUSTOM_COLOR" />
</selector>
try this may help you
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Pressed State -->
<item android:state_pressed="true"
android:color="#FFFFFF" />
<!-- Focused State -->
<item android:state_focused="true"
android:color="#FFFFFF" /> <!-- use background color here -->
<!-- Default State -->
<item android:color="#000000" />
</selector>
set this property as background to your edit-text

Transparent/translucent Activity on top of another activity having orange Background onClick

I have an activity on top of another. I am using the below code as theme in the manifest file.
<style name="Theme.Transparent" parent="android:Theme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:colorBackgroundCacheHint">#null</item>
</style>
It works totally fine. But click on any part of view has a orange background as selected area.
This is how it looks.
Any idea how to solve it?
I did a quick fix by overriding background as custom drawable to all of my views present in that activity. But I want to follow a standard.
Here's how the code looks like.
I created a drawable called selectable_background for android below 21.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:drawable="#android:color/transparent" />
<item android:drawable="#color/color_clicked" />
</selector>
for android 21 and above
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ripple_black" />
</selector>
<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/color_clicked"/>
and added to all the views
android:background="#drawable/selectable_item_background"
Reference

how can i override the Dialog Style of a theme like appcombat?

i have been trying to make my own dialog style. This is what i have now:
There are two issues I want to fix, I want to get rid of the blue line below the title.
And I have applied a listSelector to the list which is probably been overridden by the parent style, because it only appears on the selected item.
This is the code of the style of the dialog:
<style name="CustomDialogTheme" parent="">
<item name="android:windowIsFloating">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:padding">0dp</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:fitsSystemWindows">true</item>
<item name="android:listSelector">#drawable/list_selector</item>
</style>
I have tried changing the parent and using Theme.Dialog. The problem then becomes that i do not want borders around the dialog. And by setting the windowBackground to transparent did not fix that.
People said I would have to create my own 9png, and I did but somehow it was added on top of the Theme.Dialog style.
How should I proceed given my problem?
It seems that my style does not override the parent's attributes given, only adds to them?
Here is the listSelector code:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="false"
android:state_pressed="false"
android:drawable="#drawable/gradient_bg" />
<item android:state_pressed="true"
android:drawable="#drawable/gradient_bg_hover" />
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="#drawable/gradient_bg_hover" />
</selector>
Another way I could deal with this would be to change the blue line below the title color. Is that possible?

styles and Buttons

i have made a style for the button to be seen in a "bolding" way.
but when i use the setEnabled(false) and setClickable(false) it make the button disabled but i can't see it because i am using a style , so how can i make the button more like "into" (as if it is clicked) when i use set Enable(false) ???
code:
signin.setClickable(false);
signin.setEnabled(false);
I don't want to discard the style but to modify it but i don't know how.
style:
<style name="btnStyleShakespeare" parent="#android:style/Widget.Button">
<item name="android:textSize">15sp</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">#FFFFFF</item>
<item name="android:gravity">center</item>
<item name="android:shadowColor">#000000</item>
<item name="android:shadowDx">1</item>
<item name="android:shadowDy">1</item>
<item name="android:shadowRadius">0.6</item>
<item name="android:background">#drawable/custom_btn_shakespeare</item>
<item name="android:padding">10dip</item>
thanks for any help.
You need to use a custom selector list which will be set as your button background.
Something like:
btnSelector.xml in your /drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- when button is pressed (touched/clicked) -->
<item android:drawable="#drawable/button_selected" android:state_selected="true" />
<!-- When button is disabled, i.e clickable and enabled is set to false -->
<item android:drawable="#drawable/button_disabled" android:state_enabled="false"/>
<!-- Default, when button is clickable -->
<item android:drawable="#drawable/button_normal" android:state_enabled="true" />
</selector>
And you will have 3 different drawables each for clicked/pressed state, disabled state and normal state.
Then in your custom style change:
#drawable/btnSelector.xml
Create another XML file in res\color named something like text_color.xml (based on Adil Soomro's answer here):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- disabled state -->
<item android:state_enabled="false" android:color="#000000" />
<item android:color="#FFFFFF"/>
</selector>
In your style.xml, replace
<item name="android:textColor">#FFFFFF</item>
to
<item name="android:textColor">#color/text_color</item>

Set selected item background color on Android dropdown navigation

I think this is an easy question, but I haven't been able to find an answer to my specific problem.
I've read this great article and would like to set the background color of the selected item on the Android Dropdown list on the stock ActionBar (I'm not using Sherlock and I'm targetting ICS+), as explained in the image link below:
So far I've set my theme in this way:
<resources>
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
<item name="android:dropDownListViewStyle">#style/MyDropDownListView</item>
</style>
<style name="MyDropDownListView" parent="android:style/Widget.Holo.ListView.DropDown">
<item name="android:listSelector">#drawable/ad_selectable_background</item>
</style>
</resources>
And this is my ad_selectable_background.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="#android:integer/config_mediumAnimTime" >
<item android:state_pressed="true" android:drawable="#android:color/holo_blue_light" />
<item android:drawable="#android:color/transparent" />
</selector>
My dropdown view resource is:
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
The pressed state works, but when you open the menu no selection is displayed.
I would like to set it to holo_blue_light using the selector, but I'm not able to find the correct syntax using XML.
I'm looking for an XML only answer if it's possible (i.e. not using any code). Thanks!
Apparently, it was quite simple!
I had to edit ad_selectable_background.xml to use the checked state (as I did before, but I was missing the theme below):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="#android:integer/config_mediumAnimTime" >
<item android:state_pressed="false" android:state_checked="true" android:drawable="#android:color/holo_blue_light" />
<item android:state_pressed="true" android:drawable="#android:color/holo_blue_light" />
<item android:drawable="#android:color/transparent" />
</selector>
And then edit the theme by adding the android:spinnerDropDownItemStyle item and disabiling the checkMark (as I didn't want the radio buttons), in this way:
<resources>
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
<item name="android:dropDownListViewStyle">#style/MyDropDownListView</item>
<item name="android:spinnerDropDownItemStyle">#style/MySpinnerDropDownItem</item>
</style>
<style name="MyDropDownListView" parent="android:style/Widget.Holo.ListView.DropDown">
<item name="android:listSelector">#drawable/ad_selectable_background</item>
</style>
<style name="MySpinnerDropDownItem" parent="android:style/Widget.DropDownItem.Spinner">
<item name="android:background">#drawable/ad_selectable_background</item>
<item name="android:checkMark">#null</item>
</style>
</resources>

Categories

Resources