Adding button style to AppTheme not working - android

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.

Related

EditText style background specified in theme doesn't apply to EditTex?

I am creating an EditText like image. (Actually, I made it.)
Created drawable xml and set EditText as background.
However, to manage this more easily, I created a style in the theme and assigned it to the style property in EditText, but it is not applied.
What's the reason?
(Or if there is a better way to create these EditTexts please let me know)
--
drawable
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="5dp"/>
<solid android:color="#color/gray_400"/>
</shape>
theme.xml
<!-- memo EditText style -->
<style name="MemoEditTextStyle" parent="Widget.AppCompat.EditText">
<item name="background">#drawable/edittext_memo</item>
<item name="android:textColor">#color/purple_200</item>
<item name="android:textColorHighlight">#color/gray_400</item>
<item name="colorControlActivated">#color/gray_400</item>
</style>
in xml
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
style="#style/MemoEditTextStyle"
android:hint="#string/hint_memo"
android:paddingLeft="10dp"
android:gravity="center_vertical"/>
result
The background is not applied to EditText because in the style you define a property <item name="background"> instead of <item name="android:background">.
The same result can be also achieved using Widget.MaterialComponents without the need of the drawable xml shape like in the below example:
Define a custom TextInputLayout.OutlinedBox Style:
<style name="Custom.TextInputLayout.OutlinedBox" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<item name="boxBackgroundMode">outline</item>
<item name="boxBackgroundColor">#c2c0ba</item>
<item name="boxStrokeColor">#c2c0ba</item>
<item name="boxStrokeErrorColor">#FF0000</item>
<item name="boxCornerRadiusTopStart">5dp</item>
<item name="boxCornerRadiusTopEnd">5dp</item>
<item name="boxCornerRadiusBottomStart">5dp</item>
<item name="boxCornerRadiusBottomEnd">5dp</item>
<item name="boxStrokeWidth">0dp</item>
<item name="boxCollapsedPaddingTop">0dp</item>
<item name="hintEnabled">false</item>
<item name="errorIconDrawable">#null</item>
<item name="errorTextColor">#FF0000</item>
<item name="android:textColorHint">#B3B3B3</item>
<item name="materialThemeOverlay">#style/Custom.ThemeOverlay.TextInputEditText.OutlinedBox</item>
</style>
<style name="Custom.ThemeOverlay.TextInputEditText.OutlinedBox" parent="#style/ThemeOverlay.MaterialComponents.TextInputEditText.OutlinedBox">
<item name="editTextStyle">#style/Custom.TextInputEditText.OutlinedBox</item>
</style>
<style name="Custom.TextInputEditText.OutlinedBox" parent="#style/Widget.MaterialComponents.TextInputEditText.OutlinedBox">
<item name="android:paddingTop">16dp</item>
<item name="android:paddingBottom">16dp</item>
<item name="android:paddingStart">10dp</item>
<item name="android:paddingEnd">10dp</item>
<item name="android:paddingLeft">10dp</item>
<item name="android:paddingRight">10dp</item>
<item name="android:textColor">#color/white</item>
<item name="android:textColorHint">#color/black</item>
</style>
Usage:
<com.google.android.material.textfield.TextInputLayout
style="#style/Custom.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="MEMO"/>
</com.google.android.material.textfield.TextInputLayout>
Result:

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"/>

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"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</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!
Assuming your backgroundactivity.xml is done correctly, remove .xml extension.
<item name="android:background">#drawable/backgroundactivity.xml</item>
to
<item name="android:background">#drawable/backgroundactivity</item>

Customize back button on ActionBar (Android)

