I am having difficulty reducing padding between Cards in RecyclerView
Padding Issue Screenshots
The screenshots above were taken in Developer Mode's "Show Layout Bounds" option so the issue appears to be with the padding.
When the CardView is removed it returns to normal padding, although I would not make this change because I want CardView
I manually added negative and zero paddings which didn't work.
I also added card_view:cardUseCompatPadding="true" which didn't work either.
My Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp">
<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:id="#+id/card_view"
android:layout_width="fill_parent"
android:layout_height="225dp"
android:layout_margin="10dp"
android:clickable="true"
android:elevation="2dp"
android:foreground="?android:attr/selectableItemBackground"
card_view:cardCornerRadius="2dp"
card_view:cardPreventCornerOverlap="false"
card_view:cardUseCompatPadding="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="-25dp"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/imageView"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_alignLeft="#+id/card_view"
android:layout_gravity="left|center_vertical"
android:scaleType="fitCenter" />
<TextView
android:id="#+id/Ayoub"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="fill_vertical|fill_horizontal"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
Change this :
android:layout_height="225dp"
to :
android:layout_height="wrap_content"
Your first RelativeLayout should wrap_content not match_parent
Related
I have a layout like this:
My xml for that layout:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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="match_parent"
android:fillViewport="true"
tools:context=".view.testviews.ProfileFragment"
android:background="#color/colorDarkGrey">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginStart="20dp"
android:layout_marginTop="100dp"
android:layout_marginEnd="20dp"
app:cardCornerRadius="16dp">
<FrameLayout
android:layout_gravity="center_horizontal"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="-50dp">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/profile_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:src="#drawable/user_image_placeholder"
app:civ_border_color="#FF000000"
app:civ_border_width="2dp">
</de.hdodenhof.circleimageview.CircleImageView>
</FrameLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
I'm trying to center the circleImageView on the edge of the cardView. But the top part of it is invisible. I looked up for similar questions, but they couldn't meet my requirements. How can I achieve this?
So I tried #PPartisan's solution, and it made it like this:
The code for that :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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="match_parent"
android:fillViewport="true"
tools:context=".view.testviews.ProfileFragment"
android:background="#color/colorDarkGrey">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:id="#+id/cardView2"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginStart="20dp"
android:layout_marginTop="150dp"
android:layout_marginEnd="20dp"
app:cardCornerRadius="16dp">
</androidx.cardview.widget.CardView>
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/profile_image"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="90dp"
android:src="#drawable/user_image_placeholder"
app:civ_border_color="#FF000000"
app:civ_border_width="2dp"
>
</de.hdodenhof.circleimageview.CircleImageView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Don't make your ImageView a child of the CardView, as it will be clipped by its parent. Also, when considering XML, the drawing order is specified from top to bottom, which means that Views specified later in the XML will be drawn over any views that are specified earlier. This mean you should draw your CardView first, and then your CircleImageView. Your layout structure should therefore be:
<CardView.../>
<...CircleImageView.../>
I would also recommend you use CoordinatorLayout as it will be simpler to use features like guidelines to correctly align your various views.
Edit: Thankyou Adinia for pointing out in the comments that it is also necessary to set an elevation on the image to ensure it draws on top.
android:layout_gravity="center"
So your layout becomes:
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardCornerRadius="80dp"
app:cardElevation="10dp"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/titelbild280x280"
/>
</android.support.v7.widget.CardView>
I tried everything that i can possibly do but it seems like it requires a different approach to change the color of the black default blackground.
Here is the picture: http://imgur.com/a/CkQGX
Here is the code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#EE7469"
android:orientation="horizontal">
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_margin="8dp"
card_view:cardUseCompatPadding="true"
card_view:cardCornerRadius="10dp"
android:background="#689F38"
card_view:cardElevation="5dp"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/list_item_string"
android:layout_centerVertical="true"
android:layout_alignParentStart="true"
android:paddingStart="8dp"
android:textSize="18sp"
tools:textSize="12sp"
android:background="#689F38"/>
<!-- ADDED SPACER VIEW -->
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#689F38"
android:layout_weight="1"
/>
<!-- /ADDED SPACER VIEW -->
<Button
android:layout_marginTop="10dp"
android:id="#+id/delete_btn"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_gravity="right"
android:background="#android:drawable/ic_delete"/>
<Button
android:layout_marginTop="10dp"
android:id="#+id/edit_btn"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_toStartOf="#id/delete_btn"
android:layout_gravity="right"
android:layout_marginRight="40dp"
android:background="#android:drawable/ic_delete"
/>
</android.support.v7.widget.CardView >
</RelativeLayout>
I tried changing the background color but it's still black.
Try use:
card_view:cardBackgroundColor="#689F38"
So code be like this:
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_margin="8dp"
card_view:cardUseCompatPadding="true"
card_view:cardCornerRadius="10dp"
card_view:cardBackgroundColor="#689F38"
card_view:cardElevation="5dp"
android:layout_height="match_parent">
If you want to change programatically use:
your_cardview.setCardBackgroundColor(your_color);
I thought the problem is in inflating the view in adapter
use below code in layout inflation
View v = inflater.inflate(R.layout.item_layout, parent, false);
You can use a relative layout or linear layout in cardview and set the width and height "match_parent" and set the cardeview color "transparent" as below
android:background="#android:color/transparent"
And set the the linear or relative layout background #689f38
I have a cardview with only one textview.
<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:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:foreground="#drawable/selector_normal"
android:clickable="true"
card_view:cardCornerRadius="2dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/txt_command"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawablePadding="#dimen/padding_standard"
android:padding="#dimen/padding_standard"
android:textColor="#color/colorLightBlue"
android:textSize="#dimen/text_size_medium_small" />
</LinearLayout>
</android.support.v7.widget.CardView>
Apparently the LinearLayout does nothing other than wrapping the TextView. However if I remove the LinearLayout, having the CardView wrap just the TextView, my entire TextView disappear. Is this not allowed?
Is it possible to remove the LinearLayout?
I want to reduce my layout hierarchy by removing redundant layout.
No, it is not necessary to have LinearLayout above than android.support.v7.widget.CardView
<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:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:clickable="true"
android:foreground="#drawable/selector_normal"
card_view:cardCornerRadius="2dp">
<TextView
android:id="#+id/txt_command"
android:layout_width="match_parent" //change here to match_parent
android:layout_height="wrap_content"
android:drawablePadding="#dimen/padding_standard"
android:padding="#dimen/padding_standard"
android:textColor="#color/colorLightBlue"
android:hint="Your Text Here"
android:textSize="#dimen/text_size_medium_small"/>
</android.support.v7.widget.CardView>
On the left is how I would like my app to look, and how it is on the android studio preview. the image is aligned to CardView top.
on the right is how it actually looks. with a gap.
I tried both with linear layout, and with relative layout. doesn't work.
here is my code:
EDIT:
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/testCardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/medium_margin"
android:layout_marginRight="#dimen/medium_margin"
android:animateLayoutChanges="true"
app:cardCornerRadius="3dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/fullCardLayout"
android:layout_width="match_parent"
android:layout_height="#dimen/reward_card_height"
android:orientation="horizontal"
android:paddingLeft="#dimen/medium_margin"
>
<ImageView
android:id="#+id/imgProvider"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/tiny_margin"
android:src="#mipmap/rewards_amazon_icon"/>
.........
Try some like this:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_margin="20dp"
android:src="#mipmap/ic_launcher"/>
</RelativeLayout>
Have you tried negative margin like android:layout_marginRight="-10dp"? Also you can try just put both CardView and ImageView into RelativeLayout as a workaround.
using a minus margin didn't work:
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/testCardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/medium_margin"
android:layout_marginRight="#dimen/medium_margin"
android:animateLayoutChanges="true"
app:cardCornerRadius="3dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/fullCardLayout"
android:layout_width="match_parent"
android:layout_height="#dimen/reward_card_height"
android:orientation="horizontal"
android:layout_marginTop="-5dp"
android:paddingLeft="#dimen/medium_margin"
>
<ImageView
android:id="#+id/imgProvider"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/tiny_margin"
android:src="#mipmap/rewards_amazon_icon"/>[![enter image description here][1]][1]
Try to use this tags.
card_view:cardPreventCornerOverlap="false"
card_view:cardUseCompatPadding="true"
I have a RecyclerView inside a RelativeLayout like so:
<android.support.v7.widget.RecyclerView
android:id="#+id/cards_wrapper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:background="#eeff99"/>
and I have card view within a LinearLayout like so:
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/quote_card"
android:layout_height="200dp"
android:layout_width="match_parent"
card_view:cardBackgroundColor="#666"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="5dp"
app:cardUseCompatPadding="true"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/display_author_name"
android:text="Author Name"
android:textSize="18sp"
android:textColor="#333"
android:gravity="center_horizontal"/>
</android.support.v7.widget.CardView>
the gap between the cards are huge. The next card is seen only after one scroll a bit. What am I missing?
Here the images, before scrolling:
After scrolling:
Try this:
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent" >
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/quote_card"
android:layout_height="200dp"
android:layout_width="match_parent"
card_view:cardBackgroundColor="#666"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="5dp"
app:cardUseCompatPadding="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/display_author_name"
android:text="Author Name"
android:textSize="18sp"
android:textColor="#333"
android:gravity="center_horizontal"/>
</android.support.v7.widget.CardView>
</LinearLayout>
put card view in side a linear layout and make play with padding to change the gap
<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="wrap_content"
*android:padding="1dp"*
>
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
style="#style/list_card"
android:layout_width="match_parent"
android:layout_height="90dp" >
Make these changes in your LinearLayout. If layout_height is match_parent it takes the screens space
android:layout_width="match_parent"
android:layout_height="wrap_content"
you used card view height as match parent so if parent height also match parent than change parent height into wrap content and if you used only card view as parent than also change its height to wrap content..might be it help you