Disable button click effect in xml - android

I created a simple ImageButton in my app and im using and image in it.
I had like that when I click the button that it wont show that it was clicked (not sure how it is called but when the button changes color).
My button looks as follows:
and when I click it becomes:
<ImageButton
android:id="#+id/returnButton"
style="?android:borderlessButtonStyle"
android:layout_width="40dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:src="#drawable/ic_rarrow"
app:layout_constraintBottom_toBottomOf="#+id/tv_Username"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/tv_Username">
</ImageButton>
I want to remove this gray background even when clicked because it shows boundaries that I wish to hide.
Thank you

You can solve the problem by adding one line of code.
android:background="#null"
Hoping it will be helped

You can apply a transparent background to the ImageButton
android:background="#android:color/transparent"

Apply a background to the Button:
android:background="#android:color/white"

Related

How to prevent disabled Button from going behind another ImageView or Button?

EDIT: After more testing, I believe that the problem mentioned below is occuring on click, not on disable. Researching into elevation, to see if in click possibly changes that.
I am using a constraint layout and I have two buttons that contain images.
One of them is disabled temporarily or permanently when clicked by the user.
The problem? The second button (which should stay at the back) ends up covering the other newly disabled button.
I've tried searching for anything concerning this but couldn't find anything to help me solve this. I've also tried using an ImageView for the button that should stay underneath, but also didn't work.
// the button that should stay underneath
<Button
android:id="#+id/bg_pup_img"
android:contentDescription="#string/pup2_desc"
android:layout_width="0dp"
android:layout_height="400dp"
android:layout_marginTop="120dp"
android:scaleType="fitEnd"
android:background="#drawable/pup2"
android:enabled="false"
android:clickable="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/end_game"
app:layout_constraintVertical_bias="1.5" />
// the button I want to stay at the top
<Button
android:id="#+id/card19"
android:layout_width="60dp"
android:layout_height="80dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="40dp"
android:layout_marginRight="40dp"
android:background="#drawable/blank_card"
android:tag="19"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/card15" />
Try adding android:visibility="" to 'gone' or 'invisible'
Making you button disable will put it to background.
if i understood right , you want to show a button when a user click another one , if this is the case , why don't you make the second button "invisible" after the user clicks
I seemed to have fixed my problem.
I'm not exactly sure how elevation works on click, but when ever I clicked the "top" button, it would go below the "bottom" button.
I made the elevation of the "bottom" button to be 0dp while the "top" button is 1dp. Now I no longer have that problem.

Changing text while pressing button android studio

I've tried a tutorial on youtube but it doesn't work,when I click the button nothing happens.
When I press the button I want to Change the layout activity and also to be set on the new layout one random text from lets say other 10 and on this current layout I wanna have the button which will randomly change the first text to another one.
Here is what codes I've used and didn't worked out.
#I can't add the codes because It doesn't let me post it here..it says something like isn't valid but you see everything in the picture,so I did all that code and when I press on my button It doesn't doing anything,what can be the problem?and If you have any idea or if you know any tutorial to help me achieve what I said I want to do upper I would appreciate really much
#as you can see there is a string option which should give me one text of all of them when I click my button,but it doesn't
sorry for image but it seems like those codes aren't good
<LinearLayout
android:layout_width="332dp"
android:layout_height="184dp"
android:layout_marginStart="36dp"
android:layout_marginLeft="36dp"
android:layout_marginTop="48dp"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<Button
android:id="#+id/buttonsports"
android:layout_width="match_parent"
android:layout_height="129dp"
android:background="#drawable/ft"
tools:layout_editor_absoluteX="115dp"
tools:layout_editor_absoluteY="291dp" />
<TextView
android:id="#+id/sports"
android:layout_width="216dp"
android:layout_height="117dp"
android:text="TextView"
tools:layout_editor_absoluteX="95dp"
tools:layout_editor_absoluteY="246dp" />
</LinearLayout>
this is how big my text box is,the "sports" is the id for TextView which is "abc"
In your xml you have a LinearLayout for which you have set the height to:
android:layout_height="184dp"
and inside there is a Button with height:
android:layout_height="129dp"
So for the TextView below there is left maximum 55dp but you set:
android:layout_height="117dp"
This means that at least half the TextView is not visible.
In your code you set a random text to the TextView when you click the Button.
You say that nothing happens.
Is there a case that the text is being set but you can't see it because the TextView is mostly hidden?
Change the heights of the views and you will find out.

