I can not add a style using the selector - android

I have styles
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="TextStyle" >
<item name="android:textSize">8sp</item>
<item name="android:textColor">#0A0A0A</item>
<item name="android:typeface">serif</item>
</style>
<style name="TextStylePressed" >
<item name="android:textSize">18sp</item>
<item name="android:textColor">#0A0A0A</item>
<item name="android:typeface">serif</item>
<item name="android:textStyle">bold</item>
<item name="android:background">#C2C2C2</item>
</style>
</resources>
and I use selector arrow.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#style/TextStylePressed" android:state_selected="true"/>
<item android:drawable="#style/TextStylePressed" android:state_pressed="true"/>
<item android:drawable="#style/TextStylePressed" android:state_focused="true"/>
<item android:drawable="#style/TextStyle" />
</selector>
The problem is that I can not understand - if it works?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="#drawable/arrow" // <---- Framed here
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/arrow" // <---- then here
android:orientation="vertical" >
</LinearLayout>
Consequently, after all attempts nothing happens

AFAIK selectors can only be defined to select a drawable and not any other attribute. If you check the doc , it compiles to StatelistDrawable, So i believe it can only be used for drawables

Related

Android - How can choose shader programmatically?

I implemented a shader in this way, the xml file is called horde_shader.xml, and looks like this:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:endColor="#color/bordeaux"
android:angle="270">
</gradient>
</shape>
I have also implemented the following themes, in the related style.xml file that looks like this:
<resources xmlns:tools="http://schemas.android.com/tools">
<style name="AllyTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/royalBlue</item>
<item name="colorAccent">#color/royalBlue</item>
<item name="android:navigationBarColor">#color/royalBlue</item>
<item name="colorShader">#drawable/ally_shader</item>
<item name="imageFaction">#drawable/ally</item>
<item name="android:textColorSecondary">#android:color/white</item>
<item name="android:textColorPrimary">#android:color/white</item>
<item name="android:textColorHint">#android:color/white</item>
<item name="android:fontFamily">serif</item>
</style>
<style name="HordeTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/bordeaux</item>
<item name="colorAccent">#color/bordeaux</item>
<item name="android:navigationBarColor">#color/bordeaux</item>
<item name="colorShader">#drawable/horde_shader</item>
<item name="imageFaction">#drawable/horde1</item>
<item name="android:textColorSecondary">#android:color/white</item>
<item name="android:textColorPrimary">#android:color/white</item>
<item name="android:fontFamily">serif</item>
</style>
<style name="Theme">Theme</style>
<style name="AppTheme">AppTheme</style>
</resources>
I tried in an activity to set the background in this way:
<TextView
android:id="#+id/banner_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Esempio Titolo"
android:textAlignment="center"
android:fontFamily="serif"
android:textColor="#FFF"
android:paddingBottom="5dp"
android:background="?attr/colorShader"
android:layout_alignParentBottom="true"
android:textSize="17dp"/>

How to change android AppCompatButton's text color on pressed

I have created following style for custom AppCompatButton.
<style name="AppTheme.secondary_button" parent="Base.Widget.AppCompat.Button">
<item name="colorButtonNormal">#color/secondary_button_color</item>
<item name="colorControlActivated">#color/menu_blue</item>
<item name="android:colorButtonNormal">#color/secondary_button_color</item>
<item name="android:textColorPrimary">#color/gray</item>
<item name="android:textColor">#color/gray</item>
</style>
What is the attribute to change text color on button pressed?
To do this, you just need to replace the textColor you specified with a selector:
Something like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#color/white" />
<item android:state_focused="true" android:color="#color/grey" /> <!-- (for accessibility) -->
<item android:color="#color/black" />
</selector>
and then assign it like this:
android:textColor="#drawable/button_foreground_selector"
<android.support.v7.widget.AppCompatButton
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
style="#style/CustomAppCompatButtonStyle"
android:text="Hi, Neo"/>
Not clicked button:
Clicked button:
Style I used:
<style name="CustomAppCompatButtonStyle" parent="Base.Widget.AppCompat.Button">
<item name="colorButtonNormal">#color/red</item>
<item name="colorControlActivated">#color/blue</item>
<item name="android:colorButtonNormal">#color/green</item>
<item name="android:textColorPrimary">#color/gray</item>
<item name="android:textColor">#drawable/button_foreground_selector</item>
</style>

Padding in custom button doesn't work