in my app i added back button on ActionBar but this is not visible properly because of dark theme of ActionBar. both ActionBar and back Button are black in color so back button is not visible.
i added this code in values.xml file
<style name="Theme.MyFancyTheme" parent="android:Theme.Holo">
<item name="android:homeAsUpIndicator">#drawable/up</item>
</style>
and set.
minSdkVersion="14"
but it's not showing custom back buttom.
In the res/values/styles.xml file put some custom style:
<style name="AppThemeWithBackButton" parent="android:Theme.Holo.Light">
<item name="android:actionBarStyle">#style/ActionBar</item>
<item name="android:actionMenuTextColor">#color/white</item>
<item name="android:homeAsUpIndicator">#drawable/actionbar_back_button</item>
</style>
<style name="ActionBar" parent="android:Widget.ActionBar">
<item name="android:background">#drawable/actionbar_background</item>
<item name="android:displayOptions">useLogo|homeAsUp|showTitle</item>
<item name="android:titleTextStyle">#style/TitleText</item>
</style>
<style name="TitleText" parent="android:TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textColor">#color/white</item>
<item name="android:textAllCaps">false</item>
</style>
and use drawable/actionbar_back_button.xml layout:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:left="16dp"
android:right="16dp"
android:drawable="#drawable/back_icon" />
</layer-list>
and use drawable/actionbar_background.xml layout.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/action_bar_color" />
</shape>
Above all code is regarding of customizing actinobar button and background. You can modify it as you want. Hope it will help you. Enjoy!!!

Dividers for the actionbar menu items

