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);
// Нажатие кнопки добавит карту
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cards();
}
});
}
// Функциячтобы добавить плитки cardview.Эта функция добавит плитку вида карты
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);
}
}
// 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: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>
// --------------------------------------- // ОБНОВЛЕНИЕ № 2 После реализации onPause () иМетод onResume ------------------------------- //
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);
}
}
}