Changing actionBar dropdown background color - java

How do i change the color of the background where it says "add contact" and "about". Now its kind of grayish, but i want it to be white! This is a actionBar with dropdown, not a spinner. And im not using that actionbarsherlock thing.

For example, in you exisiting parent Style definition, add just the android:popupMenuStyle attribute as shown below:
<style name="Theme.Example" parent="#android:style/Theme.Holo.Light">
<item name="android:popupMenuStyle">#style/PopupMenu.Example</item>
.......
.......
</style>
And override the android:popupMenuStyle attribute by defining your style:
<style name="PopupMenu.Example" parent="#android:style/Widget.Holo.Light.ListPopupWindow">
<item name="android:popupBackground">#drawable/menu_dropdown_panel_example</item>
</style>
The #drawable/menu_dropdown_panel_example is this:
You can either choose to use an image similar to the one above, or use an Color resource.
And this is for the default ActionBar. Not the ActionBarSherlock. ;-)
ActionBar styles can be easily created using this cool website: http://jgilfelt.github.com/android-actionbarstylegenerator/. I usually experiment a little here before deciding on the style. Plus, it lets you download the styles and all necessary resources. Just plug them in your app and you are good to go. :-)
UPDATED: I think I caused some confusion which has resulted in the loss of the shadow on your app's drop-down. You may have replaced all the <items> in your Style that I listed in the original suggestion. The edit should fix that.
UPDATE 2:
Use these image resources instead of the #android:color/white value you are currently using. These are in the order of XHDPI, HDPI and MDPI. Save them and use them in the Style definition.

Try to get styles from ActionBarStyleGenerator service. It has preview and makes all necessary drawable, xml and styles.
There you can easily find and change required color and then find it in sources (in your case it is android:popupBackground).

You can simple override itemBackground item in your theme:
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="android:itemBackground">#color/skyBlue</item>
</style>

Related

Best practice to place menu Icons on the left side of the navigation drawer in Persian,Hebrew devices?

I am trying to have a navigation drawer that opens from the right. ( i managed to do that using LAYOUT_GRAVITY="END").
Now I want all the menu items to start from the right and also their Icons on the right too.
To do that I added (SUPPORTSRTL="TRUE") to the manifest and that also solved the problem. But here is the problem:
if the device's language is set to English, everything works perfectly.
But if the device's language is set to Persian(which is a RTL language) then everything else in the app will be right to left. That means what ever I put in the right goes to left and what ever that is set to the left goes to the right. I even tried using "start" and "end" instead of "right" and "left" but that didn't solve the problem.
To solve that I had to set the (LAYOUTDIRECTION="LTR") to the root element of layouts on every one of my actitives. That solved the problem and now everything is in the position I desired, however this doesn't seem like a sustainable solution to add this line of code to root element of each activity I will ever add to my app.
Can you please tell me if this is the best way to do it or not?
I found other questions with the same topic with no accepted proper answers.
this line of code should solve your problem
ViewCompat.setLayoutDirection(drawer, ViewCompat.LAYOUT_DIRECTION_RTL);
drawer is a reference to your DrawerLayout.
an other way to force RTL layout to activities is using styles xml. this is how i whould do it:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- 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="ShaliRTL" parent="AppTheme">
<item name="android:layoutDirection">rtl</item>
</style>
</resources>
then in android manifest apply this new style (ShaliRTL) to your specific activity:
<activity
android:name=".YourActivity"
android:label="#string/title_activity_settings"
android:theme="#style/ShaliRTL"
android:screenOrientation="fullSensor" />
good luck.

Declare a common onClick attribute for all buttons in styles.xml file

