How to make imageview in cardview align to left - android

here is my code:
<android.support.v7.widget.CardView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentPaddingLeft="#dimen/activity_horizontal_margin"
app:cardUseCompatPadding="true"
app:cardElevation="4dp">
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView android:layout_width="match_parent" android:src="#drawable/ic_open_in_new_black_48dp"
android:layout_height="match_parent"/>
<TextView android:text="in card view" android:layout_gravity="center_vertical"
android:layout_width="match_parent" android:layout_height="wrap_content"/>
</LinearLayout>
</android.support.v7.widget.CardView>
I hope one line with first ImageView and second TextView, but i get:
how to fix it?

You are using match_parent in android:layout_width and android:layout_height for imageview.
Change it to wrap_content.

Use RelativeLayout inplace of LinearLayout and user "alignParentLeft" for ImageView
<android.support.v7.widget.CardView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentPaddingLeft="#dimen/activity_horizontal_margin"
app:cardUseCompatPadding="true"
app:cardElevation="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:src="#drawable/ic_open_in_new_black_48dp"
android:layout_height="match_parent"
android:alignParentLeft="true"
/>
<TextView
android:text="in card view"
android:layout_gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
</android.support.v7.widget.CardView>

Related

Align Recycler View above linear layout

The list of things expected in the outout
1.a linear layout to the buttom of the activity as shown in the expected image
2.a recycler view above the linear layout to achieve the scrolling funtionality as shown in the expected image
The problem can be solved if I give the height to the recycler view.But in this case the layout won't be responsive to the samller sized screens
How can I achieve it?
xmlfile.xml
<?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"
xmlns:numberpicker="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"
tools:context=".CartPage">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:padding="20dp">
<ImageView
android:id="#+id/back_icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:src="#drawable/ic_arrow_back_black_24dp" />
<TextView
android:id="#+id/product_buy"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_marginLeft="20dp"
android:layout_toRightOf="#+id/back_icon"
android:fontFamily="#font/carter_one"
android:text="My Cart"
android:textColor="#color/black"
android:textSize="20sp"
tools:ignore="RtlCompat" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/product_recycler"
android:layout_width="match_parent"
android:layout_height="400sp"
android:padding="0dp" />
// this is the buttom recycler
<LinearLayout
android:layout_alignParentBottom="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:id="#+id/buttom"
android:gravity="bottom|end"
android:layout_gravity="bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/border_gray"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="#+id/details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/border_gray"
android:fontFamily="sans-serif"
android:lineHeight="28dp"
android:padding="5dp"
android:text="Price Details"
android:textColor="#color/grey_400"
android:textSize="18sp"
tools:ignore="RtlCompat" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
If you wish to tell your RecyclerView to fill the left over space of your LinearLayout you have to use the following layout_height and layout_weight on your RecyclerView:
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/product_recycler"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:padding="0dp" />

How to center CardView inside LinearLayout?

