apply custom theme in activity for every edit texts - android

I want to change background shape for all edit texts in my app. I created xml file in the drawable folder like this.
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners android:topLeftRadius="10dp" android:topRightRadius="10dp" android:bottomLeftRadius="10dp" android:bottomRightRadius="10dp"/>
<stroke android:width="1sp" android:color="#android:color/darker_gray"/>
<solid android:color="#android:color/white"/>
<padding android:top="5dp" android:left="5dp" android:right="5dp" android:bottom="5dp"/>
</shape>
And I created style tag in the styles.xml file.
<style name="editTxt_theme" parent="#android:style/Widget.EditText">
<item name="android:background">#drawable/edittxt_shape</item>
</style>
And I apply this theme into my activity. But it applied all of other widgets. Is there anything wrong?
Thanks.

First, you need to define a theme for your app in your styles.xml and use android:editTextStyle to define your custom style for all EditText views like so:
<style name="MyAppTheme" parent="android:Theme.Light">
<item name="android:editTextStyle">#style/CustomEditTextStyle</item>
</style>
<style name="CustomEditTextStyle">
<item name="android:background">#drawable/edittxt_shape</item>
<item name="android:clickable">true</item>
<item name="android:enabled">true</item>
<item name="android:focusable">true</item>
<item name="android:focusableInTouchMode">true</item>
<item name="android:gravity">center_vertical</item>
</style>
All that's left now is tell your app to use your theme in your manifest:
<application
android:theme="#style/MyAppTheme" >
...
</application>
This way, you won't need to put style attributes on all your EditText views.

Related

how to set different color of solid property for different styles in drawable.xml in android?

