how to change the button style programmatically - android

I have defined two styles in Style.xml, I want to change the style programmatically for certain conditions, Min SDK version 21
<style name="CheckOutButtonStyle" parent="#style/Widget.AppCompat.Button.Colored">
<item name="android:layout_height">60dp</item>
<item name="android:fontFamily">sans-serif-medium</item>
<item name="android:textSize">16sp</item>
<item name="android:padding">0dp</item>
<item name="android:textColor">#color/white_color</item>
<item name="android:layout_margin">#dimen/general_spacing</item>
</style>
<style name="ScanNextItemSecondaryButtonStyle" parent="#style/Widget.AppCompat.Button.Borderless">
<item name="android:layout_height">wrap_content</item>
<item name="android:fontFamily">sans-serif-medium</item>
<item name="android:stateListAnimator">#null
</item> <!-- remove the shadow, per UI request -->
<item name="android:textSize">16sp</item>
<item name="android:padding">0dp</item>
<item name="android:minHeight">0dp</item>
<item name="android:minWidth">0dp</item>
<item name="android:background">#null</item>
<item name="android:textColor">#color/colorSecondaryAccent</item>
<item name="android:layout_marginBottom">#dimen/general_spacing</item>
</style

1. Create xml in drawable.
\res\drawable\back_button_answer.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners android:radius="10dip" />
<!-- background -->
<gradient
android:startColor="#D6D7D6"
android:centerColor="#E2E2E2"
android:centerY="0.75"
android:endColor="#D6D7D6"
android:angle="270"
/>
<stroke android:width="2dip" android:color="#fff"/>
</shape>
2. Button btn.setBackgroundResource(R.drawable.back_button_answer);

Related

android:theme="#style/..." doenst take background

I got the following background for an edit text:
edit_text_rounded_white.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="#66c6c6c6"/>
<stroke android:width="0.1dp"
android:color="#66c6c6c6"/>
<corners android:radius="40dp"/>
</shape>
</item>
</selector>
in my styles.xml I define the following:
<style name="editText">
<item name="android:background">#drawable/edit_text_rounded_white</item>
<item name="android:drawablePadding">5dp</item>
<item name="android:paddingBottom">2dp</item>
<item name="android:paddingTop">2dp</item>
<item name="android:paddingLeft">10dp</item>
<item name="android:paddingRight">10dp</item>
<item name="android:textCursorDrawable">#drawable/edit_text_cursor</item>
<item name="colorControlNormal">#color/colorPrimaryDark</item>
<item name="colorControlActivated">#color/transparentWhite50</item>
</style>
When I set the style via: style="#style/editText" for my editTexts it takes the background but doesnt take the colorControl I set. When I set the style via android:theme="#style/editText" it takes the colorControls but doesnt show the background image. I already tried src and windowBackground instead of background in the style file.
Any ideas how achieve both? Showing background and the colorControls?
Split your style into two:
<style name="editTextStyle">
<item name="android:background">#drawable/edit_text_rounded_white</item>
<item name="android:drawablePadding">5dp</item>
<item name="android:paddingBottom">2dp</item>
<item name="android:paddingTop">2dp</item>
<item name="android:paddingLeft">10dp</item>
<item name="android:paddingRight">10dp</item>
<item name="android:textCursorDrawable">#drawable/edit_text_cursor</item>
</style>
<style name="editTextTheme">
<item name="colorControlNormal">#color/colorPrimaryDark</item>
<item name="colorControlActivated">#color/transparentWhite50</item>
</style>
And then apply both to your view:
<EditText
...
android:theme="#style/editTextTheme"
style="#style/editTextStyle"/>

Custom spinner UI with arrow and vertical line

