Common theme for all Buttons in Android - java

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.

Related

How to set Android Button click effect, using different color themes

I don't have much experience and only started working on Android applications 2 weeks ago. Sorry in advance if the code or it's approach looks messy.
My current app uses 2 color themes: Dark Theme and Light Theme.
They are created within styles.xml.
All my Buttons and TextViews get background color from attr folder, where I declared colors. In colors.xml I initialized these colors. And in styles.xml I set which color belongs to which theme.
Now, back to giving a button click effect - the only way i know how to do that, is to add it as android:background.
But i have set for background my own color scheme, created in attr.
The question is - how to set effect for a button and at the same time keep colors of currently toggled color theme (theme can be changed with ToggleButton).
styles.xml
<resources>
<!-- Light theme. -->
<style name="LightTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:textSize">30sp</item>
<item name="colorPrimary">#color/colorBtnOperatorsTextLightTheme</item>
<item name="colorPrimaryDark">#306E32</item>
<item name="colorAccent">#color/colorPrimary</item>
<item name="colorBtnOperatorsText">#color/colorBtnOperatorsTextLightTheme</item>
<item name="colorBtnNumbersText">#color/colorBtnNumbersTextLightTheme</item>
<item name="colorBtnBackground">#color/colorBtnBackgroundLightTheme</item>
<item name="colorOutputText">#color/colorOutputLightTheme </item>
<item name="colorInputText">#color/colorInputLightTheme</item>
<item name="colorAppBackground">#color/colorAppBackgroundLightTheme</item>
<item name="android:windowAnimationStyle">#style/WindowAnimationTransition</item>
</style>
<!-- Dark theme. -->
<style name="DarkTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:textSize">30sp</item>
<item name="colorPrimary">#000000</item>
<item name="colorPrimaryDark">#000000</item>
<item name="colorAccent">#color/colorPrimary</item>
<item name="colorBtnOperatorsText">#color/colorBtnOperatorsTextDarkTheme</item>
<item name="colorBtnNumbersText">#color/colorBtnNumbersTextDarkTheme</item>
<item name="colorBtnBackground">#color/colorBtnBackgroundDarkTheme</item>
<item name="colorOutputText">#color/colorOutputDarkTheme</item>
<item name="colorInputText">#color/colorInputDarkTheme</item>
<item name="colorAppBackground">#color/colorAppBackgroundDarkTheme</item>
<item name="android:windowAnimationStyle">#style/WindowAnimationTransition</item>
</style>
<style name="WindowAnimationTransition">
<item name="android:windowEnterAnimation">#android:anim/fade_in</item>
<item name="android:windowExitAnimation">#android:anim/fade_out</item>
</style>
</resources>
This is how one of the buttons looks like in activity_main.xml and next to it - theme color changer - ToggleButton
<Button
android:id="#+id/seven"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_margin="5dp"
android:layout_weight="1"
android:text="7"
android:textColor="?attr/colorBtnNumbersText"
android:background="?attr/colorBtnBackground"
android:onClick="buttonClick"/>
<ToggleButton
android:id="#+id/colorToggle"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_margin="5dp"
android:layout_weight="1"
android:minWidth="0dp"
android:minHeight="0dp"
android:background="#drawable/toggle_selector"
android:textOff=""
android:textOn="" />
attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ds" >
<attr name="colorBtnOperatorsText" format="color" />
<attr name="colorBtnNumbersText" format="color" />
<attr name="colorBtnBackground" format="color" />
<attr name="colorOutputText" format="color" />
<attr name="colorInputText" format="color" />
<attr name="colorAppBackground" format="color" />
</declare-styleable>
</resources>
You can add Drawable resource file that include selector tag. By the tag you can add click effect. In selector tag you can add first item tag that have a property which is android:state_enabled="false" means selected state and 2nd item tag will be default state of your button. See the example.Use the drawable as background in your example.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false"
android:drawable="#drawable/selected" />
<item android:drawable="#drawable/default" />
</selector>
Create two drawables which are selected and default that will include shape tag by the tag you can design your button for selected and default state click effect in your drawable folder. Giving one example:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#color/gray"/>
<corners
android:radius="#dimen/_10sdp"/>
</shape>

AppCompatButton not changing Text nor background with selectors

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)

Button styling gives weird results

I have a simple Button style, like
<style name="AppTheme.Button" parent="Widget.AppCompat.Button.Colored">
<item name="colorButtonNormal">#color/colorPrimary</item>
<item name="android:textColor">#color/white</item>
</style>
at this point the output is as this
it seems that the style is not applied and the colors are not set correctly. When I change the style to something like this
<style name="AppTheme.Button" parent="Widget.AppCompat.Button.Colored">
<item name="android:background">#color/colorPrimary</item>
<item name="android:textColor">#color/white</item>
</style>
I get something like this
Thw Button looks insane, but the style is applied.
How can I retain the Button shape as shiwn in the first picture and apply my styles correctly?
Ok I've found a way to get the Job done quite promisingly
style.xml
<style name="AppTheme.Button" parent="Widget.AppCompat.Button.Colored">
<item name="android:background">#drawable/btn_ripple</item>
<item name="android:textColor">#color/white</item>
<item name="android:padding">10dp</item>
<item name="android:minWidth">88dp</item>
<item name="android:minHeight">36dp</item>
<item name="android:layout_margin">3dp</item>
<item name="android:elevation">1dp</item>
<item name="android:translationZ">1dp</item>
</style>
and last the btn_ripple.xml is
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/btn_press">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="2dp" />
<solid android:color="#color/colorPrimary" />
</shape>
</item>
</ripple>
and the result is
Regards
I think the problem may be located in the parent.
<style name="AppTheme.Button" parent="Widget.AppCompat.Button.Colored">
I belive the second snippet sets the button style, overriding the parent. Try to remove the parent, and see what happens.
<style name="AppTheme.Button">
<item name="android:background">#color/colorPrimary</item>
<item name="android:textColor">#color/white</item>
</style>

How to make a custom button for pressed and released at android?

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 ^_^

How to create a state list drawable for my ListView items?

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.

Categories

Resources