TextView state_pressed/state_focused/state_selected style change - android

I'm trying to change a TextView style based on its state.
My styles.xml contains:
<style name="text_normal_ops">
<item name="android:gravity">left</item>
<item name="android:textColor">#color/text_usual_color</item>
<item name="android:textStyle">bold</item>
</style>
<style name="text_normal_ops_pressed">
<item name="android:gravity">left</item>
<item name="android:textColor">#color/text_pressed</item>
<item name="android:textStyle">bold</item>
</style>
My selector (text_ops.xml)is defined as:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" style="#style/text_normal_ops_pressed" />
<item android:state_focused="true" style="#style/text_normal_ops_pressed" />
<item android:state_selected="true" style="#style/text_normal_ops_pressed" />
<item style="#style/text_normal_ops"/>
</selector>
But when I apply this to my textview (style="#drawable/text_ops") it does not work.
Any tips?
Thanks

In android as per my knowledge there is only two state-list 1. Color State List Resource 2. StateListDrawable. If you are using style in it then please recheck the doc
Please check below link for more info
http://developer.android.com/guide/topics/resources/color-list-resource.html
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

The problem is the line style="#drawable/text_ops" this should be style="#style/text_ops".
I haven't tried using a selector for styles but it would be cool if it works that way.

Related

Android TabLayout selected tab background

Is there really really no easy way to use a TabLayout and be able to set the colors of the tabs (selected, not selected)? Like the selected tab background uses colorPrimary, non selected tabs uses colorPrimaryDark or something? I've searched the web including this and this and much more. I can change the background color with solution 1 but now the indicator is missing and I want it back.
This can't be so hard to do..
Solution of first link:
<style name="Base.Widget.Design.TabLayout" parent="android:Widget">
<item name="tabBackground">#drawable/tab_background</item>
</style>
// tab_background
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/tab_background_selected" android:state_selected="true" />
<item android:drawable="#drawable/tab_background_unselected" android:state_selected="false" android:state_focused="false" android:state_pressed="false" />
</selector>
ANSWER:
<style name="Base.Widget.Design.TabLayout" parent="android:Widget">
<item name="tabBackground">#drawable/tab_background</item>
<item name="tabIndicatorColor">#color/colorAccent</item>
<item name="tabIndicatorHeight">3dp</item>
</style>
style change
<style name="Base.Widget.Design.TabLayout" parent="android:Widget">
<item name="tabBackground">#drawable/tab_background</item>
<item name="tabIndicatorColor">#000000</item>
<item name="tabIndicatorHeight">5dp</item>
</style>

Button style working in Android 5 but not Android 4

I have a button style with a blue background that works fine in API 22, but the same button appears in dark grey without the applied style in Android 4. This is my style:
<style name="MyApp.Plain" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">#drawable/background</item>
<item name="android:buttonStyle">#style/MyApp.Widget.Button</item>
</style>
<style name="MyApp.Widget.Button" parent="#android:style/Widget.Button">
<item name="android:background">#drawable/btn_blue</item>
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">14sp</item>
<item name="android:textColor">#fff</item>
<item name="android:gravity">center_vertical|center_horizontal</item>
</style>
My btn_blue.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn_disabled" android:state_enabled="false"/>
<item android:drawable="#drawable/btn_pressed" android:state_enabled="true" android:state_pressed="true"/>
<item android:drawable="#drawable/btn_normal_blue" android:state_enabled="true"/>
</selector>
and btn_normal_blue.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="90"
android:endColor="#368ac6"
android:startColor="#0e58a4" />
<corners android:radius="20dp" />
</shape>
What could be the reason for this behavior and how can I fix this?
EDIT: This does not work with support v7:22.2.0, but does work with v7:21.0.3. I didn't change anything besides the dependency and changed AppCompatActivity to ActionBarActivity.
Possibly this is an Android bug.
Try buttonStyle instead of android:buttonStyle, since this is AppCompat attribute pre Lollipop, so it should be without android prefix.
I have seen some layouts where the attributes are applied twice, with and without the Android prefix. Can you try this:
<style name="MyApp.Widget.Button" parent="#android:style/Widget.Button">
<item name="android:background">#drawable/btn_blue</item>
<item name="background">#drawable/btn_blue</item>
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">14sp</item>
<item name="android:textColor">#fff</item>
<item name="android:gravity">center_vertical|center_horizontal</item>
</style>
I am currently trying to find where I saw this previously, and why it worked. Let me know if it did.
Try to use AppCompat:
<style name="MyApp.Widget.Button" parent="Base.TextAppearance.AppCompat.Button">
<item name="android:background">#drawable/btn_blue</item>
...
</style>
#MichaƂ Kisiel answer is correct "Try buttonStyle instead of android:buttonStyle, since this is AppCompat attribute pre Lollipop, so it should be without android prefix."
I just want to add that if you for instance create a view programatically you should put android:buttonStyle and buttonStyle too, sample:
<item name="android:buttonStyle">#style/OrangeButton</item>
<item name="buttonStyle">#style/OrangeButton</item>

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?

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>

Style android spinner

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>

Categories

Resources