Cardview In Androidx – Material Design

There are so many properties in Android Material Design. In this tutorial we discuss about cardview. In Material Design Cardview we can design so many things at creative way.

Cardview – It is a material design component in 2014. It is very easy to use and understand for android developers. The main pupose of cardview is a looking good interface in Listview and all other feature like images and text. You can also changed some color in cardview with allegant background and use in proper manner.

How to Implement Cardview in Android App?

First of all you have to add cardview support library in your build.gradle file. And then click on sync button in Android Studio. So After successfull added you can use cardview on your app. Below code of support library.

implementation 'androidx.cardview:cardview:1.0.0'

Now you can add cardview in your XML File.

<androidx.cardview.widget.CardViewxmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_Data"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

Now there are so many properties of cardview.

Give Effect of Cardview clickevent. Code given Below

android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"

if you want to change cardview background color then use this code for the cardview.

card_view:cardBackgroundColor="#FFF" // (XML)

In JAVA

cardView.setCardBackgroundColor(Color.WHITE); // (JAVA)

Give Cardview Corner shape. Use this code

card_view:cardCornerRadius="7dp" // (XML)

In JAVA

[php] cardView.setRadius(0); // (JAVA) [/php]

If you want to give elevation of cardview. Then use this code

[php] card_view:cardElevation=”7dp” // (XML) [/php]

In JAVA

[php] cardView.setCardElevation(2.1f); // (JAVA) [/php]

Give content padding in cardview. Use below code.

[php] card_view:contentPadding=”7dp” // (XML) [/php]

In JAVA

[php] cardView.setContentPadding(30, 30, 30, 0); // (JAVA) [/php]

Simple Listview In Android – Example

Now, We are publish of simple listview example in Android Studio.

In this example, We can give you simple introduction of Listview. It is basic example of Listview Tutorials.

This example use for beginner user who dont know about the android. It is simple example of Listview Tutorials.

Below are the code of the Simple Listview App.

First of all you have to create New Project which is called “Simple Listview” or anything else.

Now Below are the code of MainActivity.java

package com.web.simple.listview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    ListView lst_CountryName;
    // Defined Array values to show in ListView
    String[] worldValues = new String[]{"India",
            "United States",
            "Argentina",
            "Russia",
            "Australia",
            "Afghanistan",
    };

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

        // Get ListView object from xml
        lst_CountryName = (ListView) findViewById(R.id.listing);

        // Define a new Adapter
        // First parameter - Context
        // Second parameter - Layout for the row
        // Third parameter - ID of the TextView to which the data is written
        // Forth - the Array of data

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, android.R.id.text1, worldValues);


        // Assign adapter to ListView
        lst_CountryName.setAdapter(adapter);

        // ListView Item Click Listener
        lst_CountryName.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                // ListView Clicked item index
                int itemPosition = position;

                // ListView Clicked item value
                String itemValue = (String) lst_CountryName.getItemAtPosition(position);

                // Show Alert
                Toast.makeText(getApplicationContext(), " Click Country Name : " + itemValue, Toast.LENGTH_LONG)
                        .show();

            }

        });
    }
}

Now In XML File.

activity_main.xml

<!--?xml version="1.0" encoding="utf-8"?-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

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

</RelativeLayout>

Now output of the project is given below.

Here is the screenshot.