I have created a custom alert dialog with a layout that contains an edit text. What I want is to create a Toast such that on clicking the Yes button of the alert dialog the string inside the edit text gets displayed as the message of the Toast.
Below is my code
This is my 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/myList"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
This is my dialog_layout.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">
<EditText
android:id="@+id/editTextInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:hint="PIN"
android:inputType="number"
tools:ignore="HardcodedText" />
</RelativeLayout>
MainActivity.java
import android.app.AlertDialog;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
AlertDialog alertDialog;
AlertDialog.Builder builder;
EditText editTextPIN;
ListView listView;
List<String> customList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.myList);
customList = new ArrayList<>();
customList.add("George");
customList.add("Brandon");
customList.add("Lucas");
customList.add("Ann");
editTextPIN = findViewById(R.id.editTextInput);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.support_simple_spinner_dropdown_item, customList);
listView.setAdapter(adapter);
listView.setOnItemClickListener((parent, view, position, id) -> {
switch (position) {
case 0:
builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Safaricom");
builder.setMessage("Enter Scratch Card PIN");
builder.setView(R.layout.dialog_layout);
builder.setCancelable(false);
builder.setPositiveButton("YES", (dialog, which) -> {
//I want the String pin to be toasted on clicking the "YES" positive button
String pin = editTextPIN.getText().toString();
Toast.makeText(this, pin, Toast.LENGTH_SHORT).show();
}).setNegativeButton("Cancel", (dialog, which) -> dialog.cancel());
alertDialog = builder.create();
alertDialog.show();
return;
case 1:
Toast.makeText(this, "pos " + position, Toast.LENGTH_SHORT).show();
return;
case 2:
Toast.makeText(this, "pos " + position, Toast.LENGTH_SHORT).show();
return;
case 3:
Toast.makeText(this, "pos " + position, Toast.LENGTH_SHORT).show();
}
});
}
}
Try with the following code and update your dialog code.
public void showDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Safaricom");
builder.setMessage("Enter Scratch Card PIN");
View view = this.getLayoutInflater().inflate(R.layout.dialog_layout, null);
builder.setView(view);
builder.setCancelable(false);
builder.setPositiveButton("YES", (dialog, which) -> {
//I want the String pin to be toasted on clicking the "YES" positive button
EditText editTextPIN = view.findViewById(R.id.editTextInput);
String pin = editTextPIN.getText().toString();
Toast.makeText(this, pin, Toast.LENGTH_SHORT).show();
}).setNegativeButton("Cancel", (dialog, which) -> dialog.cancel());
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}