I have a button that I have defined two selectors one for the text and one for background.
Selector for text color
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/yellow" android:state_activated="true" />
<item android:color="#color/pink" android:state_activated="false" />
</selector>
Selector for background
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/image_no_selected" android:state_activated="true" />
<item android:drawable="#drawable/image_selected" android:state_activated="false" />
</selector>
Button in Xml
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/myButton"
....
android:background="#drawable/selector_background"
...
android:textColor="#drawable/selector_text_color"
/>
When I press the button I want to change the state so I put this :
myButton.isSelected = isSelected //boolean that changes true or false
But is not changing the button even-though I'm changing the isSelected what I'm missing?
Selected isn't the same as activated. You need to use either:
myButton.isActivated = ...
Or change android:state_activated to android:state_selected:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/image_no_selected" android:state_selected="true" />
<item android:drawable="#drawable/image_selected" android:state_selected="false" />
</selector>
(it seems like your drawable names or reversed here by the way)
Related
So this is what I am trying to achieve (corners are rounded):
And here is where I'm stuck at (corners are not rounded):
Main activity xml:
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation"
style="#style/CustomBottomNavigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/bottom_navigation_menu"
app:itemBackground="#color/bottom_navigation_bar_background_color"
app:itemTextColor="#color/bottom_navigation_bar_content_color"
app:itemIconTint="#color/bottom_navigation_bar_content_color"/>
color/bottom_navigation_background_color.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="#color/background_selected" />
<item android:state_checked="false" android:color="#color/background" />
</selector>
color/bottom_navigation_content_color:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/text_active" android:state_checked="true" />
<item android:color="#color/text_inactive" />
</selector>
I think the problem is that you're trying to use color instead of drawable.
In the drawable folder create an xml called background_menu_item.xml, with content:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/nav_bar_colors_bg" />
<corners android:radius="120dp" />
</shape>
In the color folder create another XML called nav_bar_colors_bg.xml, with content like below:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/navBarActiveIconColor" android:state_checked="true" />
<item android:color="#color/navBarInactiveIconColor" android:state_checked="false"/>
</selector>
Then in the xml where you have the bottom navigation view add:
app:itemBackground="#drawable/background_menu_item"
Also if you'd like to change the icon color as well then add:
app:itemIconTint="#drawable/nav_bar_colors"
and create a nav_bar_colors.xml in the drawable folder with content like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/navBarInactiveIconColor" android:state_checked="true" />
<item android:color="#color/navBarActiveIconColor" android:state_checked="false" />
</selector>
Also make sure that the android:background is your inactive color to align with material design:
android:background="#color/navBarActiveIconColor"
Note: namings could be better I know + SO formed my reply incorrectly and due to lack of time I rather deleted it, but now after formatting I cannot undelete it so I'm posting it again.
I've got the following button:
<Button
android:id="#+id/searchCompaniesButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/company_icon"
android:drawablePadding="10dp"
android:drawableRight="#drawable/next_icon_big"
android:gravity="left|center_vertical"
android:paddingLeft="10dp"
android:backgroud="#drawable/transparent_button_selector"
android:paddingRight="10dp"
android:text="#string/searchCompanies" />
transparent_button_selector is transparent_button_selector.xml in /drawable-ldpi folder:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/lightGrey"
android:state_pressed="true" />
<item android:drawable="#color/veryLightGrey"
android:state_focused="true" />
<item android:drawable="#null" />
</selector>
Also I've got a colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="lightGrey">#3d3d3d</color>
<color name="veryLightGrey">#4d4d4d</color>
</resources>
But when I run my app, log will say:
'item' tag requires a 'drawable' attribute"
What the hell? I know there are few similar questions, but answers are not helpful. I tried:
Restarting Eclipse
Cleaning projects
Running app at real device(crush)
Replace this:
<item android:drawable="#null" />
with:
<item android:drawable="#color/transparent" />
and add transparent color in your color file with code: #00000000
The drawable attribute always takes #drawable.i.e
you have to replace
<item android:drawable="#color/..."
android:state_pressed="true" />
with
<item android:drawable="#drawable/..."
android:state_pressed="true" />
Use this,
<item
android:state_pressed="true"
android:drawable="#drawable/selected_state" />
Then make a shape in drawable folder with name "selected_state" like this.
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/lightGrey" />
</shape>
Your android:background attribute is misspelled:
android:backgroud="#drawable/transparent_button_selector"
The letter 'n' is missing in 'background'
i want to make a common theme for all my buttons in all activities across the whole applications. i want to put just 2 images for Focus and Default...i want to do all in xml how can i do this ??
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:state_pressed="false"
android:drawable="#drawable/btn_focused" />
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="#drawable/btn_pressed_focused" />
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/btn_pressed" />
<item android:color="#ffffff" />
</selector>
The easiest way is to use theming for your buttons. Read this: Styles and Themes
Create a style for your button, then create a style (which is a theme) with a parent such as
parent="android:Theme.something"
with the value
<item name="android:buttonStyle">#style/my_button_style</item>
Then in the manifest, there is a theme attribute both in the application and the activity element.
I know there are many questions of this kind here, but I really dont know the reason that my code is not working.
I'm trying to make a button with three states: normal, pressed and released. When I say released, I mean that I would like to make this like a toogle button, with the states active and not active.
When I release the button, it returns to the default state. I would like to change the image by click, as a checkButton works.
I tried this:
http://blog.androgames.net/40/custom-button-style-and-theme/
http://www.gersic.com/blog.php?id=56
Android: How to Create a Custom button widget
http://techdroid.kbeanie.com/2010/03/custom-buttons-on-android.html
custom_buttom.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="#drawable/focussed" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/focussedandpressed" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/pressed" />
<item android:drawable="#drawable/default" />
</selector>
layout.xml:
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/menu"
android:layout_weight="1" android:background="#drawable/custom_button"></Button>
You are using android:state_focused and android:state_pressed.
What about android:state_checked?
You want custom something like Checkbox, right? In that case, you need two image for state check true and false, and a transparent file for background (which have no content, just has the same size as other images). Create two drawable selector file have content like this:
background_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/your_bg" />
</selector>
state_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_focused="true"
android:drawable="#drawable/checkbox_on" />
<item android:state_checked="false" android:state_focused="true"
android:drawable="#drawable/checkbox_off" />
<item android:state_checked="false"
android:drawable="#drawable/checkbox_off" />
<item android:state_checked="true"
android:drawable="#drawable/checkbox_on" />
</selector>
Put these xml to your drawable folder, and in your layout, create a checkbox:
<CheckBox
android:layout_width="50dip"
android:layout_height="50dip"
android:background="#drawable/background_selector"
android:button="#drawable/state_selector"
/>
Hope this help ^_^
I have a custom ListView selector which draws on top of a ListView. It works fine, but I want the text inside of the listview to turn white. How can I do that?
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:state_focused="true"
android:drawable="#drawable/stocks_gradient" />
<item android:state_pressed="true"
android:drawable="#drawable/swipeview_selected_gradient" />
<item android:state_focused="true"
android:drawable="#drawable/swipeview_selected_gradient" />
</selector>
You need to apply specific color or selector to each list item.
I have something like this (abreviated)
layout/dash_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/DashboardListItem">
... Your text components etc ....
color/dashboard_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:state_focused="true"
android:drawable="#android:color/transparent" />
<item android:state_pressed="true"
android:drawable="#color/dash_border_color" />
<item android:state_focused="true"
android:drawable="#color/dash_border_color" />
</selector>
values/style.xml
<style name="DashboardListItem">
<item name="android:background">#color/dashboard_selector</item>
</style>
You probably will need to play with it to figure where exactly apply things. It can probably be simplified. Anyway, it's just a direction since I don't know your specific requirements.