I have button style:
<style name="NavDrawerButton" parent="android:Widget.Button">
<item name="android:background">#drawable/navdrawer_item</item>
<item name="android:textColor">#color/navdrawer_item_text</item>
<item name="android:layout_marginTop">1dp</item>
<item name="android:gravity">start|center_vertical</item>
<item name="android:paddingLeft">16dp</item>
<item name="android:paddingRight">16dp</item>
<item name="android:textSize">16sp</item>
<item name="android:clickable">true</item>
</style>
In /drawable:
<!--Navdrawer_item.xml-->
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="#drawable/navdrawer_item_pressed"/>
<item
android:state_enabled="true"
android:drawable="#drawable/navdrawer_item_enabled" />
</selector>
<!--navdrawer_item_enabled-->
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/backrepeat">
</item>
</layer-list>
My button:
<Button
android:id="#+id/category1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/NavDrawerButton"
android:text="#string/category1"/>
Problem is that padding doesn't work. There is no padding between left edge of button and text inside it. I can't understard why it is so.
Could you help me please?
I have test your code there is no error...might be in your parent layout...
string.xml
<style name="NavDrawerButton" parent="android:Widget.Button">
<item name="android:background">#drawable/image1</item>
<item name="android:layout_marginTop">1dp</item>
<item name="android:gravity">start|center_vertical</item>
<item name="android:paddingLeft">10dp</item>
<item name="android:textSize">16sp</item>
<item name="android:clickable">true</item>
</style>
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"
tools:context="com.example.MainActivity"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="sdfg"
style="#style/NavDrawerButton" >
</Button>
</LinearLayout>
output

Change background of the button in Action Bar, Android

I'm having a problem, I'm developing a app and I want to change background color of the buttons of the Action Bar. I have made follow it:
In my menu.xml, I have this code:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".main" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_search"
app:showAsAction="always|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="#string/action_search"/>
<item android:id="#+id/action_carrito"
android:title="#string/action_example"
android:icon="#drawable/ic_carrito"
app:showAsAction="always" />
</menu>
And my styles.xml, I have this:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:actionBarStyle">#style/MyActionBar</item>
<item name="actionBarStyle">#style/MyActionBar</item>
<item name="actionButtonStyle">#style/MyActionButtonStyle</item>
<item name="android:actionButtonStyle">#style/MyActionButtonStyle</item>
</style>
<style name="MyActionBar" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#android:color/white</item>
<item name="android:textSize">18sp</item>
</style>
<style name="MyActionButtonStyle" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:background">#color/darker_green</item>
</style>
</resources>
Both buttons catch this background color. But I only want to put it for the button with id is "#+id/action_carrito". I hope somebody can to help me. Thanks in advance.
What if you try to create a action_carrito_bg.xml which is a layer-list under your drawable folder, that looks like:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#color/darker_green"></solid>
</shape>
</item>
<item>
<bitmap android:src="#drawable/ic_carrito"/>
</item>
</layer-list>
Then remove MyActionButtonStyle from styles.xml, and set that drawable as icon of the menu item:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".main" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_search"
app:showAsAction="always|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="#string/action_search"/>
<item android:id="#+id/action_carrito"
android:title="#string/action_example"
android:icon="#drawable/action_carrito_bg"
app:showAsAction="always" />
</menu>

Create an toggle / sliding button

I have follow the answer in Android: Create a toggle button with image and no text everything was fine but when I load my application and when I click on my toggle button, nothing happen like it didn't change from off state to on state. So what do i have to do to make the sliding works? Do i need to code anything if yes what should i code?
Thanks
btn_toggle_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+android:id/background"
android:drawable="#android:color/transparent" />
<item android:id="#+android:id/toggle"
android:drawable="#drawable/btn_toggle" />
</layer-list>
btn_toggle.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/close" />
<item android:state_checked="true"
android:drawable="#drawable/open" />
</selector>
layout.xml
<ToggleButton
android:id="#+id/toggleButton"
style="#style/toggleButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/btn_toggle_bg"
android:onClick="onToggleClicked"
android:layout_marginLeft="150dp" />
style.xml
<style name="Widget.Button.Toggle" parent="android:Widget">
<item name="android:background">#drawable/btn_toggle_bg</item>
<item name="android:disabledAlpha">?android:attr/disabledAlpha</item>
<style name="toggleButton" parent="#android:Theme.Black">
<item name="android:buttonStyleToggle">#style/Widget.Button.Toggle</item>
</style>
Follow the three steps and after it all your app's togglebutton got the new style!
toggle.xml: (drawable-folder)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="#drawable/toggle_off" />
<item android:state_checked="true" android:drawable="#drawable/toggle_on" />
</selector>
styles.xml: (values-folder)
<style name="toggle" parent="#android:style/Widget.Button.Toggle">
<item name="android:background">#drawable/toggle</item>
<item name="android:textOn"> </item>
<item name="android:textOff"> </item>
</style>
and last step you have to teach the current AppTheme that you have a custom toggle-button:
<style name="AppTheme" parent="android:Theme.Holo.Light">
<item name="android:buttonStyleToggle">#style/toggle</item>
</style>

Categories

Resources