My layout involves a lot of Buttons with all attributes absolutely same except the android:text="button_name" attribute.
The theme for these buttons is defines as follows in styles.xml
<style name="Core.ButtonStyleSmall">
<item name="android:layout_width">16sp</item>
<item name="android:layout_height">16sp</item>
<item name="android:layout_marginLeft">8sp</item>
<item name="android:layout_marginBottom">6sp</item>
<item name="android:textSize">10sp</item>
<item name="android:onClick">takeToApplication</item>
</style>
What I want to achieve is a common onClick statement for all of these buttons and thus I have added it here but it doesn't seem to work.
In my java file it still prompts a warning saying Method'takeToApplication(android.view.View)' is never used.
What is the correct way to do this? Please help me regarding this.
Since no one had a definite answer, I will go ahead and answer it myself:
Any xml attribute can be declared in the styles.xml file for all the views that need to share any common attributes. Thus, onClick works just fine when declared there.
It gives you a warning beforehand saying Method 'name_of_method' is never used, but that is just because the call to the resources files happen at runtime. So the warning can be safely ignored, it renders no problems.

ActionBar three-dot dropdown opens at the wrong place

I've implemented an ActionBar with the com.android.support:appcompat-v7:22.1.1 library. Here you can see the code and my question:
ActionBar with appcompat library v7 (ava.lang.IllegalStateException: You need to use a Theme.AppCompat theme)
That works, but I have one last problem. After changing the ActionBar to the new library, the settings dropdown opens into the actionbar and not among it (see image) and the background of the settings dropdown is grey instead of white?
How can I solve that?
Answering the first part of your question, the placement/location of the dropdown menu is supposed to be that way according to the Material Design guidelines, so that's not a bug.
Concerning the second part, to change the color of the dropdown menu to white, in styles.xml, add the following lines:
<style name="AppTheme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:popupMenuStyle">#style/popupMenuStyle</item>
</style>
<style name="popupMenuStyle" parent="#android:style/Widget.PopupMenu">
<item name="android:popupBackground">#FFFFFF</item>
</style>
And in your AndroidManifest.xml, replace:
android:theme="#style/Theme.AppCompat.Light.DarkActionBar">
with:
android:theme="#style/AppTheme">

Editing XML Style Sheets Changes Buttons in Android

I'm working on an Android app and decided I wanted to change the colors across the whole app, so I used an xml style sheet. This changed my colors, but it didn't change my EditText, TextView, and Button colors, so I had to make an individual style sheet for each element and then call those sheets from within the main color sheet I was using.
However, once I did that, the colors acted appropriately for the three widgets, but the boxes shrunk and disrupted our entire layout. We believe it's because we had overridden the default styles for those, but I thought the way it was written, we were merely overriding the color options.
I'd like to figure out how to change the colors of EditText, TextView, and Button, but not change their formatting.
The Styles.xml for our project:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<color name="oldblue">#33b5e5</color>
<color name="gungrey">#9C9C9C</color>
<color name="textGold">#ffe27e</color>
<color name="backgroundBlack">#000000</color>
<color name="buttonGrey">#222222</color>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="android:textColor">#color/textGold</item>
<item name="android:windowBackground">#color/backgroundBlack</item>
<item name="android:colorBackground">#color/backgroundBlack</item>
<item name="android:editTextStyle">#style/TextEditStyle</item>
<item name="android:textViewStyle">#style/TextViewStyle</item>
<item name="android:buttonStyle">#style/OurButtonStyle</item>
</style>
<!-- Allows us to edit the EditText field -->
<style name="TextEditStyle" parent="android:Widget.EditText">
<item name="android:background">#e3e3e3</item>
</style>
<!-- Allows us to edit the TextView words (much of the app text) -->
<style name="TextViewStyle" parent="android:Widget.TextView">
<item name="android:textColor">#color/textGold</item>
</style>
<!-- Allows us to edit the Buttons in the app -->
<style name="OurButtonStyle" parent="android:Widget.Button">
<item name="android:textColor">#color/textGold</item>
<item name="android:background">#color/buttonGrey</item>
<item name="android:padding">10dp</item>
<!-- This line here helped us fix the issue with the buttons colliding with the textboxes, but ideally, we want to change a single thing and fix all the widgets and not have to use padding here -->
</style>
</resources>
Our app is using Linear Layout on all of our pages. We would like to keep it this way if possible.
Please let me know if you need to see any screenshots or xml code of the individual pages. They're all having the same error, though.
Thanks for any help you can give!
Instead of android:, try prefacing your parent references with #android:style/.
When you're modifying the background attribute for aButton or EditText, you're replacing the entire background that already exists which is why you're losing all the "formatting".
So with a Button, the default buttons you see are actual images.
For example, btn_default_normal_holo.9.png located in (path-to-sdk)\platforms\android-19\data\res\drawable-xhdpi
One way is to create 9 patch images to create your background. You can refer to the answer by Basim Sherif here. In that question, it asks a similar question to yours. So in this case you can use the existing images and modify the colors.
Otherwise, you will need to set your own styles (drawables and selectors) to "match" the original formatting or using your own formatting.
Also note that since you set the background of your buttons to a color, there aren't any selectors anymore (like changing the color when the button is selected).

