Background drawable does not fill floating action button - android

I have FAB and trying to fill a drawable(round_drawable.xml) to my entire fab but the drawable appears currently as in the top left corner as shown here,
I tried a solution from Setting src and background for FloatingActionButton but it did not work, added most attributes from other sources but neither scaleType, src, background did not work.
How do I get to fill the FAB with the round_drawable ?
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="100dp"
android:layout_height="95dp"
android:scaleType="fitXY"
app:backgroundTint="#color/white"
android:src="#drawable/round_drawable"
android:backgroundTintMode="src_atop"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</android.support.constraint.ConstraintLayout>
round_drawable.xml
#drawable/right is a png image of right arrow.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:innerRadius="50dp"
android:shape="oval">
<corners android:radius="10dp" />
<gradient
android:angle="45"
android:endColor="#D425B5"
android:startColor="#EBF928" />
</shape>
</item>
<item
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp">
<bitmap
android:gravity="center"
android:src="#drawable/right" />
</item>
</layer-list>

Just add this line in you dimen.xml file
<dimen name="design_fab_image_size" tools:override="true">56dp</dimen>
NOTE : 56dp is default size of FAB buttom
Default - 56dp
mini - 40dp
custom - whatever you have specified in fabCustomSize
TIP : use app:fabCustomSize rather giving custom height width to FAB button.
Example
in your layout
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:backgroundTint="#color/white"
<!--inmprtent-->
app:fabCustomSize="100dp"
android:src="#drawable/round_drawable"
android:backgroundTintMode="src_atop"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
now create dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
<dimen name="design_fab_image_size" tools:override="true">100dp</dimen>
</resources>
keep your round_drawable.xml as it is and you are good to go
Result
cheers..

You don't have to give specific size to FAB.And it won't require background drawable also for any specific color. It has attribute fabSize in which you can pass predefined sizes like mini(40dp), normal(56dp). And still if you want to add size remove sizes from drawable <item> tag. That tag actually making your drawable small.
check this sample code for creating Fab with image in center
<android.support.design.widget.FloatingActionButton
android:id="#+id/fbtn_addNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginBottom="#dimen/margin_16"
android:layout_marginRight="#dimen/margin_16"
app:srcCompat="#drawable/ic_add_white_28dp"
app:backgroundTint="#color/colorPrimary"
app:borderWidth="#dimen/margin_0"
app:elevation="#dimen/margin_8"
app:fabSize="normal"
android:visibility="visible"
app:pressedTranslationZ="#dimen/margin_5" />
It will look like this. Hope it will help...

Updated answer of 2019 with androidX:
Dependency : implementation 'com.getbase:floatingactionbutton:1.10.1'
XML:
<com.getbase.floatingactionbutton.FloatingActionsMenu
android:id="#+id/multiple_actions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_margin="#dimen/fab_margin"
android:layout_marginBottom="20dp"
app:fab_addButtonColorNormal="#color/colorPrimaryDark"
app:fab_addButtonColorPressed="#color/colorPrimaryDark"
app:fab_colorPressed="#color/colorPrimaryDark"
app:fab_labelStyle="#style/menu_labels_style">
<com.getbase.floatingactionbutton.FloatingActionButton
android:id="#+id/btn_langauge_translation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/ic_language_icon"
app:fab_size="normal"
android:paddingBottom="15dp"
app:fab_colorNormal="#color/colorPrimaryDark"
app:fab_colorPressed="#color/colorPrimaryDark"
app:fab_title="" />
<com.getbase.floatingactionbutton.FloatingActionButton
android:id="#+id/btn_facebook_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/ic_fb_icon"
app:fab_size="normal"
android:paddingBottom="15dp"
app:fab_colorNormal="#0184FF"
app:fab_colorPressed="#0184FF"
app:fab_title="" />
<com.getbase.floatingactionbutton.FloatingActionButton
android:id="#+id/btn_whats_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/ic_whatsapp_icon"
app:fab_colorNormal="#4EC248"
android:paddingBottom="15dp"
app:fab_size="normal"
app:fab_colorPressed="#4EC248"
app:fab_title="" />
<com.getbase.floatingactionbutton.FloatingActionButton
android:id="#+id/btn_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/ic_email_icon"
app:fab_colorNormal="#848484"
android:paddingBottom="15dp"
app:fab_colorPressed="#848484"
app:fab_size="normal"
app:fab_title="" />
</com.getbase.floatingactionbutton.FloatingActionsMenu>
Output:

