Android - Styling Button Image View - Removing Blue Highlight On Click - android

I have designed my own graphics composed of two images for an Image Button - one focused and one unfocused. I have the following button in the layout and XML file for it in drawable folder:
<ImageButton
android:id="#+id/btnPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
style="?android:attr/borderlessButtonStyle"
android:src="#drawable/btn_play" />
.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/ic_playfocused" />
<item android:state_focused="true" android:drawable="#drawable/ic_playfocused" />
<item android:state_selected = "true" android:drawable = "#drawable/ic_playfocused" />
<item android:drawable = "#drawable/ic_playdefault" />
</selector>
When I click the button, it switches the two images properly, but the problem is that I also see a partially transparent blue rectangle that normally highlights button clicks. How can I get rid of this blue highlight so that when my button is clicked, the only thing that happens is the switch between the two images?
Thank you in advance :)

Ok so just set the button background to transparent, worked for me just now.
<ImageButton
android:id="#+id/btnPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#android:color/transparent"
style="?android:attr/borderlessButtonStyle"
android:src="#drawable/btn_play" />

Related

Android: How to use selector with a single item

I have a ToggleButton with text and I want to show a drawableStart next to it only if it's selected. When it isn't selected, I'd like to show just the text (the drawable should disappear).
I tried setting up my selector with just a single item for when android:state_checked=true but when I run the app it gets completely ignored. How can I do this?
Here's my code ToggleButton code:
<ToggleButton
android:id="#+id/list_item_service_list_add_button"
style="#style/OrangeToggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:textOff="#string/list_item_service_list_add"
android:textOn="#string/list_item_service_list_added"
android:button="#android:color/transparent"
android:drawableStart="#drawable/add_to_cart_icon_selector"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
and here's my add_to_cart_icon_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#drawable/ic_check_circle"
android:state_checked="true" />
</selector>

Android - how to implement elevation/z-order in AbsoluteLayout?

I'm new to Android programming and want to create my own piano app.
I use absoluteLayout with dp - or is there a better way?
I have buttons declared like this
<Button
android:id="#+id/d3"
android:layout_width="55dp"
android:layout_height="200dp"
android:layout_x="55dp"
android:background="#drawable/selector_button"
android:scrollbars="horizontal"
android:gravity="bottom|center_horizontal"
android:paddingBottom="20dp"
android:textSize="60dp"
android:textColor="#ff4444" />
selector_button
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/white_pressed"
android:state_pressed="true" />
<item android:drawable="#drawable/key" />
</selector>
The problem is that the white key when pressed down draws over the black one
here's a screenshot
I've tried the bringToFront-method on the black key, an the elevation option on it, but haven't been able to solve the problem.
Do you have a hint?

How to remove extra padding or margin in material design button?

