Button with image without the regular button shape: Android - android

I have an image which I want to use as a button. But I want only the image to be visible as the button, with the shape of the image (I do not want the regular button around the image, nor I want the regular button shape). How can I achieve this? Can I achieve this without designing a custom button?

Check the android developers reference and Tutorial Custom Button. It's exactly what you need!

Set the image with button.setBackgroundDrawable (or Resource, etc.)

Use ImageButton with background property set to some color i.e. #000 and no padding

<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" android:background="#drawable/ic_launcher"/>

Related

How to set a background Image to a Button BUT STILL WORKS LIKE A NORMAL BUTTON (With Effects)?

I have been trying to achieve the Normal android button effects when the button is already set with a background image
But the problem is, once the background is set, the button default nature is gone (shadows, clickable animation etc..)
Can Someone please give me a working solutions for this?
Here's the code to my button
<Button style="#style/Widget.AppCompat.Button.Colored"
android:id="#+id/student"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/student"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintVertical_bias="0.891" />
It looks amazing since I have set a background image but It still looks like you have set an image to a jLabel in Java)
How can I get the default button looks while keeping my background image intact?
Button works by setting the background to a background with the elevation, shadows, ripple, etc. To get the same behavior, you're going to need to set your background to a StateListDrawable that performs the ripple effect and puts the shadows and elevation effects with your image as the background inside of all that. You can't just set the background to be your image, that will lose all of those features. The easiest way to get that right is to look at the AOSP source code and find the drawable it uses, copy it to your project, and edit appropriately.
Basically in Android a Button is just a TextView with a special background preset.
Why don't you use ImageButton widget ? It has the same behaviour as a Button and ImageView combined
Check this tutorial to see how to work with it.
as mentionned in the official documentation
Displays a button with an image (instead of text) that can be pressed or clicked by the user. By default, an ImageButton looks like a regular Button, with the standard button background that changes color during different button states. The image on the surface of the button is defined either by the android:src attribute in the XML element or by the ImageView.setImageResource(int) method

Make a Button's background transparent

I am very new to Android Studio and creating apps in general.
I have created an Image Button and uploaded an Image, problem is that I want to make the background of the Button transparent.
This is what I mean:See Example
The background of the left button is black and I want to make it transparent, like the Right Button.
Just put this property inside ImageButton tag:
android:background="#android:color/transparent"
----------OR--------------
You can also user vector image for this:
Click on Vector Asset
Click on small icon image(just below ic_android_black_24dp)
Search any icon and add to your drawable. The icon you are using is appeared in search result. use this list icon.
Try This
<ImageButton
android:layout_width="wrap_content"
android:background="#android:color/transparent"
android:src="#drawable/image"
android:layout_height="wrap_content" />

Android use vector drawable to replace text in button

Currently I have a button that looks like this:
What I want to do is to be able to replace the text with this vector drawable but keep the blue rectangle background:
I looked into doing this but I cannot seem to get it right.
I tried the following combination:
<Button
android:id="#+id/deletebutton"
android:layout_width="0dp"
android:layout_height="50dp"
android:src="#drawable/deletebtn24dp"
android:backgroundTint="#color/primaryButtonColor"
android:layout_weight="1"/>
However that just led to a blank blue button with no drawable. What can I do so the drawable replaces the text?
Also I would like to support API levels start at 16+.
You have to use app:srcCompat="#drawable/deletebtn24dp" instead of android:src="#drawable/deletebtn24dp". Using an ImageButton might also be better than using a straight Button.

How can I stretch the image in imagebutton to the whole area of the imagebutton?

Here is a picture that will help you understand my problem:
I want to stretch the image shown inside ImageButton to the whole area of the ImageButton.
As you can see, the picture with the number 1 only take about 95% of the ImageButton and you can also see the boundaries of the ImageButton.
I still want to maintain the image button style
Here's how I am using ImageView:
<ImageButton
android:id="#+id/secondActivityBbuttonDownRight"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:scaleType="fitStart"
android:src="#drawable/111" />
I think I found out what is my problem.
I need a custom component that can contain only an image and have a clicking effect.
ImageButton is ImageView.
If you want your image to fill button
than set android:scaleType="fitXY", so the image you set in android:src will be stretched to fill whole image button.
ImageButton has a background under your image and padding.
If you do not want background
set android:background="#android:color/transparent".
If you do not want padding
set android:padding="0" or set background without padding.
You can also use
android:scaleType="centerCrop"
android:padding="0dp"
android:scaleType="fitXY" tested and works correctly.
Use below code :-
<ImageButton
android:id="#+id/secondActivityBbuttonDownRight"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:scaleType="fitStart"
android:background="#drawable/111" />
You should consider creating a 9-patch image to make the image fit correctly on an ImageButton in case you need to stretch it, but you don't want the image to become distorted.
To have a button with an image that completely covers it, you can use something like this:
<ImageButton android:id="#+id/iconBtn"
android:layout_width="64dip"
android:layout_height="64dip"
android:src="#drawable/icon_button"
android:scaleType="fitCenter"
android:background="#00000000"/>
For a full tutorial on creating buttons with custom skins and the use of selectors to manage their states, you can check a tutorial I've written some time ago here (Creating a custom Android button with a resizable skin).
If your image "111" has no padding itself then simply use android:background"#drawable/111"
Just set this two attributes on the ImageButton:
android:background="?attr/selectableItemBackgroundBorderless"
android:scaleType="centerCrop"
With those, you will get the provided src to be stretched, and also if you put some specific width and height on that ImageButton you won't get a rectangle around it.

Creating header with home page(Landing screen) button

One of the application in my mobile has a header like the one shown below in the snapshot. I like the design very much and would like to create one for my application. Clicking the home icon takes the user to landing page(Home page). I have only created buttons of squarish and rounder corners. But not sure, how to create one like the one given below. Any help would be highly appreciated.
Create the image in photoshop or whatever.
Then create an imageview and let it act like a button:
<ImageView
android:id="#+id/camera_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/button_camera"
android:onClick="onCameraButtonClick" />
onClick is introduced in Android 1.6
or a button and set the background:
<Button
android:id="#+id/camera_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_camera"
android:onClick="onCameraButtonClick" />
drawable/camera_button should be a state drawable so the user can see when it is being clicked/focused.
in your activity:
public void onCameraButtonClick(View v){
// Do Something
}
It's all here:
http://developer.android.com/reference/android/widget/Button.html
The way that I would do this is just create the buttons in photoshop/GIMP on the same canvas then save each as PNG, you can use the slice tool for this. Then I would just add an attributes to each button. There's attributes that let you set the left margin of say the right button to line up with the right margin of the left button. The backgrounds of the buttons could also be patch9 images, and the icons inside it could be the src attribute of the button view. The button with the camera would have SRC pointing to a PNG that has the camera icon as well as "Camera". And the button itself not having any text.

Categories

Resources