I used this code and it worked:
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/add"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/ic_add"
android:scaleType="center"
android:backgroundTint="#android:color/transparent"
android:outlineAmbientShadowColor="#android:color/transparent"
android:outlineSpotShadowColor="#android:color/transparent"
/>
and in the dimens.xml file:
<dimen name="design_fab_image_size" tools:override="true">40dp</dimen>

Just add this line in you style.xml file
<style name="FullImageFloatingButton" parent="Widget.Design.FloatingActionButton">
<item name="fabSize">normal</item>
<item name="maxImageSize">56dp</item>
</style>
<style name="FullImageMiniFloatingButton" parent="Widget.Design.FloatingActionButton">
<item name="fabSize">mini</item>
<item name="maxImageSize">40dp</item>
</style>
Use FullImageFloatingButton for normal FloatingButton and FullImageMiniFloatingButton for mini FloatingButton.

Related

How to add border in Floating action button in Android

I was trying to build something like the below image:
Here is my xml code:
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab_scan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/app_name"
android:src="#drawable/vector_nav_qr_scanner"
app:backgroundTint="#android:color/white"
app:layout_anchor="#id/bottom_app_bar" />
I was searching a lot but couldn't figure out how can I add the light blue background with a white circle border and also an icon in the center. Can any one help?
Try this:
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#B1E4F6" //inner background color
android:src="#drawable/scan"
app:backgroundTint="#color/white" //outer background color (border color)
app:borderWidth="8dp"
app:maxImageSize="16dp" //inner icon size
app:tint="#android:color/holo_blue_dark" /> //inner icon color
Result:
Add
app.strokeColor = "#000033"
app.strokeWidth = "3dp"
in your floating action button xml..
for example:
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab_scan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/app_name"
android:src="#drawable/vector_nav_qr_scanner"
app:backgroundTint="#android:color/white"
app:layout_anchor="#id/bottom_app_bar"
app:strokeColor = "#000033"
app:strokeWidth = "3dp" />
edit: app:strokewidth and app:strokeColor is working for ExtendedFloatingActionButton. Sorry, that's my fault.
You can use
app:borderWidth="4dp" in stead of app:strokeWidth = "3dp". remove strokeColor. there showes little border.
i hope it will help you.
please let me know if it doesn't work.
OR
define custom shape in drawable folder
fab_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="0dp"
android:shape="ring"
android:thicknessRatio="2"
android:useLevel="false" >
<solid android:color="#android:color/transparent" />
<!-- here set the width and color of your border -->
<stroke
android:width="5dp"
android:color="#android:color/darker_gray" />
</shape>
then wrap your FloatingActionButton with a layout. use fab_bg.xml file in layout's background.
for example:
<LinearLayout
android:id="#+id/fabWrapperId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/fab_bg"
android:padding="5dp">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab_scan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/app_name"
android:src="#drawable/vector_nav_qr_scanner"
app:backgroundTint="#android:color/white"
app:layout_anchor="#id/bottom_app_bar"
app:fabSize="normal" />
</LinearLayout>
NB: make sure your stroke width from fab_bg and layout padding keep the same.

(Android) How can I put a drop shadow in a Custom View?

I want to create a Custom Button with the following design using View and give it a drop shadow like the design below.1
The shadow options are: box-shadow: 10px 7px 25px 0px #0000001F
main_round_teamsbtn.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:padding="10dp"
android:shape="rectangle">
<solid android:color="#color/btn_color" />
<corners
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="105dp" />
</shape>
</item>
</layer-list>
teams_main_btn.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/main_round_teamsbtn"
android:paddingStart="16dp">
<TextView
android:id="#+id/teamsHead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/notosans_bold"
android:includeFontPadding="false"
android:text="원하는\n공모전을\n찾아보세요"
android:textColor="#color/black"
android:textSize="22sp"
app:layout_constraintBottom_toTopOf="#+id/teamsSub" />
<TextView
android:id="#+id/teamsSub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/notosans_regular"
android:text="현재 구하고 있는 팀 둘러보기"
android:textColor="#8C8C8C"
android:textSize="10sp"
app:layout_constraintBottom_toBottomOf="parent" />
<ImageView
android:id="#+id/teamsImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="4dp"
android:background="#drawable/findteams_btn"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0" />
</androidx.constraintlayout.widget.ConstraintLayout>
main.xml
...
<com.example.moizar.MainTeamsBtnView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
...
I want an answer how to give a shadow
I doubt you can achieve what you want by code, even if this is possible you will write a lot of code, probably involving RenderScript or some new APIs on Android 12...
consider using your image as 9-patch drawable with properly marked content/stretch areas. content (text?) would be full height and half width, stretch area only first pixels
teams_main_btn.xml
You can use attributes elevation in TextView or ImageView.
android:elevation= "10dp"
You can watch this link for more:
https://www.youtube.com/watch?v=nNHChjTZCtw&ab_channel=CodeAndDesign