I have a portrait display and I want to translate it to landscape mode.
In landscape mode, first child of encompassing LinearLayout, which is also a LinearLayout, should contain CardView with ImageView (rounded, I'm not sure how is that gonna turn out). In second LinearLayout, I would like to have Username and Password EditText next to each other horizontally. And finally, the last LinearLayout should contain Login and Sign Up buttons also horizontally.
I don't know how to center CardView inside first child LinearLayout and how to make ImageView rounded.
This what I have:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient"
android:gravity="center"
tools:context=".LoginActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1">
<androidx.cardview.widget.CardView
android:id="#+id/imageView"
android:layout_width="150dp"
android:layout_height="match_parent"
app:cardBackgroundColor="#090909"
app:cardCornerRadius="250dp">
<!--<androidx.cardview.widget.CardView
android:layout_width="150dp"
android:layout_height="145dp"
android:gravity="center"
app:cardCornerRadius="250dp">
</androidx.cardview.widget.CardView>-->
</androidx.cardview.widget.CardView>
<!--<ImageView
android:layout_width="150dp"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:scaleType="centerCrop"
android:src="#drawable/login_logo" />-->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<!--HERE WILL BE Username and Password-->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<!--HERE WILL BE Login and Sign up-->
</LinearLayout>
</LinearLayout>
android:gravity handles the alignment of its children,
android:layout_gravity handles the alignment of itself.
If you want to more rounded card view then fix heigh of cardView.
Give weight to other Linear layout "2", and Layout with card view weight "1" for small size.
Thanks You! Happy Coding
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#abcdef"
android:layout_gravity="center"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<androidx.cardview.widget.CardView
android:id="#+id/imageView"
android:layout_width="150dp"
android:layout_height="match_parent"
app:cardBackgroundColor="#090909"
app:cardCornerRadius="150dp">
<!--<androidx.cardview.widget.CardView
android:layout_width="150dp"
android:layout_height="145dp"
android:gravity="center"
app:cardCornerRadius="250dp">
</androidx.cardview.widget.CardView>-->
</androidx.cardview.widget.CardView>
<!--<ImageView
android:layout_width="150dp"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:scaleType="centerCrop"
android:src="#drawable/login_logo" />-->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:orientation="horizontal">
<!--HERE WILL BE Username and Password-->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:orientation="horizontal">
<!--HERE WILL BE Login and Sign up-->
</LinearLayout>
</LinearLayout>
[This is your project(image) => https://i.stack.imgur.com/68ARf.png ]
your problem is simple. just change your orientation your linear layout like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" <!--change this-->
android:layout_weight="1">
<androidx.cardview.widget.CardView
android:id="#+id/imageView"
android:layout_width="150dp"
android:layout_gravity="center" <!-- and this-->
android:layout_height="match_parent"
app:cardBackgroundColor="#090909"
app:cardCornerRadius="250dp">
</androidx.cardview.widget.CardView>
<!--<ImageView
android:layout_width="150dp"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:scaleType="centerCrop"
android:src="#drawable/login_logo" />-->
</LinearLayout>
after this manipulation your cardview image will be in the center

Custom Card not showing as in design

I have been trying for some time now to fix layout.I have a custom cardview like this :this image
This custom card is inside a fragment this one:
<LinearLayout android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/grey"
xmlns:android="http://schemas.android.com/apk/res/android" >
<android.support.v7.widget.RecyclerView
android:layout_width="wrap_content"
android:id="#+id/recycle"
android:layout_height="match_parent"/>
</LinearLayout>
Which itself is inside a viewpager.The problem is when I run the app my custom card becomes full width,I have tried even with RelativeLayout still isn't as I have in my design layout.
Custom_Card Layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
xmlns:card_view="http://schemas.android.com/tools"
android:layout_marginTop="10dp"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="#+id/job_post"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
card_view:cardBackgroundColor="#fff"
card_view:cardCornerRadius="3dp"
card_view:cardElevation="4dp"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="360dp"
android:layout_height="wrap_content"
android:background="#fff">...
Can sb help me?Thanks in advance
Use CardView as your Parent layout in item layout as follows -
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_margin="6dp"
app:cardElevation="#dimen/dimen4"
app:cardBackgroundColor="#fff"
android:id="#+id/job_post"
app:cardCornerRadius="3dp"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_margin="2dp"
android:layout_height="wrap_content"
android:background="#fff">
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="TextView" />
</RelativeLayout>
</android.support.v7.widget.CardView>
Give width of RecyclerView match_parent in your activity or fragment.
Thanks , I hope its work for you
You can use card view in linear layout and then set margin like below:-
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:background="#color/blue"
xmlns:android="http://schemas.android.com/apk/res/android" >
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_margin="20dp"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Cardview Demo"/>
</android.support.v7.widget.CardView>
</LinearLayout>
I hope it will help you and you can set margin of cardview or your required width to cardview

create view over cardview in android

I want to create this layout
this is a cardview (gray view) and imageview(blue view) for that i use this code
<FrameLayout 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="#color/abc_search_url_text_normal"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:layout_marginBottom="3dp"
android:paddingTop="5dp"
app:cardBackgroundColor="#color/abc_input_method_navigation_guard">
</android.support.v7.widget.CardView>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="bottom"
android:layout_marginBottom="30dp"
android:layout_marginRight="10dp"
android:scaleType="fitXY"
android:src="#drawable/abc_ratingbar_full_material" />
</FrameLayout>
but in result my image view going to back of cardview i try reletivelayout instead of framelayout but same result.
The CardView has by default elevation in API 21+, you can manipulate the elevation to be less than that of the ImageView
<android.support.v7.widget.CardView xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="1dp"/>
<ImageView
android:id="#+id/image_view_user"
android:layout_width="48dp"
android:layout_height="48dp"
android:elevation="2dp"/>
in Pre 21 you can use
overlayView.bringToFront();
root.requestLayout();
root.invalidate();
This will not work in 21+ if you have different elevation
Just put CardView in other view like in FrameLayout.
Just like below:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_marginStart="10sp"
android:layout_marginEnd="10sp"
>
<FrameLayout
android:layout_marginTop="15sp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="100sp"
app:cardCornerRadius="5sp"
app:cardBackgroundColor="#color/colorPrimary">
<!--other view items -->
</android.support.v7.widget.CardView>
</FrameLayout>
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="90sp"
android:layout_height="90sp"
app:civ_border_width="2dp"
android:layout_marginStart="10sp"
app:civ_border_color="#color/colorPrimaryDark"
android:id="#+id/profileImg"
android:background="#drawable/ic_if_profle_1055000"
xmlns:app="http://schemas.android.com/apk/res-auto"/>
Screenshot:
Try:
Use a RelativeLayout
Align the ImageView to the parent right/end and add a right/end margin
Align the FAB to the top of the card view and add a negative margin
<RelativeLayout
...>
<CardView
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="120dp"
/>
<ImageView
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#id/cardView"
android:layout_marginTop="-32dp"
android:layout_marginRight="20dp"
/>
</RelativeLayout>
Just add elevation to ImageView greater than CardView elevation
<FrameLayout 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="#color/abc_search_url_text_normal"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:layout_marginBottom="3dp"
android:paddingTop="5dp"
app:cardBackgroundColor="#color/white">
</androidx.cardview.widget.CardView>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="bottom"
android:layout_marginBottom="30dp"
android:layout_marginRight="10dp"
android:scaleType="fitXY"
android:elevation="8dp"
android:src="#drawable/ic_launcher_background" />
You can wrap CardView in FrameLayout but then the shadow will be cropped.
Set android:clipChildren="false" to fix it
<android.support.constraint.ConstraintLayout 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="wrap_content"
android:clipChildren="false"
android:layout_height="140dp">
<FrameLayout
android:id="#+id/frameLayout4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:clipChildren="false">
<android.support.v7.widget.CardView
app:cardCornerRadius="#dimen/card_radius"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.CardView>
</FrameLayout>
</android.support.constraint.ConstraintLayout>
Another way around it would be to wrap the image view in a card view. Just ensure the background is transparent with height and width wrapping content and it will go on top of the previous card view.

ImageView not displaying in android xml (relative layout)

![I had coded it in linear layout (code given below) but want to know how to do the same using relative layout, or is that just a bad idea given the layout?
linear layout -
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:src="#drawable/ocean"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scaleType="centerCrop"
/>
<TextView
android:text="You're invited!"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:textSize="45sp"
android:paddingTop="16dp"
android:paddingLeft="16dp"
android:background="#009688"/>
<TextView
android:text="Bonfire at the beach"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:paddingTop="8dp"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:textSize="24sp"
android:background="#009688"/>
][1]When using relative layout my ImageView does not appear on the screen:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:src="#drawable/ocean"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scaleType="centerCrop"
android:id="#+id/ocean"
/>
</RelativeLayout>
Its because you are using android:layout_height="0dp"
Please use android:layout_height="wrap_content"
Change your ImageView's height like this.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:src="#drawable/ocean"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="centerCrop"
android:id="#+id/ocean"
/>
You can only use layout_weight in LinearLayout.
Try this.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertizal">
<ImageView
android:src="#drawable/ocean"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scaleType="centerCrop"
android:id="#+id/ocean"/>
android:layout_height="0dp" This is wrong in your ImageView
Change this to
android:layout_height="match_parent"
or
android:layout_height="wrap_content"
Basically you set your height as 0. So nothing to show.
Edit :
Also you can remove this android:layout_weight="1". No need for weight in RelativeLayout

Categories

Resources