Customize back button on ActionBar (Android) - 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!!!

Related

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.

Styling popup menu item's

I am trying to style the items of my popup so they look like a list of buttons below eachother. The only problem is that I can't get to change anything of the popup items. I have tried to set a global popupMenuStyle in my app style but that didn't to anything. I tried to set an actionLayout on the menu items but still no change.
How can I change the styling of my popup menu items?
My menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/test1"
android:title="Test" />
<item android:id="#+id/test2"
android:title="Test 2" />
</menu>
How I open the popup menu:
PopupMenu popupMenu = new PopupMenu(getContext(), mButton);
popupMenu.getMenuInflater().inflate(R.menu.popup_menu, popupMenu.getMenu());
popupMenu.show();
Try this as part of your styles.xml file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:popupMenuStyle">#style/PopupMenu</item>
<item name="android:textAppearanceLargePopupMenu">#style/myPopupMenuTextAppearanceLarge</item>
<item name="android:textAppearanceSmallPopupMenu">#style/myPopupMenuTextAppearanceSmall</item>
</style>
<style name="PopupMenu" parent="#android:style/Widget.PopupMenu">
<item name="android:popupBackground">#FFFFFF</item>
<item name="android:divider">#444444</item>
<item name="android:dividerHeight">1px</item>
<item name="android:background">#FFFFFF</item>
</style>
<style name="myPopupMenuTextAppearanceSmall" parent="#android:style/TextAppearance.DeviceDefault.Widget.PopupMenu.Small">
<item name="android:textColor">#000000</item>
<item name="android:textSize">12sp</item>
<item name="android:background">#FFFFFF</item>
</style>
<style name="myPopupMenuTextAppearanceLarge" parent="#android:style/TextAppearance.DeviceDefault.Widget.PopupMenu.Large">
<item name="android:textColor">#000000</item>
<item name="android:textSize">18sp</item>
<item name="android:background">#FFFFFF</item>
</style>
</resources>
and then in your xml layout file for the activity add this line:
style="#style/AppTheme"
or in your AndroidManifest.xml file, add this to the application tag:
android:theme="#style/AppTheme"
This will affect how Android renders a popup menu in your app.

Button style working in Android 5 but not Android 4

I have a button style with a blue background that works fine in API 22, but the same button appears in dark grey without the applied style in Android 4. This is my style:
<style name="MyApp.Plain" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">#drawable/background</item>
<item name="android:buttonStyle">#style/MyApp.Widget.Button</item>
</style>
<style name="MyApp.Widget.Button" parent="#android:style/Widget.Button">
<item name="android:background">#drawable/btn_blue</item>
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">14sp</item>
<item name="android:textColor">#fff</item>
<item name="android:gravity">center_vertical|center_horizontal</item>
</style>
My btn_blue.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn_disabled" android:state_enabled="false"/>
<item android:drawable="#drawable/btn_pressed" android:state_enabled="true" android:state_pressed="true"/>
<item android:drawable="#drawable/btn_normal_blue" android:state_enabled="true"/>
</selector>
and btn_normal_blue.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="90"
android:endColor="#368ac6"
android:startColor="#0e58a4" />
<corners android:radius="20dp" />
</shape>
What could be the reason for this behavior and how can I fix this?
EDIT: This does not work with support v7:22.2.0, but does work with v7:21.0.3. I didn't change anything besides the dependency and changed AppCompatActivity to ActionBarActivity.
Possibly this is an Android bug.
Try buttonStyle instead of android:buttonStyle, since this is AppCompat attribute pre Lollipop, so it should be without android prefix.
I have seen some layouts where the attributes are applied twice, with and without the Android prefix. Can you try this:
<style name="MyApp.Widget.Button" parent="#android:style/Widget.Button">
<item name="android:background">#drawable/btn_blue</item>
<item name="background">#drawable/btn_blue</item>
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">14sp</item>
<item name="android:textColor">#fff</item>
<item name="android:gravity">center_vertical|center_horizontal</item>
</style>
I am currently trying to find where I saw this previously, and why it worked. Let me know if it did.
Try to use AppCompat:
<style name="MyApp.Widget.Button" parent="Base.TextAppearance.AppCompat.Button">
<item name="android:background">#drawable/btn_blue</item>
...
</style>
#Michał Kisiel answer is correct "Try buttonStyle instead of android:buttonStyle, since this is AppCompat attribute pre Lollipop, so it should be without android prefix."
I just want to add that if you for instance create a view programatically you should put android:buttonStyle and buttonStyle too, sample:
<item name="android:buttonStyle">#style/OrangeButton</item>
<item name="buttonStyle">#style/OrangeButton</item>

change the android actionbar submenu text color