Bottom navigation bar - rectangle action button

Using google.android.material:
Is it possible to make action button rectangle ?
Is it possible to move it a little higher / lower ?
I am talking about the middle circle button.
You can use the app:shapeAppearanceOverlay attribute to achieve a square button and the app:fabCradleVerticalOffset attribute to change the distance of the FAB to the BottomAppBar.
Something like:
<com.google.android.material.bottomappbar.BottomAppBar
app:fabCradleVerticalOffset="16dp"
app:fabCradleRoundedCornerRadius="0dp"
app:fabCradleMargin="0dp"
..>
<com.google.android.material.floatingactionbutton.FloatingActionButton
app:shapeAppearanceOverlay="#style/SquareFloatingShapeOVerlay"
../>
with:
<style name="SquareFloatingShapeOVerlay" parent="">
<item name="cornerSize">0dp</item>
</style>
If you want a rectangular shape you can use a ExtendedFloatingActionButton instead of the FloatingActionButton.
Something like:
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
app:layout_anchor="#id/bottom_app_bar"
app:shapeAppearanceOverlay="#style/ShapeAppearanceOverlayExtended"
../>
with:
<style name="ShapeAppearanceOverlayExtended" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">0dp</item>
</style>
Note: it requires the version 1.1.0 of the Material Components Library.
Try this: If you are using FloatingActionButton
app:borderWidth="0dp"
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_add"
android:layout_marginRight="20dp"
app:fabSize="normal"
android:elevation="#dimen/fab_elevation"
android:background="#000000"
app:borderWidth="0dp"
android:stateListAnimator="#animator/fab_anim"
android:layout_gravity="center_horizontal"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
Hope this will help !
To change the shape of the Floating Action Button, you can create a separate resource file in the drawable folder for eg: shape.xml as-
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/colorAccent" />
and then in Floating Action Button add the following property:
android:background="#drawable/shape"
Also, you can use margin to move it higher.
Use elevation attribute to FAB
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:menu="#menu/nav_items" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:layout_marginBottom="40dp"
android:clickable="true"
app:elevation="8dp"
app:srcCompat="#android:drawable/ic_input_add" />

Android xml drawable is bigger first time

I have two clickable text views with the same drawable left image. When I open fragment first time the first image looks bigger then second one. After clicking on first text view the next fragment is opening and then when I return back two images have the same size. What's wrong? Please help to find out reason of this bug.
This is my layout:
<include layout="#layout/toolbar" />
<RelativeLayout
android:id="#+id/dmvLawyers"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true">
<TextView
style="#style/DashBtnStyle"
android:drawableLeft="#drawable/lawyer_selected"
android:text="#string/dmv_lawyers"
android:textAllCaps="false" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:paddingRight="#dimen/add_friends_padding_left"
android:src="#drawable/add" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="#dimen/divider"
android:background="#color/lineColor" />
<RelativeLayout
android:id="#+id/tlcLawyers"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true">
<TextView
style="#style/DashBtnStyle"
android:drawableLeft="#drawable/lawyer_selected"
android:text="#string/tlc_lawyers"
android:textAllCaps="false" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:paddingRight="#dimen/add_friends_padding_left"
android:src="#drawable/add" />
</RelativeLayout>
</LinearLayout>
This is image drawable (lawyer_selected.xml), where femida is png image, and white background is needed for selected state (button in selected state is green and image should be with white frame):
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<padding
android:bottom="#dimen/dash_icon_padding"
android:left="#dimen/dash_icon_padding"
android:right="#dimen/dash_icon_padding"
android:top="#dimen/dash_icon_padding"/>
<solid android:color="#android:color/white"/>
<corners
android:radius="#dimen/dash_icon_radius"/>
</shape>
</item>
<item android:drawable="#drawable/femida"/>
</layer-list>
Image femida.png
Just use nested LinearLayout instead and use android:layout_weight="1" or any required value to you and it will work for sure.
**In your case it will be 1 only, while the main parent LinearLayout can have wrap_content as height