I am trying to implement a spinner with a custom layout (not the spinner item layout), the outer layout. I want it to look like this:
http://imgur.com/a/U6RzU
I searched stackoverflow and google, but couldn't figure out how to come up with this (beside putting the whole picture as a background)
Thanks a lot in advance
Edit
This is what I tried:
styles.xml
<style name="spinner">
<item name="android:layout_marginTop">6dp</item>
<item name="android:layout_marginBottom">6dp</item>
<item name="android:layout_marginLeft">0dp</item>
<item name="android:layout_marginRight">0dp</item>
<item name="android:padding">10dp</item>
<item name="android:background">#drawable/spinner_bg</item>
<item name="android:popupBackground">#color/red</item>
<item name="android:textSize">15sp</item>
</style>
spinner_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<layer-list>
<item>
<shape>
<gradient android:angle="90" android:endColor="#ff0000"
android:startColor="#ff0000" android:type="linear" />
<stroke android:width="0.33dp" android:color="#0fb1fa" />
<corners android:radius="0dp" />
<padding android:bottom="3dp" android:left="3dp" android:right="3dp" android:top="3dp" />
</shape>
</item>
<item android:right="5dp">
<bitmap android:gravity="center_vertical|right" android:src="#drawable/arrow_white_down" />
</item>
</layer-list>
</item>
</selector>
where arrow_white_down is a png of a tiny white arrow (like the one in the link)
I solved it by using a RelativeLayout with the spinner and an image (arrow) beside it, it doesn't look really good in spinnerMode="dropdown" but this is the best I could do
layout
<RelativeLayout
android:id="#+id/spinner_container"
style="#style/spinner_container">
<Spinner
android:id="#+id/spinner"
style="#style/spinner"
android:layout_toLeftOf="#+id/spinner_image"
android:layout_toStartOf="#+id/spinner_image" />
<ImageView
android:id="#+id/spinner_image"
style="#style/spinner_image" />
</RelativeLayout>
styles.xml
<style name="spinner_container">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">35dp</item>
<item name="android:orientation">horizontal</item>
<item name="android:layout_marginTop">6dp</item>
<item name="android:layout_marginBottom">6dp</item>
<item name="android:layout_marginLeft">0dp</item>
<item name="android:layout_marginRight">0dp</item>
<item name="android:background">#color/red</item>
</style>
<style name="spinner">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_margin">0dp</item>
<item name="android:layout_alignParentLeft">true</item>
<item name="android:layout_alignParentStart">true</item>
<item name="android:padding">5dp</item>
<item name="android:textColor">#color/white</item>
<item name="android:background">#color/red</item>
<item name="android:popupBackground">#color/red</item>
<item name="android:textSize">15sp</item>
</style>
<style name="spinner_image">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">match_parent</item>
<item name="android:layout_margin">5dp</item>
<item name="android:padding">0dp</item>
<item name="android:layout_alignParentRight">true</item>
<item name="android:layout_alignParentEnd">true</item>
<item name="android:adjustViewBounds">true</item>
<item name="android:src">#drawable/bc_spinner_arrow</item>
</style>
where bc_spinner_arrow is a transparent pic of a white arrow and a vertical line (like the one in the question)

How to add styling to the android spinner

Here is my code for spinner_style.xml
<item><layer-list>
<item><shape>
<gradient android:angle="90" android:endColor="#B3BBCC" android:startColor="#E8EBEF" android:type="linear" />
<stroke android:width="1dp" android:color="#000000" />
<corners android:radius="4dp" />
<padding android:bottom="3dp" android:left="3dp" android:right="3dp" android:top="3dp" />
</shape></item>
<item ><bitmap android:gravity="bottom|right" android:src="#drawable/spinner_arrow" />
</item>
</layer-list></item>
style.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="spinner_style">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:background">#drawable/spinner_style</item>
<item name="android:layout_margin">10dp</item>
<item name="android:paddingLeft">8dp</item>
<item name="android:paddingRight">20dp</item>
<item name="android:paddingTop">5dp</item>
<item name="android:paddingBottom">5dp</item>
<item name="android:popupBackground">#DFFFFFFF</item>
</style>
And in my activity layout I am using spinner as:
<Spinner
android:id="#+id/spinner"
style="#style/spinner_style" />
But I am not getting the required design. Here is the layout image for the given code. The spinner is covering the whole screen instead of height as wrap_ content:
I need to add change
<item name="android:layout_height">wrap_content</item>
to
<item name="android:layout_height">30dp</item>
in style.xml. Though I still don't know why wrap content is not working here.

No Ripple Effect With SelectableItemBackground

