I'm trying to set a shadow for my button, but instead of getting shadow for the whole button I'm getting it for button's text only.
I've tried both: using xml styles
<style name="shadowed_button">
<item name="android:shadowColor">#444444</item>
<item name="android:shadowDx">1</item>
<item name="android:shadowDy">1</item>
<item name="android:shadowRadius">9</item>
</style>
and setting shadow programmatically
button.setShadowLayer(9, 1, 1, Color.rgb(44, 44, 44));
Both work identically.
Does anyone know how to do this?
To do so you have to make the image of the button with shadow and place on the button as drawable.
Related
I've added rounded corners to a PopupMenu and it looks good, except now when I press a menu item, I get the ugly effect shown here:
In this example, while my finger is touching the third item, Android adds a default color behind the item, which shows through at the rounded corners. It also adds a rounded background to the item text, which partially covers the "glow" around the radio button.
This is my XML style for rounding the corners of the PopupMenu:
<style name="popover" parent="#android:style/Widget.PopupMenu">
<item name="android:itemBackground">#android:color/transparent</item>
<item name="android:colorBackground">#android:color/transparent</item>
<item name="android:background">#drawable/alert_rounded_dark</item>
<item name="android:textColor">#F9F9F9</item>
<item name="android:textColorSecondary">#F9F9F9</item>
<item name="colorAccent">#F9F9F9</item>
</style>
And alert_rounded_dark.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#2B2B2B" />
<corners android:radius="10dp" />
</shape>
It looks like Android is applying the rounded background style to the subviews of the popup menu, including the menu items and the menu item text. I tried to stop that by setting itemBackground to #android:color/transparent or #null, but that had no effect.
I tried changing the background attribute to windowBackground, popupBackground or panelBackground, but then the whole PopupMenu and its items were transparent.
I had a similar issue when rounding the corners of ListView rows, which I solved with this function of ListView:
this.listView?.setSelector(android.R.color.transparent)
I think this corresponds to the listSelector XML attribute, but I don't see a similar function or attribute in the class reference for PopupMenu, and using the listSelector attribute for a PopupMenu does nothing.
Is there another way to visually clean this up? I think stopping the inheritance of the rounded corner background and removing the pressed color would be best, but addressing either one should help a lot.
UPDATE
I found that changing itemBackground to the same color as the menu background stopped the system color from appearing behind items when pressing them (the four light gray corners you see in the picture), but that killed the rounded corners by adding an opaque, square background to every item. Then I tried setting itemBackground to a drawable selector that sets every state to the alert_rounded_dark drawable, to preserve the rounded corners, but that didn't suppress the system pressed color like setting a background color did.
How do you apply the popover style to your PopupMenu?
If you use a ContextThemeWrapper, you can't directly apply PopupMenu style.
You need to assign your popover style to the popupMenuStyle attribute in the theme overlay. Here is how you can do it:
styles.xml
<style name="ThemeOverlay.App.PopupMenu" parent="">
<!-- if using android.widget.PopupMenu -->
<item name="android:popupMenuStyle">#style/PopupMenu</item>
<!-- if using androidx.appcompat.widget.PopupMenu -->
<item name="popupMenuStyle">#style/PopupMenu</item>
<!-- Other attributes -->
<item name="android:textColor">#F9F9F9</item>
<item name="android:textColorSecondary">#F9F9F9</item>
<item name="colorAccent">#F9F9F9</item>
</style>
<style name="PopupMenu" parent="#android:style/Widget.PopupMenu">
<item name="android:popupBackground">#drawable/alert_rounded_dark</item>
</style>
And use it like so:
val menu = PopupMenu(
// Using "ContextThemeWrapper" for PopupMenu customization
ContextThemeWrapper(context, R.style.ThemeOverlay_App_PopupMenu),
view // Your anchor view
)
menu.inflate(R.menu.your_menu_id)
menu.show()
Here is how it looks on my end:
Bonus:
If you wish to further customize RadioButton inside the PopupMenu, you can add radioButtonStyle attribute to the theme overlay, like so:
<style name="ThemeOverlay.App.PopupMenu" parent="">
...
<item name="radioButtonStyle">#style/YourCustomRadioButton</item>
</style>
How to change the text color of the bottom menu item?
Thanks!!!
change the the text color from values>>Color.xml
<item name="android:ColorPrimary">#000000</item>
<item name="android:textColorPrimary">#000000</item>
<item name="android:attr/textColorPrimary">#000000</item>
you can put you own color code in it. In my case I have put it Black(#000000)
IF this dnt work try to change the Accent Color from the same color.xml
Try the following:
<item name="android:textColorSecondary">#000</item>
<item name="android:textColorSecondaryInverse">#000</item>
Struggled a bit with it, but the above eventually worked (on my KitKat phone not sure about other APIs)
Just remember to apply it to main theme and custom theme.
I've added a couple of simple buttons to a layout and I want to remove the blue shadow/border that apears around the button when I press it. How do I customize the button's apearance on "onpress" action?
there is no way to do this - all "looks" of buttons states are drawables. you can replace them creating own graphics or use Holo generator (deprected, prefering using AppCompat lib)
HERE you have a Selector example and also method for coloring button ("shadow" will stay, but will be in your color matching app style)
ImageView.setColorFilter(Color.argb(255, 93, 93, 93));
This is gray color, you can set according to your color of image background.
try to set Background drawable for your button as:
btn.setBackgroundResource(R.drawable.yellow_button);
yellow_button.xml :
<!-- pressed -->
<item android:drawable="#drawable/button_1_selected" android:state_pressed="true"/>
<!-- focused -->
<item android:drawable="#drawable/button_1_normal" android:state_focused="true"/>
<!-- default -->
<item android:drawable="#drawable/button_1_normal"/>
For my android project I have some default buttons.
To make the app look nicer I try to change the colors with the following code :
<style name="CustomStyle"parent="#android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:buttonStyle">#style/CustomButton</item>
</style>
<style name="CustomButton" parent="android:style/Widget.Button">
<item name="android:background">#color/buttonBackgroundColor</item>
<item name="android:textColor">#color/buttonTextColor</item>
</style>
The color of the buttons are adjusted like I want but all the exisiting default heights and paddings are gone.
How should I adjust the colors of my button without losing the existing padding and height and other default values?
I want to change the style for all buttons in my app in multiple fragments/activitys so doing it programmaticly is not prefered.
Minimum api level that I use is 14, if I used api level 21 or higher I could use backgroundTint instead of background and get the right result but this is not an option now.
Android default button uses a 9 patch drawable as background.
which has some default padding, look at the content area defined by the right and bottom lines.
You can define padding as well in the custom style you have.
<style name="CustomButton" parent="android:style/Widget.Button">
<item name="android:background">#color/buttonBackgroundColor</item>
<item name="android:textColor">#color/buttonTextColor</item>
<item name="android:padding">#dimen/custom_button_padding</item>
</style>
You can do it programatically with :
yourButton.setBackgroundColor(getResources().getColor(Color.parseColor("#FFFFFF")));
or if you want to do it using styles:
1) You should insert a dimension value within the style xml:
<dimen name="buttonHeight">40px</dimen>
2) Reference the dimension value. So in your layout file you'll write:
android:layout_height="#dimen/buttonHeight"
I need to customize ScrollView so it will look like in the picture:
I already successfully customized the Thumb and the Track, but I don't know how to add Arrows like in the picture I provided.
Here is the style I'm using:
<!-- Scrollbar -->
<style name="scroll_view">
<item name="android:scrollbarAlwaysDrawVerticalTrack">true</item>
<item name="android:scrollbars">vertical</item>
<item name="android:fadeScrollbars">false</item>
<item name="android:scrollbarThumbVertical">#drawable/sb_thumb</item>
<item name="android:scrollbarTrackVertical">#drawable/sb_track_vertical_bg</item>
</style>
The Arrows should be a part of the ScrollView style to avoid extra spacing and also they need to be simple, non clickable and e.t.c.
If you want to add buttons for up and down, you can place two buttons above and below of Scrollview.And programatically scroll the scroll bar on button click.
For that purpose, you can use following code,
scrollview.scrollTo(5, 10);