Place ImageView over Button android

I am trying to place an ImageView over a Button using RelativeLayout. Here is my xml:
<RelativeLayout
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_weight="0.50" >
<Button
android:id="#+id/btnFindDaysInBetween"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/blue_500"
android:text="#string/dt_text_days" />
<ImageView
android:id="#+id/imageview_find_days_in_between"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:contentDescription="#string/empty"
android:src="#drawable/ic_check_circle_white" />
</RelativeLayout>
Here is the image screenshot:
As you can see, the ImageView's src image is not visible. However if i change the button at the back to an ImageView, the image of the top ImageView is visible. Please refer below..
Changed xml:
<RelativeLayout
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_weight="0.50" >
<!--
<Button
android:id="#+id/btnFindDaysInBetween"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/blue_500"
android:text="#string/dt_text_days" />
-->
<ImageView
android:id="#+id/imageview_find_days"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:contentDescription="#string/empty"
android:src="#drawable/ic_send_black" />
<ImageView
android:id="#+id/imageview_find_days_in_between"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:contentDescription="#string/empty"
android:src="#drawable/ic_check_circle_white" />
</RelativeLayout>
Changed xml's screenshot:
What is it that i am doing wrong in the first layout?
The reason is actually very simple. :) We are so caught up thinking in 2D that we overlook the elevation - in Z.
There is nothing wrong with your first layout. The Button simply has a higher elevation than the ImageView - exactly 1dp higher. Therefore, no matter how you arrange the two views, the Button rises above.
A bit of proof:
A Button, by default gets the Widget.Material.Button style:
<!-- Bordered ink button -->
<style name="Widget.Material.Button">
<item name="background">#drawable/btn_default_material</item>
<item name="textAppearance">?attr/textAppearanceButton</item>
<item name="minHeight">48dip</item>
<item name="minWidth">88dip</item>
<item name="stateListAnimator">#anim/button_state_list_anim_material</item>
<item name="focusable">true</item>
<item name="clickable">true</item>
<item name="gravity">center_vertical|center_horizontal</item>
</style>
The attribute that introduces this elevation is android:stateListAnimator. StateListAnimator is similar to StateListDrawable, and provides state change animations. The complete xml is here: Link. But here's the base state of the button:
<!-- base state -->
<item android:state_enabled="true">
<set>
<objectAnimator android:propertyName="translationZ"
android:duration="#integer/button_pressed_animation_duration"
android:valueTo="0"
android:startDelay="#integer/button_pressed_animation_delay"
android:valueType="floatType"/>
<objectAnimator android:propertyName="elevation"
android:duration="0"
android:valueTo="#dimen/button_elevation_material"
android:valueType="floatType" />
</set>
</item>
As you can see, the elevation value for the button is set to #dimen/button_elevation_material:
<dimen name="button_elevation_material">1dp</dimen>
And that's how the ImageView ends up being behind/below the Button.
So, what can we do?
A straight-forward solution would be to set the ImageView's elevation to the same amount - 1dp.
Another solution, which will require a bit of work, is to remove the Button's elevation rather than change ImageView's. Based on the default StateListAnimator, we can create our own - and remove the elevation. Then, in your res/values-v21/styles.xml, define a style that inherits from Widget.Material.Button:
<style name="MyDepressedButtonStyle" parent="android:Widget.Material.Button">
<item name="android:stateListAnimator">#anim/customized_state_animator</item>
</style>
Now, set this style on your Button:
<Button
style="#style/MyDepressedButtonStyle"
....
.... />
Edit:
Actually, we can apply the customized StateListAnimator directly:
<Button
android:stateListAnimator="#anim/customized_state_animator"
....
.... />
No need to take the scenic route!
I found a solution:
simply android:elevation="2dp"
<Button
android:id="#+id/btnAccess"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_gravity="right|center"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView4"
android:background="#drawable/or"
android:layout_alignBottom="#+id/btnRegister"
android:layout_centerHorizontal="true"
android:layout_margin="5dp"
android:elevation="2dp" />
Actually it's much easier to just set the StateListAnimator to #null
<Button
...
android:stateListAnimator="#null" />
Source Android 5.0 android:elevation Works for View, but not Button?
Use ImageButton replace Button and set ImageButton background as transparent.
<ImageButton
android:id="#+id/btnFindDaysInBetween"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/blue_500_text"
android:background="#android:color/transparent"
/>
<ImageView
android:id="#+id/imageview_find_days_in_between"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:contentDescription="#string/empty"
android:src="#drawable/ic_check_circle_white" />
You can make a image that has your blue_500 color and text(that is easy to create), then set this image to your ImageButton. After that, your ImageView will see on the top of ImageButton.
Hope this help!
Button is just a TextView with a certain style applied to it, so if you replace Button with TextView you can display an ImageView on top of it. This also works on API < 21.
android:background exists for all the view. As the name suggests this is what is going to be there in the background.
android:src exists for ImageViews and its subclasses. You can think of this as the foreground. Because ImageView is a subclass of View you even have android:background for that.
If the foreground is smaller than background, the background portion which is not covered by the foreground would be visible.
Also, you can use transparency in the foreground in which case the background would be visible(transparently).
You can use BACKGROUND FOR ALL THE VIEWS.. But You can use SRC only for ImageView & ImageButton.....
#Vamsi I tried your both combinations and first one is not working with Button. You have to go through ImageView. This is what I tried with with ImageView:
While I tried to do it with Button and see what was result:
I tried to change the order but all in vain! It seems you have to go with either ImageView or ImageButton.
At the end! You can see what I had tried:
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent">
<!-- <ImageView
android:id="#+id/btnTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_launcher_web"
android:contentDescription="#string/app_name"
android:text="#string/app_name" /> -->
<ImageView
android:layout_width="wrap_content"
android:id="#+id/imgView"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:contentDescription="#string/app_name"
android:src="#drawable/ic_launcher" />
<Button
android:id="#+id/btnTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imgView"
android:layout_alignTop="#+id/imgView"
android:text="#string/app_name" />
</RelativeLayout>
I had done same kind of work either with ImageView or ImageButton (probably with ImageView) and tried same approach as you were trying with Button.
Thanks
If you want to get an ImageView on top of a Button, and you are developing for Android API < 21 (for instance, KitKat = 19), the easiest way is to not use a Button at all and use 2 ImageView instead. Why would you want to do that? May be because you defined a drawable shape to make the button look "cooler", so you are already using android:background with that shape.
Ex:
<Button
android:id="#+id/button01"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/buttonshape"
/>
<ImageView
android:id="#+id/image01"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginTop="10dp"
android:layout_marginStart="10dp"
android:src="#drawable/desiredImageOnTop"
/>
Where #drawable/buttonshape.xml is:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="30dp"
/>
<gradient
android:angle="45"
android:centerColor="#47A891"
android:centerX="35%"
android:endColor="#000000"
android:startColor="#E8E8E8"
android:type="linear"
/>
<padding
android:bottom="0dp"
android:left="0dp"
android:right="0dp"
android:top="0dp"
/>
<size
android:width="100dp"
android:height="100dp"
/>
<stroke
android:width="3dp"
android:color="#878787"
/>
</shape>
In that case, you should replace the Button with an ImageView, change android:background to android:src, and then, in the java code, you just add an OnClickListener as if it was a Button (both controls derive from View, and OnClickListener is a View event). Works like a charm!
Actually, This is not an error, it is a simple design issues. I have been working on it for last 2 hours. And at last get an easy way.
Now, I want to share the code.
<FrameLayout
android:layout_alignParentStart="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<androidx.appcompat.widget.AppCompatButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="4dp"
android:background="#drawable/custom_button"
android:paddingLeft="33dp"
android:layout_marginStart="33dp"
android:text="CHECK APPOINTMENT"
android:textAlignment="viewEnd"
android:textSize="18sp"
/>
</LinearLayout>
<androidx.cardview.widget.CardView
android:layout_width="60dp"
android:layout_height="wrap_content"
app:cardCornerRadius="40dp">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:elevation="2dp"
android:src="#drawable/care_logo"
android:scaleType="fitCenter"
/>
</androidx.cardview.widget.CardView>
</FrameLayout>
It is working.
I placed ImageView on a Button with a diversion of RelativeLayout, hope this helps.
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<View
android:id="#+id/tempview"
android:layout_width="0dp"
android:layout_height="0dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tempview"
android:background="#drawable/ic_launcher" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tempview"
android:src="#drawable/img_cancel" />
</RelativeLayout>

Categories

Resources