public class MainActivity extends AppCompatActivity {
private AlertDialog.Builder builder;
private AlertDialog dialog;
private EditText editText;
private Button saveButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final List<MyListData> myListData = new ArrayList<MyListData>();
final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
myListData.add(new MyListData("Email", android.R.drawable.ic_dialog_email));
myListData.add(new MyListData("Info", android.R.drawable.ic_dialog_info));
myListData.add(new MyListData("Delete", android.R.drawable.ic_delete));
myListData.add(new MyListData("Dialer", android.R.drawable.ic_dialog_dialer));
myListData.add(new MyListData("Alert", android.R.drawable.ic_dialog_alert));
myListData.add(new MyListData("Map", android.R.drawable.ic_dialog_map));
final MyListAdapter adapter = new MyListAdapter(myListData);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
builder = new AlertDialog.Builder(MainActivity.this);
View view = getLayoutInflater().inflate(R.layout.popup, null);
editText = view.findViewById(R.id.editText);
saveButton = view.findViewById(R.id.saveButton);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s = editText.getText().toString();
dialog.dismiss();
if (!s.isEmpty()) {
myListData.add(new MyListData(s, android.R.drawable.ic_dialog_alert));
final MyListAdapter adapter = new MyListAdapter(myListData);
recyclerView.setAdapter(adapter);
}
}
});
builder.setView(view);
dialog = builder.create();
dialog.show();
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/enter_item"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textSize="18sp" />
</androidx.appcompat.widget.Toolbar>
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="24dp">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="19dp"
android:autofillHints=""
android:hint="@string/hint_item"
android:textColorHighlight="@color/colorPrimary"
android:textColorLink="@color/colorPrimaryDark"
android:textCursorDrawable="@color/colorPrimaryDark" />
<Button
android:id="@+id/saveButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/editText"
android:layout_marginTop="15dp"
android:layout_marginEnd="5dp"
android:background="?attr/colorPrimary"
android:text="@string/save_button"
android:textColor="@android:color/white"
android:textSize="14sp"
android:textStyle="bold" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>