After specifying a custom style for some buttons that I am using for an Android app, I noticed that a mysterious black border appearing around the button when clicked. I have tried overriding the border in my style, but nothing seems to work. How can I get rid of it?
orange_button.xml
<item android:state_pressed="true" android:drawable="#color/OrangePastel"/>
<item android:state_pressed="false" android:drawable="#color/OrangePastel"/>
<item android:state_focused="true" android:drawable="#color/GreenPastel"/>
<item android:state_focused="false" android:drawable="#color/GreyPastel"/>
<item android:state_selected="true" android:drawable="#color/YellowPastel"/>
<item android:state_selected="true" android:drawable="#color/PurplePastel"/>
<item android:state_window_focused="true" android:drawable="#color/LavenderPastel"/>
<item android:state_window_focused="false" android:drawable="#color/WhitePastel"/>
<item android:state_checkable="false" android:drawable="#color/RedPastel"/>
<item android:state_checkable="true" android:drawable="#color/DarkGreenPastel"/>
<item android:state_hovered="true"
android:drawable="#color/OrangePastel"/>
<item android:state_checked="false"
android:drawable="#color/OrangePastel"/>
<item android:state_checked="true" android:drawable="#color/OrangePastel"/>
activity_button_test.xml
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="ButtonTestActivity">
<Button
android:id="#+id/testButton2"
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/orange_button"/>
</LinearLayout>
Unpressed
Pressed
EDIT: try adding this in your styles.xml:
<resources>
<style name="AppTheme" parent="android:Theme.Holo.Light">
<item name="android:buttonStyle">android:borderlessButtonStyle</item>
</style>
</resources>
Related
Im trying to set a background color from the style xml depending if the state of the button is enabled or not.
Button XML:
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/loginButton"
style="#style/SuccessButton2"
android:layout_width="200dp"
android:layout_height="40dp"
android:text="Iniciar sesiĆ³n"
android:gravity="center"
android:enabled="true"
/>
Style SuccesButton2:
<style name="SuccessButton2" parent="TextAppearance.MaterialComponents.Button">
<item name="android:textColor">#color/button_text_color</item>
<item name="android:textSize">16sp</item>
<item name="android:background">#color/button_background_color</item>
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">40dp</item>
<item name="android:textAllCaps">false</item>
<item name="fontFamily">#font/myriad_pro_regular</item>
</style>
XML button_background_color:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="#color/dark_gray"/>
<item android:state_enabled="true" android:color="#color/red"/>
i have the same for the Text and its working fine:
button_text_color:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="#4E4E4E"/>
<item android:state_enabled="true" android:color="#android:color/black"/>
The background item its not working, it show the button with a transparent background only, enabled or not.
You can try with android:backgroundTint .
Tint to apply to the background.
<item name="android:background">#color/button_background_color</item>
<item name="android:backgroundTint">#color/button_background_color</item>
I want to change a color of this area behind the icons at TabLayout, only a line with icons. I also need to change this pink line under the title, but I can't find appropriate parameters at style. My style:
<style name="AppTabLayout" parent="Widget.Design.TabLayout">
<item name="tabIndicatorColor">?attr/colorAccent</item>
<item name="tabIndicatorHeight">4dp</item>
<item name="tabPaddingStart">6dp</item>
<item name="tabPaddingEnd">6dp</item>
<item name="tabBackground">?attr/selectableItemBackground</item>
<item name="tabTextAppearance">#style/AppTabTextAppearance</item>
<item name="tabSelectedTextColor">#color/darkPurple</item>
</style>
<!-- for text -->
<style name="AppTabTextAppearance" parent="TextAppearance.Design.Tab">
<item name="android:textSize">12sp</item>
<item name="android:textColor">#color/orange</item>
<item name="textAllCaps">false</item>
</style>
try this use following property of tab-layout
app:tabIndicatorColor="#color/colorBlack"// it set Selected tab underline color
app:tabSelectedTextColor="#color/colorPrimary"// it set Selected tab text color
app:tabTextColor="#color/colorAccent"// it set tab text color
Add atribute in xml:
<android.support.design.widget.TabLayout
....
app:tabBackground="#drawable/tab_color_selector"
...
/>
And create in drawable folder, tab_color_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/tab_background_selected" android:state_selected="true"/>
<item android:drawable="#color/tab_background_unselected"/>
</selector>
For the tab background color, create a selector and put that selector's resource ID like:
<item name="tabBackground">#drawable/selector_name</item>.
For the pink underline, change the color in
<item name="tabIndicatorColor">#color/your_new_color</item>
try this
Create bg_color in your drawable folder
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="#drawable/timecard"
android:color="#color/colorText" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="#drawable/timecard_selected"
android:color="#color/colorSkyBlue"/>
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="#drawable/timecard"
android:color="#color/colorText"/>
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="#drawable/timecard_selected"
android:color="#color/colorSkyBlue"/>
<!-- Pressed -->
<item android:state_selected="true" android:state_pressed="true" android:drawable="#drawable/timecard"
android:color="#color/colorText"/>
<item android:state_pressed="true" android:drawable="#drawable/timecard_selected"
android:color="#color/colorSkyBlue" />
and add this in your layout like this
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp"
android:tabStripEnabled="false"
app:tabIndicatorHeight="0dp"
android:fitsSystemWindows="true"
app:tabContentStart="0dp"
app:tabBackground="#drawable/bg_color"
app:tabPaddingStart="0dp"
android:paddingLeft="0dp"
app:tabPaddingEnd="0dp"
app:paddingEnd="0dp"
android:layout_gravity="center_vertical"
app:tabIndicatorColor="#color/colorSkyBlue"
android:paddingRight="0dp"
app:tabSelectedTextColor="#color/colorSkyBlue" />
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
I want to use a switch in my app, but I want it to have custom colors. Right now it doesn't show up at all.
Switch in my xml:
<Switch
android:id="#+id/switch"
android:track="#drawable/switch_track"
android:thumb="#drawable/switch_thumb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
switch_track.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="#color/grey_light"/>
<item android:state_pressed="true" android:drawable="#color/grey_light"/>
<item android:state_checked="true" android:drawable="#color/accent_light"/>
<item android:drawable="#color/grey_light"/>
</selector>
switch_thumb.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="#color/grey"/>
<item android:state_pressed="true" android:drawable="#color/grey"/>
<item android:state_checked="true" android:drawable="#color/accent"/>
<item android:drawable="#color/grey"/>
</selector>
Do I need to add icons for the selectors instead of colors or is there a way to just change the color?
If you are using the support library, you could use the material properties to color a SwitchCompat:
<android.support.v7.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
styles.xml:
<style name="AppTheme" parent="Base.Theme.AppCompat.Light">
<item name="colorSwitchThumbNormal">#color/grey</item>
<item name="colorControlActivated">#color/accent</item>
</style>
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