How to implement shadow in Android? - android

My UX designer has provided me with an elegant design and exported it as an Xd file. For shadows of some elements, Xd gives me below information:
Shadow color: #101F400D
X: 0dp
Y: 3dp
Blur: 10dp
How can I create this layout in android with the above information?
Note: elevation is not what I want!

You can use cardView or Material design cardview to get the shadow effect
If you want to follow the guideline of material design you have to implement dependency
implementation 'com.google.android.material:material:1.2.0-alpha02'
You can use material cardview as shown below
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="4dp"
app:cardBackgroundColor="#color/colorWhite">
</androidx.constraintlayout.widget.ConstraintLayout>

You have to use a nine patch image to achieve this. Create a nine patch image using any tool like this - https://inloop.github.io/shadow4android/
Then, set this image as background to your view.

Related

I want to add a circular profile image inside my constraintlayout which is inside the cardview which is again inside cnstrntlyout like whtsappp

Like this (watsapp SS)
I'm stuck with it I can not find how to round profile pics like this and than I don't know how to constraint my my 3 text views basically I'm trying to create something like this
Screenshot of what I'm trying to say
The problem in the above screenshot (which is of a tutorial on youtube) he used vector asset as an profile but I want to use jgp files and than round it and constraint my 3 textviews accordingly like I said.
I would love some help. Thanks.
Try ShapeableImageView in your android project.
To use above view in android, you need to add dependency material design 1.2.0 or higher.
implementation 'com.google.android.material:material:1.2.0'
In your style.xml add,
<style name="ShapeAppearanceOverlay.App.CornerSize50Percent" parent="">
<item name="cornerSize">50%</item>
</style>
In your layout file , add this view
<com.google.android.material.imageview.ShapeableImageView
android:layout_width="150dp"
android:layout_height="150dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="#mipmap/ic_launcher"
app:strokeColor="#android:color/darker_gray"
android:layout_margin="10dp"
app:shapeAppearanceOverlay="#style/ShapeAppearanceOverlay.App.CornerSize50Percent"
/>
Result
You can try these libraries
CircleImageView
or
RoundedImageView
According to your requirements
You can either make the shapes yourself in the drawable folder which is a lot of work or use third party libraries like hdodenhof/CircleImageView.
I recommend using the third party library so that you can access it in your xml layouts like any other components

Android Widget Shadow which supports from API 16

I want to have shadow in Android widget like button, edittext, searchview or anything. I have no idea about android elevation. I think it only supports from API 23. If there is any proper solution then please provide.
here is my demo widget. I need the shadow in border of my searchview.
<android.support.v7.widget.SearchView
android:id="#+id/searchView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroud="#android:color/white" />
You can get the shadow effect by adding this to the widget:
android:background="#android:drawable/dialog_holo_light_frame"
Got the answer from here: https://stackoverflow.com/a/30931750/8466860.
Place your views inside CardView. Using CardView you can add elevation, rounded corners etc. It supports back till API 7.
to use CardView, add it via your app level gradle..
dependencies {
...
compile 'com.android.support:cardview-v7:25.3.1'
}

CardView - Adding cuts on left and right side with shadow

I have to get this type of card design with semi-circle on both side of cardview along with shadows on both card view and semi-circles.
Thanks in advance.
I found a solution here that create a custom view called TicketView.
It provide various option to set the radius of the arc and divider options.
You can use it as custom view.
<com.vipulasri.ticketview.TicketView
android:layout_width="match_parent"
android:layout_height="160dp"
android:layout_marginTop="60dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:id="#+id/ticketView"
app:orientation="vertical"
app:scallopRadius="10dp"
app:showBorder="false"
app:scallopPositionPercent="50"
app:showDivider="true"
app:dividerType="dash"
app:ticketElevation="14dp"/>
It's not possible using Elevation API. A shadow is generated using view's ViewOutlineProvider with a convex path outlining the view. Your view is not convex, so you cannot make a proper ViewOutlineProvider, so you cannot get a nice, generated shadow that way.
The easiest way is to use a 9-patch with these cutouts drawn on it.

Android Material Ripples

I would like to follow material design guidelines in my application. For tappable text should I use unbounded or bounded surface ink ripples (displayed when the user presses on the text)?
I used this Custom ripple layout for myself, I think it is easy to use:
RippleLayout
and you can use it like this in your xml file:
<your.package.name.RippleLayout
android:layout_width="40dp"
android:layout_height="40dp"
app:mrl_rippleSelectedColorState="#android:color/transparent">
<!-- other views like buttons and.... -->
</your.package.name.RippleLayout>

android.support.design.widget.FloatingActionButton implenetation of FAB reduces image size

So I have started migrating to the official support design library for an Android app of mine, replacing elements as I go. I just replaced an FAB (implemented as ImageButton) with official android.support.design.widget.FloatingActionButton. But the icon inside the button is smaller as compared to the one it was in ImageButton. What may have been a reason?
FloatingActionButton's implementation:
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon" />
Tried adding app:fabSize="normal but that didn't work, either.
This is how it looks after using FloatingActionButton:
And this is how it looked like while using it as a ImageButton:
Set scale type to android:scaleType="center"
Just a guess since you haven't provided any details at all. The design library's FloatingActionButton has padding calculations which will shrink the icon if it is too large, whereas your ImageButton probably just centers the icon with no scaling at all. I'm guessing that you would not observe this behavior if you were using a correctly sized 24dp icon, like the ones you get from: https://www.google.com/design/icons/.
You can use : app:maxImageSize="..dp" to change the size of your icon!

Categories

Resources