I am developing Tablet android application. Here i got one issue with the UI of the action bar menu items.
Note: I am using android Native actionbar.
1) How to show divider between menu items like as show in the image.
I tried with divider styles and custom styles but it doesn`t reflect in my action bar.
2) How can I add multiple menu items into a single menu item like as show in the picture.
I tried with custom layout but overflow menu for the items is not showing.
I tried with PopupMenu for the overflow menu but the icon is not visible in overflow list.
Here I was using styles for the action bar application, styles.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/AppTheme.ActionBar</item>
<item name="android:textAllCaps">false</item>
<item name="android:actionMenuTextAppearance">#style/AppTheme.ActionBar.MenuTextStyle</item>
<item name="android:actionOverflowButtonStyle">#style/OverFlow</item>
<item name="android:divider">#color/dividercolor</item>
</style>
<style name="OverFlow" parent="#android:style/Widget.Holo.ActionButton.Overflow">
<item name="android:src">#drawable/ic_actionbar_dots</item>
</style>
<style name="AppTheme.ActionBar" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#drawable/actionbar_background</item>
<item name="android:height">100dip</item>
<item name="android:titleTextStyle">#style/AppTheme.ActionBar.Text</item>
<item name="android:textColor">#color/white</item>
<item name="android:divider">#drawable/divider</item>
<item name="android:showDividers">beginning</item>
<item name="android:dividerPadding">10dp</item>
</style>
<style name="AppTheme.ActionBar.MenuTextStyle" parent="android:style/TextAppearance.Holo.Widget.ActionBar.Menu">
<item name="android:textAllCaps">false</item>
<item name="android:textColor">#color/white</item>
<item name="android:textSize">26sp</item>
</style>
<style name="AppTheme.ActionBar.Text" parent="#android:style/TextAppearance">
<item name="android:textAllCaps">false</item>
<item name="android:textColor">#color/white</item>
</style>
menu.xml
<group android:id="#+id/menu_mainGroup" >
<item
android:id="#+id/connection"
android:icon="#drawable/nw_pt_calc"
android:showAsAction="always"
android:title=" ">
</item>
<item
android:id="#+id/status_menu"
android:actionLayout="#layout/application_status_overflowmenu"
android:showAsAction="ifRoom|withText">
</item>
<!--
<item
android:id="#+id/status"
android:icon="#drawable/ic_available"
android:showAsAction="always"
>
<menu>
<item
android:id="#+id/status1"
android:icon="#drawable/ic_available"
android:showAsAction="always"
android:title="#string/menu_available">
</item>
<item
android:id="#+id/status2"
android:icon="#drawable/ic_busy"
android:showAsAction="always"
android:title="#string/menu_busy">
</item>
<item
android:id="#+id/status3"
android:icon="#drawable/ic_logoff"
android:showAsAction="always"
android:title="#string/menu_logoff">
</item>
</menu>
</item>
<item
android:id="#+id/display_pic"
android:icon="#drawable/ic_person"
android:showAsAction="always">
</item>
<item
android:id="#+id/display_name"
android:showAsAction="always">
</item>
-->
<item
android:id="#+id/menu_search"
android:icon="#drawable/ic_search"
android:showAsAction="always"
android:visible="false">
</item>
<item
android:id="#+id/help"
android:icon="#drawable/ic_help"
android:showAsAction="always"
android:visible="false">
</item>
<item
android:id="#+id/about"
android:showAsAction="never"
android:title="#string/about">
</item>
<!--
<item
android:id="#+id/notification_history"
android:showAsAction="never"
android:title="#string/notification_history">
</item>
<item
android:id="#+id/comm_msg_history"
android:showAsAction="never"
android:title="#string/comm_msg_history">
</item>
-->
</group>
Help will be appreciated. Thanks looking forward to hear answers from geeks :).
I don't know if you have resolved your problem. But I can explain you just in case.
First of all, to customize the divider in your ActionBar, you must to do this in your Theme with the parent ActionButton. You do this:
<style name="AppTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:divider">#color/dividercolor</item>
</style>
<style name="AppTheme.ActionBar" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:divider">#drawable/divider</item>
</style>
Try this way:
<style name="AppTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionButtonStyle">#style/ActionButton</item>
<item name="android:actionBarDivider">#color/blue</item>
<item name="actionBarDivider">#color/blue</item>
</style>
<style name="ActionButton" parent="#android:style/Widget.Holo.Light.ActionButton">
<item name="android:background">#color/blue</item>
</style>
You can read the solution here: Can't change color of actionBar divider and there: ActionBar MenuItem Divider.
Then, you can create a 9-Patch drawable to look like exactly at your image. Try to use something like this image:
The black border indicate where expand your image. I did it on the left/right sides to expand your divider on its height. You can read Draw 9-patch from the Documentation to know how use the Draw 9-Patch tool in the sdk.
Or, you can make it in xml, something like below:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- first line vertical blue -->
<item android:left="1dp">
<shape android:shape="line">
<stroke
android:color="#color/blue"
android:width="1dp" />
</shape>
</item>
<!-- second line vertical white -->
<item android:right="1dp">
<shape android:shape="line">
<stroke
android:color="#color/white"
android:width="1dp" />
</shape>
</item>
</layer-list>
After that, you just have to declare it in your style.xml as:
<item ...>#drawable/blue_divider</item>.
Now you have customize divider (=
Then, your custom layout in ActionBar.
It's possible to do as below:
ActionBar actionBar = getActionBar();
actionBar.setCustomView(R.layout.application_status_overflowmenu);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM|
ActionBar.DISPLAY_SHOW_HOME);
And application_status_overflowmenu.xml will be something like:
<RelativeLayout
android="#+id/theCustomIcon"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_gravity="right" android:gravity="center"
android:paddingLeft="10dp" android:paddingRight="10dp"
android:clickable="true" >
<ImageView
android:id="#+id/blockCustomIcon"
android:layout_width="50dp" android:layout_height="50dp"
android:src="#drawable/green_block"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true" />
<ImageView
android:id="#+id/imgCustomIcon"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:src="#drawable/green_block"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/blockCustomIcon" />
<TextView
android:id="#+id/txtCustomIcon"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:src="#drawable/green_block"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:gravity="left" />
</RelativeLayout>
And finally, you can have access to the Views to update the items:
TextView text = (TextView) actionBar.getCustomView().findViewById(R.id.txtCustomIcon);
text.setText("Emily Nichols");
That's all!
If your menu options disappears, check if your onCreateOptionsMenu is inflating a good layout and if it return true. You can also add the custom item in front of options menu or add item dynamically. I let you do this as you want.
I hope this will be useful and it will helpful too.

Categories

Resources