Make borderless image button small on Android

There is some closeable view in my app and it has a close button in header. I want make this button borderless and small.The code:
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/borderlessButtonStyle"
android:layout_margin="0dp"
android:padding="0dp"
android:src="#android:drawable/ic_menu_close_clear_cancel"/>
In result the button is borderless but has much empty place around cross image (button is hitted on screenshot to make empty space visible)
How could I fix it?
You should use:
android:background="?selectableItemBackgroundBorderless"
Added
android:minHeight="0dp"
android:minWidth="0dp"
android:padding="2dp"
The result
Also I'll use negative margins to place the button closer to corner.
You can use the following on your ImageButton to remove the 'border':
android:background="#android:color/transparent"
or
android:background="#null"
If you would like change the background when the user clicks the button you can create a selector. Example ImageButton doesn't highlight on click with Transparent background.
With the introduction of ConstraintLayout, you can use constraints to reduce the width of the borderless ImageButton, that's way too wide by default, by constraining it to be as wide as the height:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/my_closeable_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
style="?android:attr/borderlessButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:contentDescription="#string/action_close"
android:src="#android:drawable/ic_menu_close_clear_cancel"
app:layout_constraintDimensionRatio="w,1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
I don't recommend you to make it any smaller than this because the touch target needs to be at least 48dp x 48dp, as for Google's Material Design guidelines, otherwise a user would have a hard time trying to touch it.
If you really need it to look like it's closer to the edges of the view, you can always use an image that's not centered (has transparent padding on one or two sides), but I'd try my best to avoid doing this workaround and try rethinking my app's design in order to accommodate the button as it is.
use textView for that..
and use textview.OnTouchListener
finally onTouch change the color of text.. thats it
or else in your code just use
android:background="#null"

Button with image: remove background color

I'd like to have an image in a button with text and I've already have it. But the problem is that my image is with some " grey background colour" (as you can see below) that I would like to remove.
In a normal situation I would use android:background="#null" and it would solve it, but I'm using as background a template to my button.
<Button
android:drawableLeft="#drawable/profile_32"
android:layout_marginRight="25dp"
android:layout_marginBottom="10dp"
android:textColor="#color/yellow3"
android:background="#drawable/buttons2"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="#string/button_profile"
android:textSize="18dp"
android:textStyle="bold"
android:id="#+id/buttonProfile"
/>
Does anyone know any other way to remove that grey background color from the image? Thanks.
Is the grey actually part of the image? If not you could try background:"#android:color/transparent"
If the grey background is part of the profile_32 image, there is not an easy way to remove it in Android. You will need to have the image re-exported or use an image editing program to remove the background.
Yes, use an ImageButton and just add
android:background="#00000000"
or
android:background="#null"
to your Button.
The first 2 "0" is alpha, "00" = trasparent

Background of my custom button isn't transparent

I created a custom button and I saved it as a .png with a transparant background.
this is the code in xml
<Button
android:id="#+id/btnReport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:drawableTop="#drawable/report" />
and when i run it in the emulator I get my button with a grey background
(I cannot post images yet, because my reputation is too low. Apologies for this)
How do I make the background transparant?
Thanks.
Vincent
You should be doing
android:background="#drawable/report"
is that image "report" only transparent? You donĀ“t need to set a transparent image, just set
android:background="#android:color/transparent"
to your button.

Categories

Resources