Home > Software design >  Android dynamically add cardview into horizontal scroll view
Android dynamically add cardview into horizontal scroll view

Time:03-24

I'm trying to add dynamically cardviews into horizontal scroll view so that I have as many cardviews as I have rows in the database. I have defined cardview in a separated XML file.

But I can't add the cardview into the layout in my main activity as the cardview is null. Is the problem in adding an element from a different file? Should I rather define the cardview in code? Or should I use RecyclerView instead?

I searched a lot but nothing helped yet.

Here is my main activity.

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinearLayout scroll =  findViewById(R.id.layout_horizontal_theme);

        CardView cardView = findViewById(R.id.theme_cardview);

        scroll.addView(cardView);

    }

XML file for card view

<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:layout_marginTop="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginLeft="20dp"
    android:layout_marginBottom="20dp"
    android:contentDescription="@string/card"
    android:minHeight="150dp"
    app:cardBackgroundColor="#FF402D"
    app:cardCornerRadius="16dp"
    android:id="@ id/theme_cardview">

    <!--    app:cardElevation="10dp"-->

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="vertical"
        android:id="@ id/layout_theme_card">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:adjustViewBounds="true"
            android:contentDescription="@string/card_image"
            android:cropToPadding="true"
            android:maxWidth="100dp"
            android:src="@drawable/android_developer" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="10dp"
            android:contentDescription="@string/card_theme"
            android:text="@string/card_theme"
            android:textColor="@color/Not_so_white_white"
            android:textSize="16sp"
            android:textStyle="bold" />

    </LinearLayout>


</androidx.cardview.widget.CardView>

And XML for main activity

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.constraintlayout.widget.ConstraintLayout
        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"
        tools:context=".MainActivity"
        tools:layout_gravity="center">

        <RelativeLayout
            android:id="@ id/relativeLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            app:layout_constraintBottom_toTopOf="@ id/horizontalScrollView"
            app:layout_constraintEnd_toEndOf="@id/guideline2"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="@id/guideline"
            app:layout_constraintTop_toTopOf="parent">

            <include layout="@layout/progress_bar" />
        </RelativeLayout>


        <HorizontalScrollView
            android:id="@ id/horizontalScrollView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:contentDescription="@string/list_scroll"
            android:fillViewport="true"
            app:layout_constraintBottom_toTopOf="@ id/horizontalScrollView2"
            app:layout_constraintEnd_toEndOf="@id/guideline2"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="@id/guideline"
            app:layout_constraintTop_toBottomOf="@ id/relativeLayout">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:id="@ id/layout_horizontal_diff">

                <include layout="@layout/difficulty_card" />

                <include layout="@layout/difficulty_card" />

                <include layout="@layout/difficulty_card" />

                <include layout="@layout/difficulty_card" />

            </LinearLayout>
        </HorizontalScrollView>


        <HorizontalScrollView
            android:id="@ id/horizontalScrollView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/list_scroll_2"
            android:fillViewport="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/horizontalScrollView"
            app:layout_constraintVertical_bias="0.503">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:id="@ id/layout_horizontal_theme">

               <!-- <include layout="@layout/theme_card" />

                <include layout="@layout/theme_card" />

                <include layout="@layout/theme_card" />

                <include layout="@layout/theme_card" />-->

            </LinearLayout>

        </HorizontalScrollView>

        <androidx.constraintlayout.widget.Guideline
            android:id="@ id/guideline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.10" />

        <androidx.constraintlayout.widget.Guideline
            android:id="@ id/guideline2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.90" />


    </androidx.constraintlayout.widget.ConstraintLayout>

    <com.google.android.material.navigation.NavigationView
         android:id="@ id/navDrawer"
         android:layout_height="match_parent"
         android:layout_width="wrap_content"
         app:headerLayout="@layout/navigation_drawer"
         app:menu="@menu/nav_drawer_menu"
         android:layout_gravity="start"
         android:fitsSystemWindows="true"/>


</androidx.drawerlayout.widget.DrawerLayout>

Thank you all for your help:)

CodePudding user response:

You need to inflate your card view and then you can use it for any purpose:

View view = View.inflate(this, R.layout.cardview_layout, null);

You can then cast it to CardView

CardView cardView = (CardView) View.inflate(this, R.layout.cardview_layout, null);

Your code will look like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);

     LinearLayout scroll =  findViewById(R.id.layout_horizontal_theme);
        
     CardView cardView = (CardView) View.inflate(this, R.layout.cardview_layout, null);

     scroll.addView(cardView);

}

CodePudding user response:

follow the example to create custom view. After that, create your custom view and put them to horizontal view.

CardView cardView = findViewById(R.id.theme_cardview); alway return null.

  • Related