Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set button have image and shadow?

To make shadow, make shadow xml file and use like android:background="@drawable/shadow". But put image is android:background="@drawable/image too.

how to set button shadow and image in one button?

I'm sorry I'm not fluent in English.

like image 229
SunSpike Avatar asked Aug 01 '17 06:08

SunSpike


People also ask

How will you add graphics to button?

Copy your image file within the Res/drawable/ directory of your project. While in XML simply go into the graphic representation (for simplicity) of your XML file and click on your ImageButton widget that you added, go to its properties sheet and click on the [...] in the src: field. Simply navigate to your image file.

How do you fit a picture inside a button?

How to programatically resize and show them? Use a android:scaleType="fitCenter" to have Android scale the images, and android:adjustViewBounds="true" to have them adjust their bounds due to scaling. All of these attributes can be set in code on each ImageButton at runtime.

What is difference between Button and image button?

So fundamentally, a Button can have text and is clickable, whereas an ImageButton is a bit more flexible in how you set the image. It has methods from its ImageView base class like setImageURI which a Button does not.


2 Answers

1. use ImageButton

try this you can use ImageButton :- 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.

 <ImageButton
  android:id="@+id/btnMain"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:adjustViewBounds="true"
  android:contentDescription="@string/btnMain_description"
  android:elevation="10dp"
  android:background="@android:drawable/dialog_holo_light_fram‌​e"
  android:scaleType="centerInside"
  android:src="@drawable/IMage"
 />

2. user CardVirew or you can try this add your button inside card view like this

compile this dependencies

compile 'com.android.support:cardview-v7:25.3.1'

layout like this

    <android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardElevation="10dp">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/disha"
        android:text="@string/app_name" />
</android.support.v7.widget.CardView>

ask me in case of any query

like image 199
AskNilesh Avatar answered Oct 13 '22 23:10

AskNilesh


create button_selector.xml in res/drawable :

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <layer-list>
            <item android:right="5dp" android:top="5dp">
                <shape>
                    <corners android:radius="3dp" />
                    <solid android:color="#D6D6D6" />
                </shape>
            </item>
            <item android:bottom="2dp" android:left="2dp">
                <shape>
                    <gradient android:angle="270" 
                        android:endColor="#E2E2E2" android:startColor="#BABABA" />
                    <stroke android:width="1dp" android:color="#BABABA" />
                    <corners android:radius="4dp" />
                    <padding android:bottom="10dp" android:left="10dp" 
                        android:right="10dp" android:top="10dp" />
                </shape>
            </item>
        </layer-list>
    </item>

    </selector>

And in your xml layout:

 <ImageButton
            android:src="@mipmap/ic_launcher"
            android:background="@drawable/shadow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:elevation="10dp"/>
like image 37
bhaskar kurzekar Avatar answered Oct 13 '22 22:10

bhaskar kurzekar