Is it possible to use ActionBar in popup window

54 views Asked by At

In my Class which extends ListFragment I have a popup window which shows when I click one button. My problem is I don't know if I can use ActionBar in it. I mean I want to get an back arrow and it should close (dismiss) popup when it gets clicked. I mean: something like this:

    toolbar = findViewById(R.id.tool_bar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle("Enter transaction");
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Is such functionality even possible? Or rather should I use customizable toolbar with added ImageView use set listener to close popup? Or maybe I should create custom Dialog?

I'm attaching a file which shows what I have now and want I want to achieve. image

This is fragment of my class

public class LastTransactionsFragment extends ListFragment implements, View.OnClickListener{

    //popupWindow
    private View popupView;
    private Button chooseTransactionDate;
    private Button chooseTodayTomorrow;
    private RadioGroup chooseTransactionType;
    private RadioButton chosenTransactionExpense;
    private RadioButton chosenTransactionIncome;
    private ImageView transactionCategoryImage;
    private Spinner chooseTransactionCategory;
    private EditText chooseTransactionAmount;
    private EditText chooseTransactionName;
    private Button saveTransactionButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_last_transactions, container, false);

        addTransactionActionButton = view.findViewById(R.id.add_transaction_action_button);
        addTransactionActionButton.setOnClickListener(this);

        return view;
    }


    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.add_transaction_action_button: {
                // inflate the layout of the popup window
                LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(LAYOUT_INFLATER_SERVICE);
                popupView = inflater.inflate(R.layout.activity_transaction, null);

                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);

//I cut rest of of listener 

                saveTransactionButton = popupView.findViewById(R.id.saveTransactionButton);
                saveTransactionButton.setOnClickListener(this);
                break;
            }


            default:
                break;
        }
    }

And this is my layout file (with custom toolbar)

    <?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">

    <include
        android:id="@+id/tool_bar"
        layout="@layout/tool_bar" />

    <Button
        android:id="@+id/saveTransactionButton"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_below="@+id/chooseTransactionName"
        android:layout_centerInParent="true"/>

//I cut rest of layout


</RelativeLayout>
1

There are 1 answers

0
H4wk On

I solved my problem. I used this:

((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
((AppCompatActivity) getActivity()).getSupportActionBar();
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);