I'm using to recyclerview to display a list a images from a web service. I'm trying to display images in ImageView within CardView widget.Everything is working fine. All the images and lists are displaying properly. but when i try to run on Kitkat 4.4.4 its showing extra(Large) padding at top and bottom of image within the Cardview. I'm using ImageView only no text nothing.
Check the attached image.
on 4.4.4
on Lollipop
I've tried almost everything that i've found on SO so far.
any help is appreciated.
Thanks
XML code for Cardview
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_horizontal_margin_half">
<android.support.v7.widget.CardView
android:layout_centerHorizontal="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/offwhite"
android:id="#+id/cardlayout_malls"
android:layout_marginLeft="#dimen/activity_horizontal_margin_half"
card_view:cardUseCompatPadding="true"
android:layout_marginRight="#dimen/activity_horizontal_margin_half">-->
<!-- Thumbnail Image -->
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/bannerImageView"/>
</android.support.v7.widget.CardView>
</RelativeLayout>
try this way
card_view:cardPreventCornerOverlap="false"
OR
in imageview you can use add this in your imageview
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:id="#+id/bannerImageView"/>
addcard_view:cardPreventCornerOverlap="false"
Extra padding is because of those tiny round corners which, need to be disabled. Basically that is not a flaw rather a Design constraint!
Related
Many similar questions were posted here, and I took my time to read them all. Majority of these posts uses ScrollView like this:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#CACACA"
android:fillViewPort="true" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</RelativeLayout>
</ScrollView>
But i dont want to use scrollview. I only use Relative layout which has 2 childs, lottiefile animation and textview to display No Internet Error page. In Android Studio preview shows white background on device size as I expected, but when I test it on my Pocophone F1, relativelayout does not take any background color (even though white color is set). What am I missing here ? Can RelativeLayout fill the device screen background color at all ?
Here are screenshots and code I am working on:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="#FFFFFF">
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/noInternetAnimation"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_centerInParent="true"
app:lottie_autoPlay="true"
app:lottie_loop="true"
app:lottie_rawRes="#raw/internet_error" />
<TextView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_below="#id/noInternetAnimation"
android:layout_centerHorizontal="true"
android:text="#string/no_internet"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
Check the screenshot, app preview on the right has white background as expected.
Now, check this when it is installed on my phone in real device.
Did you try changing your RelativeLayout to a ConstraintLayout just to see if it works? Your XML doesn't look complex at all, so I don't see why is not working
I am creating an App Gallery using CardView as my widget, but I am facing a problem that the images in my gallery are ignoring the size that I want for them, so they are all overlapping instead of creating a margin for it.
I am adapting the following Android Image Gallery:
https://medium.com/#moforemmanuel/android-simple-image-gallery-30c0f00abe64
I want something like that:
But running my code I get this result when I have more than one image:
And the code that I have based myself on you can see below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="190dp"
android:layout_height="190dp">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="1dp"
android:layout_marginLeft=".5dp"
android:layout_marginRight=".5dp"
android:layout_marginBottom=".5dp"
app:cardCornerRadius="1dp">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="center"
tools:ignore="ContentDescription" />
</FrameLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
What should I change in my code so that I can modify the images' size and still maintain a margin so that I will always have an adaptive layout?
Many thanks!
P.S.:
I've tried what Dharmaraj suggested but then I got the following layout:
Changing android:layout_width and android:layout_height to "200sp", it stills overlaps as you can see below:
To load the images I am using this two functions:
Try out this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<androidx.cardview.widget.CardView
android:layout_width="100sp"
android:layout_height="100sp"
android:layout_marginTop="1sp"
android:layout_marginLeft="1sp"
android:layout_marginRight="1sp"
android:layout_marginBottom="1Sp"
app:cardCornerRadius="1sp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5sp>
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_center="true" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
I've basically replaced FrameLayout with RelativeLayout for ease and please assign a constant height and width to the card view. Don't assign a constant height and width to the main layout. This fixed this problem for me. Please let me know if you need more clarification in the comments.
By the way the attribute android:src in image is missing. Or are you assigning image dynamically?
I added a fragment with an ImageView, which is a PNG image, and a TextView just below it.
The fragment is showing fine but when I rotate the screen the image will take most of the screen and the TextView is partially cut.
Here's 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="match_parent">
<ImageView
android:id="#+id/shopping_cart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/shopping_cart" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_below="#id/shopping_cart"
android:text="#string/no_data"
android:gravity="center"/>
</RelativeLayout>
I'm trying to find a way to resize the image in order to show both image and text in the screen
try using a scroll-view as the root element if u want to retain the same image size rather than resizing the image. Here's a small snippet -
<ScrollView
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"
android:fillViewport="true">
<RelativeLayout
android:id="#+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
(all your view elements)
</RelativeLayout>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/text_image_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
(Enter view here)
</LinearLayout>
Alright, so firstly ensure that you have the image for the ImageView in all densities. I would suggest that you use this attribute in your ImageView code:
https://developer.android.com/reference/android/widget/ImageView.ScaleType.html
The scale type defines how your image should scale according to the view. I faced a similar issue once, and i was able to solve it using this. Having said that, i have not seen the image you are using, so i cannot be very sure of it. But, i would say that you use:
android:scaleType="centerCrop"
you can find the definition of the attribute on the link provided above. Also, i do not know what devices you are targeting for your app, but its always good to keep views inside a scroll view so that they are visible even on a smaller screen.
Give it a try, if not by centerCrop, then maybe by some other attribute.
Cheers!!
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="match_parent"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/shopping_text"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="#string/no_data"/>
<ImageView
android:id="#+id/shopping_cart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/shopping_text"
android:src="#drawable/shopping_cart"/>
</RelativeLayout>
I am working on RecyclerView to display the images in CardView.
I use GridLayoutManager and i have specified 3 items in each row:
mLayoutManager = new GridLayoutManager(context,3);
Problem: Images are perfectly fitted in the card view in my testing device.But if i test it in devices smaller than my testing device, right side of the cardview is getting trimmed.
If i reduce the width in the cardview to suite the small devices,then i could see more margin in the right side in larger screens.
My Question:
How to design my xml layout to work for both small and large screens? i.e i don't want the right side edges of the cardview getting trimmed in smaller devices.
Grid_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="match_parent"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="90dp"
android:layout_height="wrap_content"
card_view:cardCornerRadius="3dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
card_view:cardElevation="0.01dp"
android:layout_marginBottom="0dp"
card_view:cardBackgroundColor="#E7A423"
>
<RelativeLayout
android:id="#+id/top_layout"
android:layout_width="90dp"
android:layout_height="120dp"
android:background="#000000"
android:layout_gravity="center">
<ImageView
android:id="#+id/img_thumbnail"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:scaleType="fitXY"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
RecyclerView.xml
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/seperator"
android:columnWidth="100dp"
android:padding="10dp"/>
The problem comes from size of your CardView. Your cardView in small screen is bigger than RecyclerView's item. Your reyclerView's item width are calculated based on recyclerWidth/yourSpanCount (it's 3 in your code). So, I suggest that you can use difference size for you cardView for difference screen-size.
To archive that, you should not declare specific dimen in your xml: android:layout_height="120dp". You can change like: android:layout_height="#dimen/imageWidth".
then you can declare dimens in extra folder like: values-xlarge/dimens.xml, values-small/dimens.xml or even with smallest width if I do not missremember.
this is may be help you => Supporting Multiple Screens
you can use multiple dimens.xml for different devices
I am using a cardview as the root of a custom view I am writing. I using the v7 support library. My XML looks like this:
<?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="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="6dp"
card_view:cardElevation="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- some other views -->
</LinearLayout>
</android.support.v7.widget.CardView>
My problem is that I am getting a white border around my card view. It looks like it is there to indicate elevation as it is thicker on the right side. I've tried adjusting cardElevation and MaxCardElevation in my XML like so :
card_view:cardElevation="0dp"
and in code in my custom view that extends CardView and uses this layout:
setCardElevation(0);
setMaxCardElevation(0);
But the white border persists. I'm not sure how to get rid of it. If anyone had any input into why this is happening or suggestions on how I can remove the white border it would be appreciated. Thanks much.
I know it's a bit late, but for anyone having a similar problem:
I had the same issue: A white border was shown on pre-lollipop devices.
I solved it setting the cardPreventCornerOverlap to false on your XML.
Like this:
<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="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="6dp"
card_view:cardPreventCornerOverlap="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- some other views -->
</LinearLayout>
</android.support.v7.widget.CardView>
Support CardView doesn't support content clipping, because it's expensive on older devices. It's possible to clip content using Canvas.saveLayer/restoreLayer and PorterDuff modes. This is how Carbon implements rounded corners with correct content clipping. See the image:
I might be late in the game, but I had the same issue. Just wanted to share a simple solution!
My custom view extended a CardView and I had a margin applied to the root element (i.e. CardView) in the XML just like in the original question. This ended up giving me the extra white border like this:
Solution was to move the margin from root of the Custom View XML to the declaration of your custom view (check comments in the snippet)
Code snippet:
<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/cvSettings"
style="#style/DefaultCardViewStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/default_margin" <!-- Remove this -->
app:cardElevation="#dimen/default_card_elevation">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
.
.
.
</android.support.v7.widget.CardView>
Ended up just moving over the margin to my custom view declaration which got rid of the extra white border:
<com.example.android.custom.MyCustomView
android:id="#+id/custom_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/default_margin" <!-- Move it here-->
cv:pt_widgetColor="#color/colorAccent" />
After the change, much cleaner :) :
use cardBackgroundColor="color" in xml card_view
sample code :
<android.support.v7.widget.CardView
android:id="#+id/card_view_khaterat"
android:layout_width="250dp"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"
app:cardBackgroundColor="#f56f6c"/>