I'm making application where I'm trying to add custom popup window with ability to close it when I click back button. The problem is I don't know how to add this back button. I'm attaching a file which shows what I have now and want I want to achieve.
I'm making application where I'm trying to add custom popup window with ability to close it when I click back button. The problem is I don't know how to add this back button. I'm attaching a file which shows what I have now and want I want to achieve.
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(LAYOUT_INFLATER_SERVICE);
popupView = inflater.inflate(R.layout.activity_transaction, null);
final PopupWindow popupWindow = new PopupWindow(popupView, 565, 760, true);
popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
Toolbar toolbar = popupView.findViewById(R.id.tool_bar);
toolbar.setTitleTextColor(Color.WHITE);
toolbar.setTitle("New transaction");
int currentDay = 1;
int currentMonth = Integer.parseInt(actualChosenMonth.getText().toString().substring(0, 2)) - 1;
int currentYear = Integer.parseInt(actualChosenMonth.getText().toString().substring(3, 7));
SimpleDateFormat simpledateformat = new SimpleDateFormat("EE");
Date date = new Date(currentYear, currentMonth, currentDay - 1);
String currentDayOfWeek = simpledateformat.format(date);
String currentDateAsString = convertDateToString(currentDay, currentMonth, currentYear, currentDayOfWeek);
chooseTransactionDate = popupView.findViewById(R.id.chooseTransactionDate);
chooseTransactionDate.setText(currentDateAsString);
chooseTransactionDate.setOnClickListener(this);
chooseTodayTomorrow = popupView.findViewById(R.id.chooseTodayTommorow);
chooseTodayTomorrow.setOnClickListener(this);
chooseTransactionType = popupView.findViewById(R.id.chooseTransactionType);
chooseTransactionType.setOnCheckedChangeListener(this);
chosenTransactionExpense = popupView.findViewById(R.id.chosenTransactionExpense);
chosenTransactionIncome = popupView.findViewById(R.id.chosenTransactionIncome);
transactionCategoryImage = popupView.findViewById(R.id.transactionCategoryImage);
chooseTransactionCategory = popupView.findViewById(R.id.chooseTransactionCategory);
ArrayAdapter<String> chooseCategoryAdapter = new ArrayAdapter<>(this.getActivity(), R.layout.popup_spinner_layout, getResources().getStringArray(R.array.list_of_expenses));
chooseTransactionCategory.setAdapter(chooseCategoryAdapter);
chooseTransactionCategory.setOnItemSelectedListener(this);
chooseTransactionAmount = popupView.findViewById(R.id.chooseTransactionAmount);
chooseTransactionAmount.requestFocus();
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
chooseTransactionName = popupView.findViewById(R.id.chooseTransactionName);
saveTransactionButton = popupView.findViewById(R.id.saveTransactionButton);
saveTransactionButton.setOnClickListener(this);
LAYOUT
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/bacgroundColorPopup"
tools:context=".TransactionActivity">
<Button
android:id="@+id/saveTransactionButton"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_below="@+id/chooseTransactionName"
android:layout_centerInParent="true"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/rounded_button"
android:text="@string/save_transaction"
android:textSize="15sp" />
<EditText
android:id="@+id/chooseTransactionAmount"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_below="@id/transactionCategoryImage"
android:layout_alignParentLeft="true"
android:textColor="@color/BrownGrey"
android:paddingLeft="10dp"
android:hint="Amount"
android:maxLength="5"
android:textColorHint="@color/BrownGreyBrighter"
android:inputType="numberDecimal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/custom_edit_text"
android:drawableRight="@drawable/calculator"
android:textSize="20sp" />
<EditText
android:id="@+id/chooseTransactionName"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_below="@id/chooseTransactionAmount"
android:layout_alignParentLeft="true"
android:textColor="@color/BrownGrey"
android:layout_marginTop="6dp"
android:layout_marginLeft="10dp"
android:paddingLeft="10dp"
android:hint="Notes"
android:textColorHint="@color/BrownGreyBrighter"
android:layout_marginRight="10dp"
android:background="@drawable/custom_edit_text"
android:drawableRight="@drawable/calculator"
android:textSize="20sp" />
<ImageView
android:id="@+id/transactionCategoryImage"
android:layout_margin="6dp"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="@id/chooseTransactionType"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp" />
<LinearLayout
android:id="@+id/dateButtons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
<Button
android:id="@+id/chooseTransactionDate"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="62dp"
android:layout_marginRight="2dp"
android:layout_weight="1.6"
android:background="@drawable/rounded_button"
android:elevation="4dp"
android:padding="7dp"
android:text=""
android:textAllCaps="false"
android:textSize="16sp"
android:textStyle="bold" />
<Button
android:id="@+id/chooseTodayTommorow"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginLeft="2dp"
android:layout_marginTop="62dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:background="@drawable/rounded_button"
android:elevation="4dp"
android:padding="10dp"
android:text="@string/yesterday"
android:textAllCaps="false"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
<RadioGroup
android:id="@+id/chooseTransactionType"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/dateButtons"
android:orientation="horizontal">
<RadioButton
android:id="@+id/chosenTransactionExpense"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:background="@drawable/radio_selector"
android:button="@android:color/transparent"
android:checked="true"
android:elevation="4dp"
android:gravity="center"
android:padding="7dp"
android:text="@string/expense"
android:textColor="@drawable/text_color"
android:textSize="16sp" />
<RadioButton
android:id="@+id/chosenTransactionIncome"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginLeft="1dp"
android:layout_marginTop="5dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:background="@drawable/radio_selector"
android:button="@android:color/transparent"
android:elevation="4dp"
android:gravity="center"
android:padding="7dp"
android:text="@string/income"
android:textColor="@drawable/text_color"
android:textSize="16sp" />
</RadioGroup>
<Spinner
android:id="@+id/chooseTransactionCategory"
android:popupBackground="@color/bacgroundColorPopup"
android:layout_margin="6dp"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_below="@+id/chooseTransactionType"
android:layout_alignParentRight="true"
android:textAlignment="textEnd" />
<include
android:id="@+id/tool_bar"
layout="@layout/tool_bar" />
</RelativeLayout>