[![enter image description here](https://i.stack.imgur.com/7OUl5.png)](https://i.stack.imgur.com/7OUl5.png) [![enter image description here](https://i.stack.imgur.com/EXcjA.png)](https://i.stack.imgur.com/EXcjA.png) 

```
  public class MainActivity extends AppCompatActivity {
    private CardView cardview;
    private LinearLayout.LayoutParams layoutparams1;
    private RelativeLayout layout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button b1 = findViewById(R.id.button);
        layout=findViewById(R.id.layout);

```

// The button press will add card

```
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cards();
            }
        });
    }

```

// Function to add cardview tiles. This will function will add cardview tile

```
    private void cards()
    {
        cardview = new CardView(getApplicationContext());
        layoutparams1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
        LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutparams1.setMargins(18, 30, 18, 0);
        cardview.setLayoutParams(layoutparams1);
        cardview.setCardBackgroundColor(Color.WHITE);
        cardview.setMinimumHeight(400);
        cardview.setRadius(30);
        layout.addView(cardview);
    }
}

```

// The xml file for the same is mentioned below and containt just a single button within a relative layout:

```
<?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:id="@+id/layout"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save"
        android:id="@+id/button"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp" />
</RelativeLayout>

```

//---------------------------------------
// UPDATE #2 After implementing the onPause() and onResume method -------------------------------//

```
public class MainActivity extends AppCompatActivity {
    private CardView cardview;
    private LinearLayout.LayoutParams layoutparams1;
    private RelativeLayout layout;
    public SharedPreferences mSettings;
    int mCount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mSettings = getSharedPreferences("APP_PREFERENCES", Context.MODE_PRIVATE);
        mCount = 0;

        Button b1 = findViewById(R.id.button);
        layout=findViewById(R.id.layout);

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCount++;
                cards();
            }
        });
    }

    private void cards()
    {
        cardview = new CardView(getApplicationContext());
        layoutparams1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutparams1.setMargins(18, 30, 18, 0);
        cardview.setLayoutParams(layoutparams1);
        cardview.setCardBackgroundColor(Color.WHITE);
        cardview.setMinimumHeight(400);
        cardview.setRadius(30);
        layout.addView(cardview);
    }

    @Override
    public void onPause() {
        super.onPause();
        SharedPreferences.Editor editor = mSettings.edit();
        editor.putInt("COUNT_CARDS", mCount);
        editor.apply();
    }

    @Override
    public void onResume() {
        super.onResume();
        if(mSettings.contains("COUNT_CARDS")) {
            mCount = mSettings.getInt("COUNT_CARDS", 0);
        }
    }
}

```