In my app i have a class called tasti.class
where i have a simple layout
with a ScrollView
and some CardView's
in it.
It also has a button and when i press it it's open a custom AlertDialog
so my question is how could i add other CardViews
after i insert in my custom AlertDialog
the name to place in the TextView
of the CardView
and the "Prezzo" that it should be a description of the textView
i mean by pressing the button "AGGIUNGI" how could i create a new CardView and add it to the bottom of the ScrollView
?
Here is my XML code of the layout part:
<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"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
android:background="@color/blueLight"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".tasti">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/rellay_LOAD"
android:paddingBottom="2dp">
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alignmentMode="alignMargins"
android:columnCount="2"
android:columnOrderPreserved="false"
android:rowCount="4">
<!-- Row 1 -->
<!--Column 1 -->
<android.support.v7.widget.CardView
android:layout_width="50dp"
android:layout_height="40dp"
android:layout_columnWeight="1"
android:layout_marginBottom="2dp"
android:layout_marginEnd="2dp"
android:layout_marginTop="4dp"
android:layout_rowWeight="1"
app:cardElevation="0.50dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CAFFE"
android:textAlignment="center"
android:textSize="14sp"
tools:ignore="HardcodedText" />
</LinearLayout>
</android.support.v7.widget.CardView>
<!--Column 2 -->
<android.support.v7.widget.CardView
android:layout_width="50dp"
android:layout_height="40dp"
android:layout_columnWeight="1"
android:layout_marginBottom="2dp"
android:layout_marginStart="2dp"
android:layout_marginTop="4dp"
android:layout_rowWeight="1"
app:cardElevation="0.50dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CAPUCCINO"
android:textAlignment="center"
android:textSize="14sp"
tools:ignore="HardcodedText" />
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Row 2 -->
<!--Column 1 -->
<android.support.v7.widget.CardView
android:layout_width="50dp"
android:layout_height="40dp"
android:layout_columnWeight="1"
android:layout_marginBottom="2dp"
android:layout_marginEnd="2dp"
android:layout_marginTop="2dp"
android:layout_rowWeight="1"
app:cardElevation="0.50dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HAMBURGER"
android:textAlignment="center"
android:textSize="14sp"
tools:ignore="HardcodedText" />
</LinearLayout>
</android.support.v7.widget.CardView>
<!--Column 2 -->
<android.support.v7.widget.CardView
android:layout_width="50dp"
android:layout_height="40dp"
android:layout_columnWeight="1"
android:layout_marginBottom="2dp"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_rowWeight="1"
app:cardElevation="0.50dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BIRRA 50Cl"
android:textAlignment="center"
android:textSize="14sp"
tools:ignore="HardcodedText" />
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Row 3 -->
<!--Column 1 -->
<android.support.v7.widget.CardView
android:layout_width="50dp"
android:layout_height="40dp"
android:layout_columnWeight="1"
android:layout_marginBottom="2dp"
android:layout_marginEnd="2dp"
android:layout_marginTop="2dp"
android:layout_rowWeight="1"
app:cardElevation="0.50dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PROFITTEROL"
android:textAlignment="center"
android:textSize="14sp"
tools:ignore="HardcodedText" />
</LinearLayout>
</android.support.v7.widget.CardView>
<!--Column 2 -->
<android.support.v7.widget.CardView
android:layout_width="50dp"
android:layout_height="40dp"
android:layout_columnWeight="1"
android:layout_marginBottom="2dp"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_rowWeight="1"
app:cardElevation="0.50dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BIRRA 1l"
android:textAlignment="center"
android:textSize="14sp"
tools:ignore="HardcodedText" />
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Row 4 -->
<!--Column 1 -->
<android.support.v7.widget.CardView
android:layout_width="50dp"
android:layout_height="40dp"
android:layout_columnWeight="1"
android:layout_marginBottom="4dp"
android:layout_marginEnd="2dp"
android:layout_marginTop="2dp"
android:layout_rowWeight="1"
app:cardElevation="0.50dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LIQUORI VARI"
android:textAlignment="center"
android:textSize="14sp"
tools:ignore="HardcodedText" />
</LinearLayout>
</android.support.v7.widget.CardView>
<!--Column 2 -->
<android.support.v7.widget.CardView
android:layout_width="50dp"
android:layout_height="40dp"
android:layout_columnWeight="1"
android:layout_marginBottom="4dp"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_rowWeight="1"
app:cardElevation="0.50dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LIMONCINO"
android:textAlignment="center"
android:textSize="14sp"
tools:ignore="HardcodedText" />
</LinearLayout>
</android.support.v7.widget.CardView>
</GridLayout>
</ScrollView>
<RelativeLayout
android:id="@+id/rellay_LOAD"
android:layout_width="match_parent"
android:layout_height="97dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_centerInParent="true"
android:layout_weight="0.65"
android:background="@color/colorWhite"
android:clickable="true"
android:gravity="center"
android:focusable="true"
android:foreground="?attr/selectableItemBackground"
tools:ignore="NestedWeights,ObsoleteLayoutParam">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:text="CREATE"
android:textSize="14sp"
tools:ignore="HardcodedText" />
</RelativeLayout>
</RelativeLayout>
While here is the code from my tasti.class
public class tasti extends AppCompatActivity {
RelativeLayout rellay_LOAD;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tasti);
rellay_LOAD = findViewById(R.id.rellay_LOAD);
rellay_LOAD.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder mBuilder = new AlertDialog.Builder(tasti.this);
@SuppressLint("InflateParams") View mView = getLayoutInflater().inflate(R.layout.custom_dialog, null);
final EditText bottone = mView.findViewById(R.id.etBottone);
final EditText prezzo = mView.findViewById(R.id.etPrezzo);
Button mLogin = mView.findViewById(R.id.btnADD);
mBuilder.setView(mView);
final AlertDialog dialog = mBuilder.create();
dialog.show();
mLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!bottone.getText().toString().isEmpty() && !prezzo.getText().toString().isEmpty()) {
Toast.makeText(tasti.this,
"Tasto aggiunto con successo",
Toast.LENGTH_SHORT).show();
dialog.dismiss();
} else {
Toast.makeText(tasti.this,
"Non lasciare dei campi vuoti!",
Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
}