How to change the text color of submenu in Android? I customize the application theme and override the relative attributes, but it still doesn't work. My menu has two submenus, which are initially hidden, when clicked, it shows. However, the style of submenus can't be modified, while the title of action bar can. This question bothers me all the day, I almost try every ways I find.
This is my code!
menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.classsignin.MainActivity" >
<item
android:id="#+id/action_overflow"
android:title="分享"
android:icon="#drawable/drop_select"
android:showAsAction="always"
>
<menu >
<item
android:id="#+id/absent"
android:title="请假"
android:icon="#drawable/absent"
/>
<item
android:id="#+id/refresh"
android:title="刷新课程"
android:icon="#drawable/refresh"
/>
</menu>
</item>
styles.xml
<style name="CustomTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/MyActionBarTheme</item>
<item name="android:actionMenuTextAppearance">#style/MyActionBarMenu</item>
<item name="android:actionMenuTextColor">#color/blue</item>
<item name="android:homeAsUpIndicator">#drawable/back</item>
<item name="android:spinnerItemStyle">#style/MySpinnerItem</item>
</style>
<style name="MyActionBarTheme" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#color/white</item>
<item name="android:titleTextStyle">#style/MyActionBarTitle</item>
</style>
<style name="MyActionBarTitle" parent="#android:style/TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textSize">16sp</item>
<item name="android:textColor">#color/blue</item>
</style>
<style name="MyActionBarMenu" parent="#android:style/TextAppearance.Holo.Widget.ActionBar.Menu">
<item name="android:textSize">16sp</item>
<item name="android:textColor">#color/blue</item>
</style>
<style name="MySpinnerItem" parent="#android:style/Widget.Holo.TextView.SpinnerItem">
<item name="android:textAppearance">#style/MyTextAppearance</item>
</style>
<style name="MyTextAppearance" parent="#android:style/TextAppearance.Holo.Widget.TextView.SpinnerItem">
<item name="android:textColor">#color/blue</item>
</style>
Essentially you will want to customise your MenuItem's style by having the android:itemBackground attribute overridden with your custom color/drawable selector in your style.xml and as well a vtextColor` attribute to override.
In case your menu has also submenu's, then you will want as well to style the header title of the submenu, which usually is automatically with White background by overriding the attribute actionBarPopupTheme with your custom style.
style.xml
<resources>
<style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
<item name="android:itemBackground">#drawable/menu_popup_selector</item>
<item name="actionBarPopupTheme">#style/SubmenuHeaderStyle</item>
</style>
<style name="SubmenuHeaderStyle" parent="ThemeOverlay.AppCompat.Light">
<item name="android:colorBackground">#color/colorPrimary</item>
<item name="android:textColor">#color/colorAccent</item>
</style>
</resources>
menu_popup_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<color
android:color="#color/colorPrimary"/>
</item>
<item>
<color
android:color="#655611"/>
</item>
</selector>
And you will have something like those screenshots
(1st menu and then the submenu after clicking one item on the 1st menu - the pink header is my submenu header).
Just for you to know when I have the normal menu without submenu would look like this hierarchy views:
<menu>
<item/>
<item/>
<item/>
</menu>
and a menu with submenu as this example of hierarchy views:
<menu>
<item/>
<item/>
<group>
<item>
<menu>
item
item
item
</menu>
</item>
</group>
<item/>
<item/>
</menu>

How I make a logo and actionBar transparent?

I am using a .png for both my logo and action Bar menu items. I am using the android.Holo.light theme.
Even though I've made the background transparent for both the logo and the menu items, a grey shaded area is still showing behind the .png. How do I change this?
Take a look at my menu XML and my style override XML below.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="android:Theme.Holo.Light">
<item name="android:selectableItemBackground">#color/transparent</item>
<item name="android:background">#00000000</item>
</style>
<style name="ActionBar" parent="#android:style/Widget.Holo.Light.ActionBar" >
<item name="android:selectableItemBackground">#color/transparent</item>
<item name="android:background">#00000000</item>
</style>
<style name="textTitle"
parent="#android:style/TextAppearance">
<item name="android:textSize">20dp</item>
<item name="android:textColor">#DC0451</item>
<item name="android:textStyle">bold</item>
</style>
<style name="textDescription"
parent="#android:style/TextAppearance">
<item name="android:textSize">15dp</item>
<item name="android:textColor">#666666</item>
</style>
</resources>
Menu xml
<item
android:id="#+id/Category"
android:title="Category"
android:showAsAction="always|withText"
android:icon="#drawable/menuitemselect"
style="#style/ActionBar"
>
<menu>
<item
android:id="#+id/catalog"
android:title="Main Catalog"
android:icon="#drawable/menuitemselect"
android:onClick="catalogClick"
/>
<item android:id="#+id/newvideos"
android:title="New"
android:icon="#drawable/menuitemselect"
android:onClick="newVideoClick"
style="#style/ActionBar"
/>
<item
android:id="#+id/popularvideos"
android:title="Popular"
android:icon="#drawable/menuitemselect"
android:onClick="popularVideoClick"
style="#style/ActionBar"
/>
</menu>
</item>
<item android:id="#+id/backButton"
android:title="Back"
android:showAsAction="ifRoom"
android:icon="#drawable/ic_launcher"
/>
</menu>
Thank you
I found that the issue was with :
<item name="android:selectableItemBackground">#color/transparent</item>.
I added it in an attempt to get rid of default highlight feature. It seems when I have this set, the grey background shows up. When I remove it, it acts as it should.

Categories

Resources