i have problem and i really search a lot and wasted big time,
my problem is i have 2 style , one style for each activity and it's child with of course different (colorPrimary and colorPrimaryDark), this is the 2 styles :
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="adapter_button_color">#color/secondary_orange</item>
</style>
<style name="AppTheme.SurveyStyle" parent="AppTheme">
<item name="colorPrimary">#color/colorPrimary2</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark2</item>
<item name="colorAccent">#color/colorAccent2</item>
<item name="adapter_button_color">#color/button_click</item>
</style>
, and i have drawable names raised_selector using as background for buttons
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/raised_pressed"/><!-- pressed -->
<item android:drawable="#drawable/raised_normal" /> <!-- default -->
and the code of raised_normal for example :
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/colorPrimary" />
<stroke
android:width="3dp"
android:color="#color/colorPrimary" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<corners
android:bottomLeftRadius="3dp"
android:bottomRightRadius="3dp"
android:topLeftRadius="3dp"
android:topRightRadius="3dp" />
when i set <solid android:color="#color/colorPrimary" /> like that the color of button in different styles will be the same , so i search and find if i write
<solid android:color="?attr/colorPrimary" /> instead will get the primerycolor of the style .
is it fine for now !! so every think work ok using (?attr/colorPrimary) work ok but this in v-21 and above and minimum sdks if i set (?attr/colorPrimary) instead of "#color/colorPrimary" the application will crash .
so what i can do in this way and set the color depend on the style ?
thanks for your time and thanks for reading
This mean there is no solution for this problem in the older phone :(

How to override Theme.AppCompat.Light.DarkActionBar theme for edittext

I am trying to create a style for EditText (MyEditText), which inherits from base theme and added extra property settings.
<style name="MyMaterialTheme" parent="MyMaterialTheme.Base">
<item name="homeAsUpIndicator">#drawable/ic_back_indicator</item>
</style>
<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="MyEditText" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:background">#dbffb1</item>
</style>
When applied MyEditText style to EditText control, existing styles (from theme) seems to be overridden (like line under EditText vanished). Same thing happens when background is changed for EditText in the layout.
I am using targetSdkVersion 23.
I like to know what is the proper method to override a single property from default theme.
Here is the code for EditText in layout (as asked by #3amoura):
<EditText android:layout_width="match_parent"
android:layout_height="82dp"
android:id="#+id/et_description"
android:inputType="textMultiLine|textLongMessage"
android:lines="10"
android:hint="Describe your complaint in details"
android:gravity="top"
style="#style/MyEditText" />
The style of MyEditText should inherit from Widget.AppCompat.EditText instead of Theme.AppCompat.Light.DarkActionBar such that it will be
<style name="MyEditText" parent="Widget.AppCompat.EditText">
<item name="android:background">#dbffb1</item>
Then you use the style in the EditText xml
Edited
If you are only trying to add a color of the Line drawn -> setBackgroundTint
If you want to change both the background color and the line color, then custom layer-list or selector will be the only option. A sample layer-list
<?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="#dbffb1" /> <!-- background color-->
</shape>
</item>
<item
android:top="-2dp"
android:right="-2dp"
android:left="-2dp">
<shape>
<solid android:color="#android:color/transparent" />
<stroke
android:width="1dp"
android:color="#color/colorAccent" /> <!-- color of stroke -->
</shape>
</item>
</layer-list>

Alertdialog shows background three times on old devices

I'm trying to make a dialog with a custom style.
On android 5 it works:
http://i.stack.imgur.com/y7zsG.png
But on older devices this happens:
http://i.stack.imgur.com/uIXuR.png
styles.xml:
<resources>
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:textColorPrimary">#FFFFFF</item>
<item name="android:alertDialogTheme">#style/PopupDialog</item>
</style>
<style name="PopupDialog" parent="Theme.AppCompat.Dialog">
<item name="android:textColorPrimary">#FFFFFF</item>
<item name="android:background">#drawable/background_dialog</item>
<item name="android:windowBackground">#android:color/transparent</item>
</style>
</resources>
background_dialog.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:bottomRightRadius="10dp" android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp" android:topRightRadius="10dp"/>
<solid android:color="#7b8f8a"/>
</shape>
I Tried many things but nothing seems to work.

Changing button style in the whole application

I'm trying to change all the TextColor of my buttons in my app to white and also trying to make it bold. But that isn't happening, I'm overwriting the android:Widget.Button
I'm developing for Jelly Bean 4.1.2
What am I doing wrong?
Theme definition in manifest
android:theme="#style/spui" >
The theme where I like to
<style name="spui" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:buttonStyle">#style/Buttonspui</item>
</style>
The style for the button itself
<style name="Buttonspui" parent="android:Widget.Button">
<item name="android:background">#drawable/btn_default_holo_light</item>
<item name="android:minHeight">48dip</item>
<item name="android:minWidth">64dip</item>
<item name="android:textColor">#ffffff</item>
<item name="android:textStyle">bold</item>
</style>
The button
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="#string/edit"
android:id="#+id/btnEdit"
style="#style/Buttonspui"/>
For styling your Button you can use this:
Go to the drawable folder and make an XML (e.g. button.xml) file called "style" which contains the following:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient android:startColor="#449def" android:endColor="#2f6699" android:angle="270" />
<stroke android:width="1px" android:color="#000000" />
<corners android:bottomLeftRadius="0dp"
android:bottomRightRadius="0dp"
android:topLeftRadius="8dp"
android:topRightRadius="8dp"/>
<padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" />
</shape>
</item>
</selector>
This is my code, you can make the necessary changes you want
Now call it in your layout XML (mainactivity.xml) like this
android:background="#drawable/button.xml"
Now to change the font color and style you can use the following which comes as part of the styles.xml in the values folder
<style name="buttonStyle" parent="#android:style/Widget.Button.Small">
<item name="android:textColor">#FFFFFF</item>
<item name="android:textSize">12sp</item>
<item name="android:textStyle">bold</item>
</style>
Now call this in the layout XML (mainactivity.xml) like this
style="#style/buttonStyle"
The final code is:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="#string/edit"
android:id="#+id/btnEdit"
android:background="#drawable/button.xml"
style="#style/buttonStyle"
/>
Hope this helps :)
This will change the default button style for the entire app, include alert dialog button. Adjust the style based on yours.
res/values/styles.xml
<resources>
<!-- Your base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Other styles -->
<item name="buttonStyle">#style/MyCustomButton</item>
</style>
<style name="MyCustomButton" parent="Widget.AppCompat.Button">
<item name="android:background">#drawable/selector_button</item>
<item name="android:textColor">#color/buttonText</item>
<item name="android:textStyle">bold</item>
<item name="android:paddingLeft">16dp</item>
<item name="android:paddingRight">16dp</item>
</style>
</resources>
res/drawable/selector_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_selected" android:state_pressed="true"/>
<item android:drawable="#drawable/button_selected" android:state_focused="true"/>
<item android:drawable="#drawable/button_unselected"/>
</selector>
res/drawable/button_selected.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="1000dp"/>
<solid android:color="#color/buttonSelected"/>
</shape>
res/drawable/button_unselected.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="1000dp"/>
<solid android:color="#color/buttonUnselected"/>
</shape>

How to set the text style of a button in selectors?

Button selectors allows to define a background for each state of the button, but how can the button's text style be defined for each state? My objective is to grey out a button when disabled. So I set a greyed out background to the disable state of the button, but the only way I found to also grey out the text is to change it's color dynamically when I disable the button...
Anybody can help?
Thanks
<style name="srp_button" parent="#android:style/Widget.Button">
<item name="android:background">#drawable/btn_default</item>
<item name="android:textColor">#ffffff</item> <!-- How to specify different color for different state? -->
<!-- more stuff -->
</style>
drawable/btn_default.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true"><shape>
<solid android:color="#color/pink" />
<corners android:radius="6dp" />
</shape></item>
<item><shape>
<solid android:color="#000000" />
<corners android:radius="6dp" />
</shape></item>
</selector>
Well!
You can define the Text color for all 4 state, similarly as you defined background for the Button. For example:
file name: /res/color/text_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="#440000"/>
<item android:state_focused="true" android:color="#004400"/>
<item android:state_pressed="true" android:color="#000044"/>
<item android:color="#444"/>
</selector>
Take this file into your Button Style like this:
<style name="srp_button" parent="#android:style/Widget.Button">
<item name="android:background">#drawable/btn_default</item>
<item name="android:textColor">#color/text_color</item> <!-- Here is specified text color -->
</style>

Categories

Resources