I'm trying to implement material design to legacy code.
I want a button to have a ripple effect on click but it shows nothing.
I'm using default ?android:attr/selectableItemBackground.
this is button xml:
<Button
android:text="Wandio"
android:padding="10dp"
android:textColor="#ffffff"
android:background="?android:attr/selectableItemBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/wandio_link" />
style in values folder:
<resources>
<color name="main_green">#45bb61</color>
<style name="AppTheme" parent="AppTheme.Base">
</style>
<style name="Theme.Splash" parent="android:Theme">
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="colorPrimary">#909CD4</item>
<item name="colorPrimaryDark">#D490CA</item>
<item name="colorAccent">#FFFFFF</item>
<item name="android:imageButtonStyle">#style/ImageButtonStyle</item>
<item name="android:windowBackground">#color/main_green</item>
</style>
<style name="ImageButtonStyle">
<item name="android:background">#android:color/transparent</item>
</style>
</resources>
style in values-v21 folder:
<resources>
<style name="AppTheme" parent="AppTheme.Base">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowReturnTransitionOverlap">true</item>
<item name="android:windowSharedElementEnterTransition">#android:transition/move</item>
<item name="android:windowSharedElementExitTransition">#android:transition/move</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:fitsSystemWindows">true</item>
<item name="android:imageButtonStyle">#style/StyleApi21</item>
<item name="android:buttonStyle">#style/StyleApi21</item>
</style>
<style name="StyleApi21">
<item name="android:background">?android:attr/selectableItemBackgroundBorderless</item>
</style>
Never had this problem before. Can't find what I'm missing.
Any suggestions?
I tryed the same thing and I have used this in my custom button xml:
<item ><ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight">
<item android:id="#android:id/mask">
<shape android:shape="oval">
<solid android:color="#FFBB00" />
</shape>
</item>
<item>
<shape android:shape="oval">
<solid android:color="#color/colorAccent" />
</shape>
</item>
</ripple>
Here is my another xml for older devices:
<item>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/fab_selected" android:state_selected="true"/>
<item android:drawable="#drawable/fab_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/fab_normal"/>
</selector>
</item>
These drawable files are just different colors of the button.

Android TabBar Style not scaling to full screen

This is my current TabBar Style
<style name="SM5Theme" parent="android:style/Theme.Holo.Light">
<item name="android:actionBarTabStyle">#style/SM5Theme.ActionBarTabStyle</item>
<item name="android:actionBarTabBarStyle">#style/SM5Theme.ActionBarTabBarStyle</item>
<item name="android:actionBarTabTextStyle">#style/SM5Theme.ActionBarTabTextStyle</item>
</style>
<style name="SM5Theme.ActionBarTabStyle" parent="android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#drawable/tab_bar_background</item>
<item name="android:gravity">center</item>
<item name="android:dividerPadding">0dp</item>
<item name="android:showDividers">middle</item>
<item name="android:textColor">#color/tab_highlight</item>
</style>
<style name="SM5Theme.ActionBarTabTextStyle" parent="#android:style/Widget.Holo.Light.ActionBar.TabBar">
<item name="android:textColor">#color/tab_textcolor</item>
</style>
<style name="SM5Theme.ActionBarTabBarStyle" parent="#android:style/Widget.Holo.Light.ActionBar.TabView">
</style>
TabBarBackground:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="#drawable/tab_bar_background_non_selected"/>
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="#drawable/tab_bar_background_selected"/>
<item android:state_selected="false" android:state_pressed="true" android:drawable="#color/tab_highlight"/>
<item android:state_selected="true" android:state_pressed="true" android:drawable="#drawable/tab_bar_background_selected_pressed"/>
</selector>
tab_bar_background_selected:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="-3dp" android:left="-3dp" android:right="-3dp">
<shape android:shape="rectangle">
<stroke android:color="#color/tab_bottom_bar" android:width="3dp"/>
<solid android:color="#color/tab_background_selected"/>
</shape>
</item>
</layer-list>
This works satisfactory on generating the Tab Theme and underline color. However, the problem is it's doesn't scale all the way.
How can I solve this? I don't want to use sherlock or the android theme generator.

Categories

Resources