I have a requirement to vary the border box on a TextInputLayout, I have a theme that I have applied to all my TextInputLayouts and I want to modify boxStrokeWidth based on state
<style name="InputTextStyle" parent="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<item name="android:textColor">#color/secondary_button_text_selector</item>
<item name="boxStrokeColor">#color/text_field_border_color</item>
<item name="boxStrokeWidth">#Integer/my_int_selector </item>
</style>
is there a way of creating a selector that returns a dimension integer?
something like
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:dimen="1dp" android:state_pressed="true" /> <!-- pressed -->
<item android:dimen="4dp" android:state_focused="true" /> <!-- focused -->
<item android:dimen="1dp" android:state_enabled="false" /> <!-- focused -->
<item android:dimen="1dp" /> <!-- default -->
</selector>
Related
I have been trying to figure out how to change the tint of a compoundDrawable of an EditText for some time with no success. I am using the EditText as a button and would like to change the color of the drawable right like the EditText underline color. How do I add a drawable state to the compoundDrawables?
I would like to tint the drawable like the underline tint when clicked.
Here is my styles.
<style name="EditTextDropdown" parent="#android:style/Widget.EditText">
<item name="android:clickable">true</item>
<item name="android:fontFamily">sans-serif</item>
<item name="android:focusable">false</item>
<item name="android:focusableInTouchMode">true</item>
<item name="android:singleLine">true</item>
<item name="android:cursorVisible">false</item>
<item name="android:layout_marginLeft">16dp</item>
<item name="android:layout_marginRight">16dp</item>
<item name="android:drawableRight">#drawable/ic_drop_down_arrow_24dp</item>
</style>
EditText
<EditText
android:id="#+id/phone"
style="#style/EditTextDropdown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Add phone" />
Here is what it looks like normal
Here is what it looks like when pressed
you can directly override the appearance of the EditText by re-assigning it's attributes in the style of the Theme you are using in your app.
given code overrides the EditText Appearance.
<item name="android:editTextBackground">
#drawable/myenglishviolettheme_edit_text_holo_light
</item>
and the file in drawable is something like this myenglishviolettheme_edit_text_holo_light.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true" android:drawable="#drawable/myenglishviolettheme_textfield_default_holo_light" />
<item android:state_window_focused="false" android:state_enabled="false" android:drawable="#drawable/myenglishviolettheme_textfield_disabled_holo_light" />
<item android:state_enabled="true" android:state_focused="true" android:drawable="#drawable/myenglishviolettheme_textfield_activated_holo_light" />
<item android:state_enabled="true" android:state_activated="true" android:drawable="#drawable/myenglishviolettheme_textfield_focused_holo_light" />
<item android:state_enabled="true" android:drawable="#drawable/myenglishviolettheme_textfield_default_holo_light" />
<item android:state_focused="true" android:drawable="#drawable/myenglishviolettheme_textfield_disabled_focused_holo_light" />
<item android:drawable="#drawable/myenglishviolettheme_textfield_disabled_holo_light" />
</selector>
You may want to change the drawable resources to match your requirement
I would like to to change background color on Item in actionBar after it is pressed and keep the background.
Now I get to change the background color when I press the item on ActionBar but it when I release the item the background color came back as before.
Keep the color as in the image after I release the item:
menu.xml
<item android:id="#+id/object_classification" android:orderInCategory="95" android:title="#string/title" android:actionLayout="#style/AppTheme" app:showAsAction="always" />
style.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:actionBarItemBackground">#drawable/ab_item_background</item>
<item name="actionBarItemBackground">#drawable/ab_item_background</item>
</style>
ab_item_background.xml (in drawable)
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="#android:integer/config_mediumAnimTime">
<!-- focused/pressed: color=holo_orange_light -->
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="#android:color/holo_orange_light" />
<!-- pressed: color=holo_green_light -->
<item
android:state_pressed="true"
android:drawable="#android:color/holo_green_light" />
<!-- normal: color=transparent -->
<item
android:drawable="#android:color/transparent" />
</selector>
Thanks a lot
I added the following selector to my listview in my drawerlayout:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selected -->
<item android:state_focused="true" android:drawable="#color/colorPrimaryDark" /> <!-- focused -->
<item android:state_focused="true" android:state_pressed="true" android:drawable="#color/colorPrimaryDark" /> <!-- focused and pressed-->
<item android:state_pressed="true" android:drawable="#color/colorPrimaryDark" /> <!-- pressed -->
<item android:drawable="#color/colorPrimary" /> <!-- default -->
</selector>
colorPrimaryDark and colorPrimary are just color values
This work amost fine. But if i press and release an listitem on API 19 or lower, the item gets blue. How is that possible?
If you are using AppCompat as a theme you should reference the colors from theme like this
<item android:drawable="?attr/colorPrimary"
I found the problem.
android:choiceMode="singleChoice"
This appeared to be the problem.
android:cacheColorHint="#00000000"
add this attribute for your listview
I'm a newbie in Android development. I'm following the tutorial on this page, and trying to get an actionbar with tabs. http://developer.android.com/training/basics/actionbar/styling.html
My code seems to be exactly same as the sample in the page, except for the "android:drawable" in actionbar_tab_indicator.xml.
I used #color/blablabla rather than the #drawable/blablabla in the sample in that web page. But based on my knowledge, #color and #drawable are replaceable of each other.
Yet, I'm not getting any tab when I run the application. Only an activity with an actionbar. And the whole background is black(blank?).
Here is my themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!-- the theme applied to the application or activity -->
<style name="TabTheme" parent="#style/Theme.AppCompat">
<item name="android:actionBarTabStyle">#style/MyActionBarTabs</item>
<!-- android support library compatibility -->
<item name="actionBarTabStyle">#style/MyActionBarTabs</item>
</style>
<!-- ActionBar tabs styles -->
<style name="MyActionBarTabs"
parent="#style/Widget.AppCompat.ActionBar.TabView">
<!-- tab indicator -->
<item name="android:background">#drawable/actionbar_tab_indicator</item>
<!-- Support library compatibility -->
<item name="background">#drawable/actionbar_tab_indicator</item>
</style>
</resources>
And here is my actionbar_tab_indicator.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- STATES WHEN BUTTON IS NOT PRESSED -->
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false"
android:state_pressed="false"
android:drawable="#color/gold" />
<item android:state_focused="false" android:state_selected="true"
android:state_pressed="false"
android:drawable="#color/articlecolor" />
<!-- Focused states (such as when focused with a d-pad or mouse hover) -->
<item android:state_focused="true" android:state_selected="false"
android:state_pressed="false"
android:drawable="#color/gold" />
<item android:state_focused="true" android:state_selected="true"
android:state_pressed="false"
android:drawable="#color/articlecolor" />
<!-- STATES WHEN BUTTON IS PRESSED -->
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false"
android:state_pressed="true"
android:drawable="#color/black" />
<item android:state_focused="false" android:state_selected="true"
android:state_pressed="true"
android:drawable="#color/blue" />
<!-- Focused states (such as when focused with a d-pad or mouse hover) -->
<item android:state_focused="true" android:state_selected="false"
android:state_pressed="true"
android:drawable="#color/black" />
<item android:state_focused="true" android:state_selected="true"
android:state_pressed="true"
android:drawable="#color/blue" />
</selector >
I'm so confused; any help is appreciated. I'm using support compat library by the way
use android:background="#drawable/actionbar_tab_indicator" to set the background color of the xml.
Also to use your theme, make sure you set android:theme="#style/TabTheme"
In android actionBar Tabs, when you select a tab, that particular tab gets the blue-Underline-Highlight, and the remaining doesn't. Which is fine.
But my project wants the particular TEXT-COLOR of SELECTED tab to be of say BLUE color and all the other UNSELECTED tab's TEXT-COLOR to be of say GREY color.
How could I acheive variation in Text-Color ?
As far as I could go I made the text-color as BLUE.. but its blue color text for SELECTED and UNSELECTED TABS also.
Following is the code am using at the moment ( created in theme.xml in res/value/ )
<style name="CustomActionBarTheme"
parent="#style/Theme.AppCompat.Light">
<item name="android:actionBarTabStyle">#style/MyActionBarTabs</item>
<item name="android:textColor">#00A7F4</item>
<item name="android:actionBarTabTextStyle">#style/custom_actionbar_tab_text_style</item>
<!-- Support library compatibility -->
<item name="actionBarTabStyle">#style/MyActionBarTabs</item>
<item name="android:textColor">#00A7F4</item>
<item name="android:actionBarTabTextStyle">#style/custom_actionbar_tab_text_style</item>
</style>
<!-- ActionBar tabs styles -->
<style name="MyActionBarTabs"
parent="#style/Widget.AppCompat.Light.ActionBar.TabView">
<!-- tab indicator -->
<item name="android:background">#drawable/actionbar_tab_indicator</item>
<item name="android:textColor">#00A7F4</item>
<!-- Support library compatibility -->
<item name="background">#drawable/actionbar_tab_indicator</item>
<item name="android:textColor">#00A7F4</item>
</style>
<style name="custom_actionbar_tab_text_style">
<item name="android:textAllCaps">false</item>
</style>
Use color state list
Create res/color/filename.xml
Example:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#000000" /> <!-- pressed -->
<item android:state_focused="true" android:color="#000000" /> <!-- focused -->
<item android:color="#FFFFFF" /> <!-- default -->
</selector>
and then use it
<item name="android:textColor">#color/filename</item>
Learn more : http://developer.android.com/guide/topics/resources/color-list-resource.html
Try This
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#000000" /> <!-- pressed -->
<item android:state_focused="true" android:color="#000000" /> <!-- focused -->
<item android:color="#FFFFFF" /> <!-- default -->
or in code
<item name="android:textColor">#color/filename</item>
If i combine all lines together than answer would be following code. Create new folder under res folder( res/color/filename.xml )
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#FFFFFF" /> <!-- pressed -->
<item android:state_focused="true" android:color="#FFFFFF" /> <!-- focused -->
<item android:state_selected="true" android:color="#FFFFFF" /> <!-- selected -->
<item android:color="#B3FFFFFF" /> <!-- default -->