Should CardView always wrap a layout first? - android

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>

Related

Cannot see textview

I am trying to make a layout with this code :
<?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:card_view="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
>
<android.support.v7.widget.CardView
app:cardCornerRadius="#dimen/cardview_corner_radius"
app:cardElevation="2dp"
card_view:cardUseCompatPadding="true"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="#f27181"
android:id="#+id/charity_overview_card"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.sharesmile.share.views.LBTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="#color/white"
android:padding="9dp"
android:text="Women Empowerment"
android:id="#+id/iv_charity_category_title"
/>
<ImageView
android:background="#color/colorPrimaryDark"
android:layout_width="128dp"
android:layout_height="126dp"
android:id="#+id/iv_charity_overview"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<com.sharesmile.share.views.LBTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_charity_amount"
android:textColor="#color/black"
android:textSize="15sp"
android:text="Test"
/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
but the last textview is not visible, even in the preview it is not showing, it shows like this :
As you can see the textview is not visible, i haven't given any weight, still facing this issue.
Also when i increase the size of the textview the value is visible as you can see here :
LinearLayout inside other parent LinearLayout with dynamic size will produce this type of issues. You should avoid nesting layout inside layout as much as possibile.
This issue can be simply fixed by removing the parent layout orientation.
For improvement
<CardView>
<LinearLayout> vertical
//Add 'N' child items here
</LinearLayout>
</CardView>
Change the following properties of your main LinearLayout, Make height and width to match_parent
android:layout_width="match_parent"
android:layout_height="match_parent"

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

too much gap between cardviews in a recyclerview

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

Android Landscape mode cutting off my last EditText

I've got a problem in my application.
My layout contains:
ImageView
Button
Four EditText
When I change the emulator in landscape mode the ScrollView goes to the third EditText. The last EditText is not displayed in landscape mode.
My layout.xml
<?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">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="60dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="235dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/formulario_foto"
android:background="#00A8EC"
android:src="#drawable/person"/>
<Button
android:layout_width="56dp"
android:layout_height="56dp"
android:id="#+id/formulario_foto_button"
android:layout_alignParentBottom='true'
android:layout_alignParentRight="true"
android:layout_marginBottom="5dp"
android:layout_marginRight="20dp"
android:background="#drawable/formulario_foto_button"
android:elevation="5dp"
android:gravity="center"
android:textColor="#FFFFFF"
android:textSize="20sp"/>
</RelativeLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:hint="Nome"
android:id="#+id/editTextNome" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:ems="10"
android:hint="E-mail"
android:id="#+id/editTextEmail" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:hint="Endereço"
android:id="#+id/editTextEndereco" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:inputType="phone"
android:ems="10"
android:hint="Telefone"
android:id="#+id/editTextTelefone" />
</LinearLayout>
</ScrollView>
It looks like ScrollView does not respect the margin of its child. This behavior has been noted in
Android ScrollView not respecting child's bottom margin
Scrollview doesn't scroll to the margin at the bottom
Scrollview extends beyond screen bottom
The workaround is to wrap your LinearLayout in a FrameLayout, ie.
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="60dp"
android:orientation="vertical">
...
</LinearLayout>
</FrameLayout>
This is slightly inefficient, since it uses an extra ViewGroup. If you are able to do so, the more efficient solution would be to use paddingTop on your LinearLayout instead of the layout_marginTop, ie.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="60dp"
android:orientation="vertical">
...
</LinearLayout>
Change linear layout height to wrap_content

My rotated TextView is cut off. What i have to do?

This is my layout
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<!--left-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:background="#color/nero"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Nothing important"
android:textSize="40dp"/>
</LinearLayout>
<!--right yellow column-->
<LinearLayout
android:id="#+id/layout_barra_nord"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="6"
android:background="#color/giallo"
android:orientation="vertical">
<TextView
android:paddingTop="20dp"
android:background="#android:color/background_light"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:rotation="90"
android:singleLine="true"
android:gravity="center"
android:layout_gravity="bottom"
android:textColor="#color/nero"
android:textSize="30sp"
android:textStyle="bold"
android:text="try"
/>
<!--android:text="long text"-->
</LinearLayout>
</LinearLayout>
</LinearLayout>
i'd like to have this effect for every text:
but if I try to add a longer text, my text will be cut off:
I see that lot of people have this problem, but I haven't found anything helpful.
How to fix this problem?
Your TextView width is match_parent which means your TextView will have the same width as your parent LinearLayout. Your LinearLayout have a narrow width so your TextView will have a narrow width too.
EDIT: You have to think of your TextView as if he is on the same orientation as your parent. If he had the same orientation you would have the TextView with a narrow width.
Change the following attribute of TextView
android:layout_height="wrap_content"
to
android:layout_height="match_parent"
so that it stretch down to the full parent height.

Categories

Resources