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

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>

Related

Tab Indicators Obscured by android:background

Simple TabLayout. No tab indicator. Arggh!
<com.google.android.material.tabs.TabLayout
android:id="#+id/my_tabs"
style="#style/MyTabLayout"
android:theme="#style/MyThemeOverlay"
android:layout_width="match_parent"
android:layout_height="50dp"/>
<style name="MyTabLayout" parent="Widget.MaterialComponents.TabLayout">
<item name="android:background">#color/red</item>
<item name="tabTextAppearance">#style/MyTextAppearance</item>
<item name="tabMinWidth">100dp</item>
<item name="tabMaxWidth">100dp</item>
<item name="tabIndicator">#drawable/tab_indicator_rounded_top_corners</item>
<item name="tabIndicatorColor">#color/color_white</item>
<item name="tabIndicatorHeight">5dp</item>
<item name="tabIndicatorFullWidth">false</item>
<item name="tabTextColor">#color/color_accent_dark</item>
<item name="tabGravity">center</item>
</style>
<style name="MyThemeOverlay">
<item name="android:background">#color/blue</item>
</style>
tab_indicator_rounded_top_corners.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<corners
android:topLeftRadius="2dp"
android:topRightRadius="2dp" />
</shape>
</item>
</layer-list>
And this is what pops out. There should be a white bar under the selected tab.
I am almost sure that this relates to this line in the theme overlay:
<item name="android:background">#color/blue</item>
Because when I do not override the background color, I can see the tab indicators. Problem is that I need to change that background color in some cases using theme overlays.

Adding button style to AppTheme not working

I've tried to add a custom button style to my AppTheme and it is not working. As a reference, I've added a Spinner style to my AppTheme and it works fine. So, I just need to find out where I'm going wrong with my button style.
style.xml:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:spinnerStyle">#style/mySpinnerStyle</item>
<item name="android:buttonStyle">#style/myButtonStyle</item>
<item name="buttonStyle">#style/myButtonStyle</item>
</style>
<style name="mySpinnerStyle" parent="#style/Widget.AppCompat.Spinner">
<item name="overlapAnchor">true</item>
<item name="android:background">#drawable/spinner_background</item>
<item name="android:padding">5sp</item>
</style>
<style name="myButtonStyle" parent="#style/Widget.AppCompat.Button">
<item name="android:background">#drawable/button_states</item>
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
</style>
I can add my button_states.xml file if needed, but know that the style works if I insert my style directly to a button attribute in my layout like below:
<Button
android:id="#+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="4"
style="#style/myButtonStyle"
android:textSize="#dimen/text_size"
android:text="OK" />
Declaration in Parent layout:
android:theme="#style/AppTheme"
So, why does it work directly in the button attribute but not through my AppTheme?
1- Create new DRAWABLE RESOUCE FILE by right click on res / drawable file in your android studio
2- write code like this in it :-
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
android:radius="100dp"
/>
<solid
android:color="#FFFFFF"
/>
<size
android:width="100dp"
android:height="100dp"
/>
</shape>
3- open your XML file which contain this button and change background of button to the drawable you created before
Thanks to Ahmad Sattout for the answer.
In my button style I removed the #style from the parent:
original:
<style name="myButtonStyle" parent="#style/Widget.AppCompat.Button">
<item name="android:background">#drawable/button_states</item>
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
</style>
changed:
<style name="myButtonStyle" parent="Widget.AppCompat.Button">
<item name="android:background">#drawable/button_states</item>
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
</style>
If anyone can explain in further detail why that happens, it would be greatly appreciated.

Android Gradient Background drawable does not work

I've created this drawable resource, backgroundactivity, to use as my app's background:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<item
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<shape
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<gradient
android:angle="90"
android:startColor="#BFEFFF"
android:centerColor="#B0E2FF"
android:endColor="#82CFFD"
android:type="linear"/>
</shape>
</item>
</selector>
I want to set this background on all my activity, so I've tried to put this drawable resource in the styles.xml like this:
<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>
<item name="android:actionBarStyle">#style/MyActionBar</item>
<item name="android:background">#drawable/backgroundactivity</item>
</style>
<style name="MyActionBar" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="#android:background">#color/colorPrimary</item>
</style>
</resources>
But in this way it didn't work: the background still stay white as default. I've also tried to replace android:background with android:windowBackground, but it causes my application to be completely black.
Thanks in advance!
Try to use windowBackground instead of background
<item name="#android:windowBackground">#color/colorPrimary</item>
It worked by simply editing the background activity.xml to the following:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:angle="90"
android:startColor="#BFEFFF"
android:centerColor="#B0E2FF"
android:type="linear" />
</shape>
</item>
</selector>

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>

apply custom theme in activity for every edit texts

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.

Categories

Resources