I am trying to create an button which is attached to the TextView above the button as shown in the below image.
The above screenshot is taken from the Note 4 and the OS version is 5.0.1.
Below is the code is used to achieve the UI.
layout/xyz.xml
<Button
android:layout_width="250dp"
android:layout_height="50dp"
android:theme="#style/myButton"
android:text="Cancel"/>
values-v21/style.xml
<style name="myButton" parent="#style/Base.Widget.AppCompat.Button">
<item name="android:colorButtonNormal">#3578A9</item>
<item name="android:inset">0dp</item>
</style>
But if I run the same code in Nexus4 OS verison 5.1.1, the button is taking the margin for all the 4 sides and the screenshot looks like below.
If I remove the "android:theme" and provide the "android:background", the UI looks like the first image. But It won't give the ripple effect. So how the achieve the UI as first image with the ripple effect.
android:insetBottom="0dp"
<com.google.android.material.button.MaterialButton
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="56dp"
android:text="Edit"
app:cornerRadius="0dp"
android:insetBottom="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
Step 1: Put the below code in styles.xml
<style name="myColoredButton">
<item name="android:textColor">#FF3E96</item>
<item name="android:padding">0dp</item>
<item name="android:minWidth">88dp</item>
<item name="android:minHeight">36dp</item>
<item name="android:elevation">1dp</item>
<item name="android:translationZ">1dp</item>
<item name="android:background">#FF0000</item>
</style>
Here you can change the textColor( I have used #FF3E96 above) and background color (I have used #FF0000) for your button. You can also override textColor values from your Button related layout xml by using android:colorButtonNormal.
Step 2:Create a new XML file under drawables folder and add the following code: I named my XML file as primary.xml
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/colorPrimary">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="1dp" />
<solid android:color="#8B8386" />
</shape>
</item>
</ripple>
Step 3: Use the style and drawable in your Button as follows.
<Button
style="#style/myColoredButton"
android:layout_width="250dp"
android:layout_height="50dp"
android:text="Cancel"
android:gravity="center"
android:background="#drawable/primary_round"
android:colorButtonNormal="#3578A9" />
Hope it solves your problem.
I also had issue related to top and bottom spacing for Apply button as you can see above image.
I wanted to show button with uniform background without any spacing as you can see for "Reset" button.
But I have to set both property button background as well as background Tint to same color to achieve this.
If I reuse this buttons at more than one place then I have to programmatically change 2 property to change background color of button.
You can try below solution:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.google.android.material.button.MaterialButton
android:id="#+id/btnLeftOk"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:filterTouchesWhenObscured="true"
android:gravity="center"
android:text="#string/apply"
android:textColor="#color/white"
app:backgroundTint="#color/primary_dark_gray"
app:cornerRadius="0dp" />
<com.google.android.material.button.MaterialButton
android:id="#+id/btnRightCancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:filterTouchesWhenObscured="true"
android:gravity="center"
android:text="#string/reset"
android:insetTop="0dp"
android:insetBottom="0dp"
android:textColor="#color/white"
app:cornerRadius="0dp"
app:backgroundTint="#color/btn_background_gray" />
</LinearLayout>
After applying below property to "Reset" button.
android:insetTop="0dp"
android:insetBottom="0dp"
Button spacing will be removed from top and bottom. It will occupy spacing with button color. (like in Reset button)
Button background can be changed using one property app:backgroundTint.

How do I set a "pressed" imagebutton in Android Studio?

I have an ImageButton:
<ImageButton
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/btn_70s"
android:src="#drawable/btn_70s"
android:background="#null" /* Sets transparent BG for button image(PNG) */
/>
The image is "btn_70s", how do I set a "pressed" version of the image for when the ImageButton is tapped?
The documentation states to create an XML file and place it in the drawable directory:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/button_focused" /> <!-- focused -->
<item android:drawable="#drawable/button_normal" /> <!-- default -->
</selector>
This to me says it would affect all the ImageButtons rather than an individual one. Would I save it as an individual XML for each button for example (btn_70s_custom.xml), and then use it like:
<ImageButton
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/btn_70s"
android:src="#drawable/btn_70s"
android:android:background="#drawable/btn_70s_custom"
/>
Only issue with this, I can't use #null to allow transparency. Any assistance is appreciate.
Have a look over there for a (possible) solution to your problem : Make button background transparent using selector
And for the record,
This to me says it would affect all the ImageButtons rather than an individual one. Would I save it as an individual XML for each button for example (btn_70s_custom.xml), and then use it like:
No, it won't set this theme for all the buttons, but only for the ones where you specify to use this selector.
Hope this helps!
You can try selectableItemBackground
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"/>

Remove focus border of EditText

How can I remove the border with appears when focusing an EditText View?
I need it cause this view has little space in the screen, but without the border it's enough. When running on Emulator an orange border appears, on Device a blue one.
Have you tried setting the background of the EditText to a transparent colour?
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/hello"
android:background="#00000000"
/>
<EditText
android:id="#+id/edittext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:drawable/editbox_background_normal"
/>
It is possible. However I would not recommend it because users are used to certain metaphors and you should not change the general UX.
You can apply different styles to your views. In your case it sounds like you want an EditText View element which looks like a TextView element. In this case you would have to specify other backgrounds for the EditText depending on the state of the View element.
In your desired layout.xml you assign a background to your EditText:
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/hello" android:background="#drawable/custom"
/>
Then you create the custom.xml in your drawable folder and add the following:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="#drawable/textfield_default" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="#drawable/textfield_disabled" />
<item android:state_pressed="true" android:drawable="#drawable/textfield_default" />
<item android:state_enabled="true" android:state_focused="true" android:drawable="#drawable/textfield_default" />
<item android:state_enabled="true" android:drawable="#drawable/textfield_default" />
<item android:state_focused="true" android:drawable="#drawable/textfield_disabled" />
<item android:drawable="#drawable/textfield_disabled" />
</selector>
Those are the possible states of your EditText View element. Normally you can access Android platform drawables directly by using #android:drawable/textfield_default, but in this case the textfield drawables are private so you have to copy them into your own drawable folder. The original resources can be found in your SDK installation folder at ANDROID_HOME\platforms\android-(API LEVEL)\data\res\drawable-(*dpi)\.
Once you are done you end up with an EditText which looks like a TextView but completely without those borders. Those orange borders you saw in the emulator are the default Android drawables. The blue ones are vendor specific (possibly Samsung).
Hope that helped and didn't confuse that much.
This is the fastest solution:
<EditText
android:id="#+id/edittext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00000000"
/>
You can keep the background color as transparent to remove the EditText border on focus.
Method 1
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00000000"
/>

Categories

Resources