How to Add Cardview in Layout Xml in Androidx

How to add cardview in layout xml in AndroidX

The class has changed to:

androidx.cardview.widget.CardView

If you want to use the old class names, you need to add android:enableJetifier=true to your gradle.properties and use the appcompat dependencies instead.

Android dynamically add cardview into horizontal scroll view

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);

}

How to add layout into card view?

Try this...

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

Create an object to your layout

View child1 = LayoutInflater.from(this).inflate(
R.layout.extra, null);

cardView.addView(child1);

How to add CardView In Center With LinearLayout

You have to use match_parent for your Root layout height instead of wrap_content and instead of android:layout_gravity="center" use android:gravity="center"

Copy below code

<?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:gravity="center"
android:orientation="vertical"
android:background="@drawable/image">
<androidx.cardview.widget.CardView
android:id="@+id/card_view"
android:layout_width="330dp"
card_view:cardElevation="4dp"
android:layout_height="60dp"
card_view:cardCornerRadius="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center">
<Spinner
android:id="@+id/simpleSpinner"
android:layout_width="wrap_content"
android:layout_height="match_parent"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="number"
android:maxLength="10"
android:hint="Enter phone number" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<Button
android:id="@+id/buttonOTP"
android:layout_marginTop="20dp"
android:layout_width="330dp"
android:gravity="center"
android:layout_height="50dp"
android:text="Send OTP" />
</LinearLayout>

How to add cardview programmatically inside view but use layout declared in xml?

As i write in comment you have to deal with visiblity and. I'm happy to see that it works

as you say you want to make this smooth then try this:

In layout

android:animateLayoutChanges="true"

Fade out animation while visibltity invisible

view.animate().alpha(0.0f);

Fade in animation while visiblity visible

view.animate().alpha(1.0f);

Android XML Insert a layout on another layout

This example could solve your problem

fragment_main.xml

<?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" >

<include
android:id="@+id/innerLayout"
layout="@layout/inner_layout" />

</RelativeLayout>

inner_layout.xml

<?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:id="top_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="inner text" />

</RelativeLayout>

Now in your MainFragment class

Depending on your need you can set the visibility of the inner_layout by using the following code snippet

 View innerLayout = findViewById(R.id.innerLayout);

//For hiding the innner layout dynamically
innerLayout.top_layout.setVisibility(View.GONE);

//For showing the innner layout dynamically
innerLayout.top_layout.setVisibility(View.VISIBLE);

//to set text in TextView
innerLayout.text.setText("Anything you want");

border around a cardview

Use your drawable file i.e. @drawable/card_edge inside the child layout (in my case LinearLayout but You can use any layout as per your requirements.) of cardview and it will create UI as per your requirement.

<androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:clickable="true"
android:focusable="true"
xmlns:android="http://schemas.android.com/apk/res/android">

<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="8dp"
app:cardCornerRadius="8dp"
android:padding="5dp"
android:elevation="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/card_edge"
>
.
.
.
</LinearLayout>

</androidx.cardview.widget.CardView>

</androidx.constraintlayout.widget.ConstraintLayout>


Related Topics



Leave a reply



Submit