How can i create vertical line with radius in cardview? - android

I have a card view with app:cardCornerRadius="20dp"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="140dp"
android:layout_margin="10dp"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
app:cardCornerRadius="20dp">
</android.support.v7.widget.CardView>
</LinearLayout>
Now I want add two vertical solid line to right of this.
like this:
enter image description here
Also vertical line must be have radius.
EDIT:
I write this:
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="140dp"
android:layout_margin="10dp"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
app:cardCornerRadius="50dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:layout_width="5dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:background="#color/colorBlack" />
</RelativeLayout>
</android.support.v7.widget.CardView>
And get this result:
enter image description here
I need radius for line

You can add as many as you want by adding view, like this :
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:layout_margin="#dimen/medium_margin"
android:background="#color/darkGray" />

You could use a View within your cardview xml, this will give you the vertical lines you require as xml.
see this post for ref: vertical line

You just need a 9-patch with two lines. That's it. Something like this:
If you target API 21+, then you don't even need to care about rounded corners. Background will be cut to CardView's outline anyway. If you target older Android versions, then you need to add these rounded corners to background drawable, but that doesn't change much.

Related

CardView doesn't cast shadow inside RecyclerView

I've tried all the stuff.
my manifest is
<application
android:hardwareAccelerated="true"
my layout is :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="240dp"
android:layout_height="200dp">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:layout_above="#+id/tvName"
сard_view:elevation="4dp"
xmlns:сard_view="http://schemas.android.com/apk/res-auto"
сard_view:cardUseCompatPadding="true"
сard_view:cardCornerRadius="5dp">
<ImageView
android:id="#+id/ivPoster"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:adjustViewBounds="true"/>
</androidx.cardview.widget.CardView>
<TextView
android:id="#+id/tvName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom|center_horizontal"
android:layout_alignParentBottom="true"
android:minLines="1"
android:singleLine="true"
android:ellipsize="marquee"
android:textSize="16sp"
android:layout_marginBottom="16dp"/>
</RelativeLayout>
So, inside RecyclerView with grid layout doesn't show any shadow, although it shows it in Android Studio shows shadow in preview
I think it's because ImageView overlaps shadows of the cardview, when you set images on your ImageView in the adapter. Give the image view some margin and shadow will show (but it will be ugly xD).
For testing I display 2 of the Cardviewbut but gave first one src. here is the result:
as you can see first one has no shadows.

How to Make GridLayout Background Transparent?

I am implementing GridLayout in a recycler view with Images loading from the drawable folder.
Image Link here
So as you can see , the background is white and I want to make it Transparent,
My XML code for this layout is
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000"
android:id="#+id/card"
card_view:cardCornerRadius="3dp"
card_view:cardElevation="2dp"
card_view:cardUseCompatPadding="true">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:scaleType="centerCrop" />
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="Abcd"
android:textSize="18dp"
android:textStyle="bold"
android:textAllCaps="true"
android:textColor="#ffffff"
android:layout_marginTop="140dp"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
I have tried a lot of things that such as :-
Making Recycler View's alpha as 0
Card View's Alpha as 0
Setting Card Background Color to Transparent and setting Max Elevation to 0 so as to reduce the shadow
Still, I am not able to get the transparent Background. Can anyone suggest a new way?
Also , the Images used are Vector Images with transparent background.
UPDATE
It works now ,Thanks to #Adley
I just had to remove the card layout that i was using and cast into a normal relative layout
Edited:
Try adding card_view:cardBackgroundColor="#android:color/transparent"
to your root view (CardView).

Add left border to image in android

Is it possible to assign the left border to an image in like my attached image in which it have left strip border?
We need to assign the image to border or we can do it with border image ?
Can anyone please Help?
Thanks in advance.
Here is the sample image:
Result after apply the code:
You can easily achieve this by:
Method 1 - where you don't want to overlap that small part of the image:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<View
android:layout_width="8dp"
android:layout_height="match_parent"
android:background="#FF0000" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/my_image" />
</LinearLayout>
Method 2 - where you want to overlap the image with the border:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/my_image" />
<View
android:layout_width="8dp"
android:layout_height="match_parent"
android:background="#FF0000" />
</RelativeLayout>
try this, I have done it.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/tcit" />
<View
android:layout_width="5dp"
android:layout_height="match_parent"
android:background="#336699" />
</RelativeLayout>
In your layout file, if you need it in landscape, just do it the same orientation which you need, and I have attached a snapshot for this, kindly have a look to it.
You can easily achieve this by creating one layout over another, setting the colors required in your background layout and adding a left margin to the upper layout.
You could always just set a background color (or even another image) to the ImageView and give it a left padding:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff0000"
android:paddingLeft="20dp"
android:src="#drawable/image_res" />

Cardview not showing shadow along bottom

I have the following xml for a cardview:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" android:padding="2dp">
<!-- A CardView that contains a TextView -->
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_gravity="center"
android:layout_width="200dp"
android:layout_height="wrap_content"
card_view:cardCornerRadius="0dp">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<TextView
android:id="#+id/info_text"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
Which results in these shadows which have no shading on the bottom at all
But I am trying to get something like this shadow, that more nicely surrounds the whole card.
I've tried changing the elevation to no avail, and searching on this issue seems to bring up nothing but people with no shadows at all issues which is not my case.
How can I get the shadows right?
Thanks.
You need to add this xml property in your card view
app:cardUseCompatPadding="true"
Adding clipChildren="false" to the parent LinearLayout should also work.
set elevation using card_view:cardElevation property
I was having the same problem until I remembered that if the parent is wrapping its content, then it will not wrap the shadow, so I gave the parent a bigger size than the child that way it will include the child and the shadow of the child
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="110dp"
android:layout_height="110dp">
<androidx.cardview.widget.CardView xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/tools"
android:layout_width="98dp"
android:layout_height="98dp"
app:cardCornerRadius="10dp"
app:cardElevation="5dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
app:srcCompat="#drawable/manhattan" />
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
add these lines in your cardView
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"

Android: how to create a button with a centre line in it?

i want to create a button with green color background and a white line in center, like in the image.
If you want to use a 9 patch (name it btn_green.9.png - mind the extension!), put it in your /res/drawable-mdpi folder.
You can use this one:
This is how it is shown in the draw9patch tool:
This is how to add it:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageButton
android:layout_width="match_parent"
android:layout_height="96dp"
android:background="#drawable/btn_green"
/>
</RelativeLayout>
It only uses 1 View, so it's a superfast (the less Views, the better performances)!!
You can create an ImageButton with Background attribute, and then create a View for the line setting the margin attribute to move it the position you want.
This is a very fast xml, but you can start playing with it
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:layout_width="fill_parent"
android:layout_height="100dp"
android:background="#009900" />
<View
android:layout_width="fill_parent"
android:layout_height="5dp"
android:layout_marginTop="50dp"
android:background="#FFFFFF" /> </RelativeLayout>
If you want to make the button like that, you can use FrameLayout.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="fill_parent"
android:layout_height="100dp"
android:background="#009900" />
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_gravity="center_vertical"
android:background="#FFFFFF" />
</FrameLayout>
The reason why i'm using FrameLayout is to overlap Views. so try this code, i hope it works.

Categories

Resources