ActionSherlockBar Styling - PopupMenu

Styling is a place for improvement in my knowledge of the android platform. Given I've being trying to make a simple change all morning and been unable to achieve it.
I want to change the style of the overflow menu in the action bar. I have been able to change the background color, and add animations, so that when clicked it fades to a selected colour. But for the life of me I cannot change the text colour.
Below are two screenshots, one using the Theme.Sherlock and the other using Theme.Sherlock.Light.
Theme.Sherlock - This is my current setting which I have customised. What I want is the overflow (aka android:popupmenu) to be in the holo theme (as below), and ideally so that I can change the text colour.
Theme.Sherlock.Light - This theme shows the style of overflow menu that I want to see. However, as highlighted in red, the overflow button is the wrong colour, and this theme also draws a horizontal line.
I don't know whether it is best/easier to change Theme.Sherlock with the popup issue, or Theme.Sherlock.Light with the overflow icon and line issue. The application is of a "Light" theme so option two is probably the long term better choice, however i'll accept an answer that can solve the problem by changing either theme.
p.s. Where do you find a list of attributes for changing theme/styles, auto-complete is very incomplete in eclipse.
Update
This is actually an update for native action bars as im back to the same problem, but no longer supporting 2.3 or below.
<style name="AppTheme" parent="android:Theme.Holo.Light">
<item name="android:actionOverflowButtonStyle">#style/OverFlow</item>
</style>
<style name="OverFlow" parent="#android:style/Widget.Holo.ActionButton.Overflow">
<item name="android:src">**Insert your icon here**</item>
</style>
This answer is probably coming too late, but to those who may stumble across this question. I find it interesting that you were able to change the overflow's background color. I found that extremely difficult to figure out whereas changing the text was pretty straight forward. Just override the android:itemTextAppearance value in your custom theme.
<item name="android:itemTextAppearance">#style/your_new_text_appearance</item>
<style name="your_new_text_appearance">
<item name="android:textColor">#color/red</item>
</style>
I'm not familiar with ActionBarSherlock, but you can find the standard OverFlow icons on your harddisk in the folder
path\to\android\android-sdk\platforms\android-api-level\data\res\drawable-resolution
and they are called ic_menu_moreoverflow_normal_holo_dark.pngand ic_menu_moreoverflow_normal_holo_light.png
Alternatively you can find them online here.
I believe that the left circle is a drawable, too. Look inside the res\drawable.
Simple do the following:
1. In the theme.xml in your app theme style add this item:
<item name="popupMenuStyle">#style/PopupMenu</item>
2. In styles.xml add this new style:
<style name="PopupMenu" parent="#style/Widget.Sherlock.Light.ListPopupWindow">
<item name="android:textSize">14sp</item>
</style>
In the style you may override text size or other parameters as you want.
Try to override style of Theme.Sherlock with help of Android Action Bar Style Generator

Categories

Resources