Android: button onpress remove shadow/border - android

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

Related

How to remove the default background color for pressed PopupMenu items?

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 Android button click color

I have one button. I have added background image to my button using background attribute. Now when I click on button, I am not getting the default orange color. How can I get that color.
One more query, In the above scenario how can I change the default orange color to some other color.
Try this http://developer.android.com/guide/topics/ui/controls/button.html#Style, change state of your button
You have to make your button custom to do that changes for that create an xml file named custom_btn in yoyr drawable folder and paste the code in it as follows:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="#drawable/tick_btn" /> <!-- pressed -->
<item android:drawable="#drawable/untick_btn" /> <!-- default -->
</selector>
and in the button you have to add android:button="#drawable/custom_btn"
// try this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="hover color or drawable" android:state_pressed="true"></item>
<item android:drawable="hover color or drawable" android:state_focused="true"></item>
<item android:drawable="color or drawable" android:state_enabled="true" android:state_focused="false" android:state_pressed="false"></item>
<item android:drawable="hover color or drawable" android:state_enabled="false"></item>
</selector>
if you're just trying to see the "default orange color" then use an ImageButton and apply your drawable to the src (rather than the background). You will then see the native image background behind your image and it will continue to do whatever it was doing before.
I am not getting the default orange color. How can I get that color.
When you add a background to a button it no more remains a default raw button , It becomes a custom button as you have rendered the default behavior by adding some background to it. Now you need to add color to your custom button on your own because the OS deals with only raw buttons not custom.
How can I change the default orange color to some other color.
To change the button state after its pressed can be done in two ways
1) Either add a background image.
2) or Add a xml to the button.
This is a very nice tutorial on Customizing Android buttons.

How to change the color of a focused EditText when using "android:Theme.Holo.Light"?

I Am using "android:Theme.Holo.Light" in my application, it gives blue as the Default color.
I want to have a EditText when focused like below.
By default it comes like this when applying holo.light theme.
You can use Holo Color generator for custom colors.
http://android-holo-colors.com/
You don't need to download all controls. Only these you want.
May this help you:
You will have to create/modify your own NinePatch image to replace the default one, and use that as the background of your EditText. If you look in your SDK folder, under your platform, then res/drawable, you should find the NinePatch image for the EditText focus state. If that's all you want to change, you can just pull it into Photoshop, or whatever image editing software you have, and change the orange color to a color of your choosing. Then save that into your drawable folder.
For example:
edittext_modified_states.xml
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:state_pressed="true"
android:drawable="#android:drawable/edittext_pressed"
/> <!-- pressed -->
<item
android:state_focused="true"
android:drawable="#drawable/edittext_focused_blue"
/> <!-- focused -->
<item
android:drawable="#android:drawable/edittext_normal"
/> <!-- default -->
</selector>

Android view state_pressed Blue background

I have a lot of custom views and I have style for state_pressed. Basically its a rectangle with
solid android:color="#DC2D5A8C"
What I am trying to do is simulate the blue background color that comes with the standard views/controls. For example: when you click on a button or list view item, the background changes to blue (on_pressed).
I got that to work with the above style, but the problem is let's call it the tint effect. In a button, the text caption is black. When you press, the background is blue and the text color changes to white.
Now how can I achieve such a so called "tint" change in my custom control's view?
Your response is much appreciated.
Thanks!
You can use selector xml file to do this.You must be setting background to the button.Instead of that set the xml file to its background.Create selector.xml file in your drawable folder as shown below and set that xml file as background to that button just like : android:background="#drawable/selector"
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use blue -->
<item android:drawable="#drawable/btn_blue"
android:state_pressed="true" />
<!-- When not selected, use black-->
<item android:drawable="#drawable/btn_black"/>
</selector>
By doing this you will get so called tint effect to your button.Hope this will help you.

How can I change the android button to dark grey instead of light grey

I would like to change the color of the android button in my application to be dark grey instead of light grey. I intent to keep the color of the focus/pressed the same. I just want to change the color of the button in 'normal' state to dark grey.
I have found this thread:
Standard Android Button with a different color
I think the easiest way is to do this. Is that correct? But how can I make the LightingColor Filter to make it darkgrey?
button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xFFAA0000));
I have tried
button.getBackground().setColorFilter(new LightingColorFilter(0xffffffff, 0xFF3A3A3A));
But all I get is a white color.
That's similar to what I am doing in my TabHost tabs in XML. If you have custom buttons and graphics it can be done in Java, but it is much easier if you're able to do it in your XML.
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="#drawable/settings"
android:state_selected="true" />
<!-- When not selected, use white-->
<item android:drawable="#drawable/mountain" />
</selector>
You could use the default light theme
Example

Categories

Resources