Pyae Phyo
Pyae Phyo

Reputation: 127

How to do setVisibility in CardView?

How to do setVisibility in CardView? I would like to do setVisibility (GONE) for CardView. I don't want to do LinearLayout setVisibility (GONE). Programmatically. How to do this?

    <android.support.v7.widget.CardView
                    android:id="@+id/cv_ingredient"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="@dimen/cardMarginVertical"
                    android:layout_marginLeft="@dimen/cardMarginHorizontal"
                    android:layout_marginRight="@dimen/cardMarginHorizontal"
                    android:layout_marginTop="@dimen/cardMarginVertical"
                    app:cardBackgroundColor="@color/colorWhite"
                    app:cardCornerRadius="2dp"
                    app:cardElevation="5dp"
                    app:cardPreventCornerOverlap="false"
                    app:contentPadding="0dp">

                    <LinearLayout
                        android:id="@+id/linear_ingredient"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical">

                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="Ingredients"
                                android:textAppearance="?android:attr/textAppearanceMedium" />

                            <ListView
                                android:id="@+id/list_ingredient"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"/>

                    </LinearLayout>

</android.support.v7.widget.CardView>

Upvotes: 0

Views: 10582

Answers (6)

Sikander Bhutto
Sikander Bhutto

Reputation: 226

You can use card view inside linearLayout:

<?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:id="@+id/cv_ingredient"
    app:cardBackgroundColor="@color/colorWhite"
    app:cardCornerRadius="2dp"
    app:cardElevation="5dp"
    app:cardPreventCornerOverlap="false"
    app:contentPadding="0dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="@dimen/cardMarginVertical"
    android:layout_marginLeft="@dimen/cardMarginHorizontal"
    android:layout_marginRight="@dimen/cardMarginHorizontal"
    android:layout_marginTop="@dimen/cardMarginVertical">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ingredients"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ListView
        android:id="@+id/list_ingredient"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp" />
</android.support.v7.widget.CardView>

</LinearLayout>

I used like this in my app, this xml also you can use...

<?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="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    card_view:cardCornerRadius="4dp">

    <RelativeLayout
        android:id="@+id/ll_customer_details_row"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:background="@color/gray_drawer">

        <TextView
            android:id="@+id/cutomerDetails_textView_date"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:textSize="10sp" />

        <TextView
            android:id="@+id/cutomerDetails_textView_name"
            android:layout_width="match_parent"
            android:layout_height="25dp"
            android:layout_below="@+id/cutomerDetails_textView_date"
            android:paddingLeft="5dp" />

        <TextView
            android:id="@+id/cutomerDetails_textView_number"
            android:layout_width="match_parent"
            android:layout_height="25dp"
            android:layout_below="@+id/cutomerDetails_textView_name"
            android:layout_marginLeft="5dp" />

        <ImageButton
            android:id="@+id/callButtonImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/cutomerDetails_textView_name"
            android:layout_marginRight="5dp"
            android:background="@color/transparent"
            android:src="@drawable/phone" />

    </RelativeLayout>
</android.support.v7.widget.CardView>

</LinearLayout>

Upvotes: 0

Naresh Palle
Naresh Palle

Reputation: 369

Just click on invalidate cache/restart in Android studio IDE. It is an IDE error. After that you can able to import the CardView class in java file.

import android.support.v7.widget.CardView;

CardView editProfileLayout = (CardView) rootView.findViewById(R.id.cardview_layout);
editProfileLayout.setVisibility(View.GONE);

Upvotes: 0

Alex Chengalan
Alex Chengalan

Reputation: 8281

You cant do that man!, Because CardView is act as parentView of LinearLayout here. Visibility of parentView will AFFECT the childView also.

Upvotes: 1

Abanoub Samaan
Abanoub Samaan

Reputation: 226

Try to add this property to your second CardView

android:visibility="gone"

Upvotes: 0

Nongthonbam Tonthoi
Nongthonbam Tonthoi

Reputation: 12963

Why don't you just do this:

CardView cv_ingredient = (CardView) findViewById(R.id.cv_ingredient);
cv_ingredient.setVisibility(View.GONE);

Upvotes: 1

Hitesh Sahu
Hitesh Sahu

Reputation: 45140

If you set visibility of parent to GONE then all of child Views will also be GONE, you cannot set linearlayout visible white cardview is gone. I suggest redesign your layout

Upvotes: 0

Related Questions