CardView leaves a gap on top - android

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"

Related

How to center an ImageView on the edge of a cardView?

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>

Background not filling entire screen in scrollview android

I'm developing an app and I need to fill the entire screen with a background, this view contains a scrollview. The problem is that the background is not covering the entire screen, it has a white stripe at the bottom, like the image below.
I have already tried many options of scaleType, like fitXY, centerInside, etc. none of them worked out.
My XML is below.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/signup_background" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="40dp">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/image_view_photo"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_gravity="center"
android:layout_marginBottom="25dp"
android:src="#drawable/camera_icon" />
<android.support.v7.widget.AppCompatEditText
android:id="#+id/edit_text_name"
style="#style/BaseEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/name_icon"
android:hint="#string/field_name"
android:inputType="textCapWords" />
<android.support.v7.widget.AppCompatEditText
android:id="#+id/edit_text_email"
style="#style/BaseEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/email_icon"
android:hint="#string/field_email"
android:inputType="textEmailAddress" />
<android.support.v7.widget.AppCompatEditText
android:id="#+id/edit_text_password"
style="#style/BaseEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/password_icon"
android:hint="#string/field_password"
android:inputType="textPassword" />
<android.support.v7.widget.AppCompatEditText
android:id="#+id/edit_text_confirm_password"
style="#style/BaseEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="35dp"
android:drawableLeft="#drawable/password_icon"
android:hint="#string/field_confirm_password"
android:inputType="textPassword" />
<com.jacksonueda.reachalert.Utils.LoadingButton
android:id="#+id/btn_signup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="#string/action_sign_up" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
</ScrollView>
Can anyone help me? Thanks.
I think that you have to give background to ConstraintLayout with width wrap_content. also set your height for both ScrollView and ConstraintLayout wrap_content.
In constraint layout you have to specify constraint. You don't have specified constraint in ImageView
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/signup_background" />
Replace it with
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/signup_background"
card_view:layout_constraintBottom_toBottomOf="parent"
card_view:layout_constraintLeft_toLeftOf="parent"
card_view:layout_constraintRight_toRightOf="parent"
card_view:layout_constraintTop_toTopOf="parent" />
Try to use RelativeLayout instead of ConstraintLayout in your xml. Relative Layout is best for overlapping two elements of xml.
I got the answer, what I needed to do was to put a RelativeLayout as parent of the ScrollView, like below.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/signup_background"
android:orientation="vertical"
android:padding="40dp">
...
</LinearLayout>
</ScrollView>
</RelativeLayout>

android recyclerview gridview item not showing properly

I am using recyclerview gridview to show product. But its showing like this
this is the view
my xml view code is
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/tools"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="#+id/card_view"
android:layout_width="#dimen/cover_height"
android:layout_height="wrap_content"
android:layout_gravity="center"
card_view:cardUseCompatPadding="true"
android:elevation="7dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.makeramen.roundedimageview.RoundedImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/thumbnail"
android:src="#drawable/placeholder_image"
android:scaleType="fitXY"
app:riv_oval="false"
app:riv_corner_radius_top_left="6dp"
app:riv_corner_radius_top_right="6dp"
android:layout_height="#dimen/cover_height"
android:layout_width="match_parent" />
<pseudozero.smalam.com.finwallet.utils.TextViewPlus
android:id="#+id/tvProductTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/thumbnail"
android:textColor="#color/colorBlack"
android:textSize="15dp"
android:text="Wrist Watch"
android:padding="5dp"
app:customFont="fonts/Proxima_Nova_Bold.otf"/>
<pseudozero.smalam.com.finwallet.utils.TextViewPlus
android:id="#+id/tvProductVendor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tvProductTitle"
android:textSize="13dp"
android:paddingLeft="5dp"
android:paddingBottom="5dp"
android:text="Rolex"
app:customFont="fonts/Proxima_Nova_Reguler.otf"/>
<pseudozero.smalam.com.finwallet.utils.TextViewPlus
android:id="#+id/tvPriceTag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="500 BDT"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:textColor="#color/colorWhite"
android:textSize="13dp"
android:padding="10dp"
android:background="#drawable/price_tag_back_n"
app:customFont="fonts/Proxima_Nova_Bold.otf"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
I am using a card view inside linearlayout. But I am getting this
I am loading all data from server. Only the second item is going up. I want that all items will be decorated.
Use fixed height of CardView and RoundedImageView
Use attribute android:scaleType="centerCrop" instead of android:scaleType="fitXY" to RoundedImageView.
Hope this will help~
RelativeLayout Height should be wrap_content
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
to
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
And in your code you do not close parent LinearLayout.

Excessive padding between Cardviews in RecyclerView?

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

Android minus Margin/Padding

I work with android, I want to draw design like this
There are two LinearLayuots ( blue and white ), I want add picture their borders, I have tryied to use minus margin but not work
Can Anybody help me?
You'd want something like this:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/firstRow"
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="#013032"
android:orientation="horizontal"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="#ff3e7730"
android:layout_below="#+id/firstRow"/>
<ImageView
android:layout_alignParentRight="true"
android:layout_marginRight="30dp"
android:layout_width="140dp"
android:layout_height="140dp"/>
</RelativeLayout>
you can use a RelativeLayout for your layout's parent and then add two LinearLayout and one ImageView or Button. and set your Button (or ImageView,etc) to centerVertical and alignParentLeft and give some marginLeft to 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="wrap_content"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.neo.myapplication.MyActivity">
<LinearLayout
android:id="#+id/aboveLayout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#android:color/darker_gray"
></LinearLayout>
<LinearLayout
android:layout_below="#+id/aboveLayout"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#android:color/holo_green_dark"></LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:src="#drawable/ic_launcher"/>
</RelativeLayout>

Categories

Resources