ActionBar for DialogFragment

I have a DialogFragment that displays in full screen using setStyle(STYLE_NORMAL, R.style.Theme_App) .

DialogFragment shows a penalty, but the up action (the homeAsUp action on the ActionBar) does not work. I tried to implement onOptionsItemSelected in DialogFragment , but it is never called.

Is there a way to get the up action callback in DialogFragment so that I can reject it? For reference, I am using ActionBarCompat .

+6
source share
7 answers

There is no way to bind an ActionBar to a DialogFragment , even if you can set a DialogFragment theme, it will not register as an ActionBar , and Dialog.getActionBar() will always return null.

Instead of getting an ActionBar , you can always attach a layout that looks like an ActionBar and set the functionality on it using the menu.

Another way is to create activity using actionBar as a dialog, you can link to this post

+9
source

This was not possible, but there is a workaround for this using the Toolbar . Now you can include the Toolbar as part of your DialogFragment layout xml and you can set its design / icon to suit your needs. You will also need to implement setNavigationOnClickListener if you want the back button to behave as if it were normal. See Example Class below.

 package com.package.name; import android.app.Dialog; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.DialogFragment; import android.support.v7.widget.Toolbar; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout; public class MyDialogFragment extends DialogFragment { private View parentView; private Toolbar toolbar; @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) { setStyle(DialogFragment.STYLE_NO_FRAME, R.style.Theme_AppCompat_NoActionBar); return super.onCreateDialog(savedInstanceState); } @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //The layout xml file contains the toolbar parentView = inflater.inflate(R.layout.dialogfragment_createpost, container, false); initView(); initData(); return parentView; } private void initView() { toolbar = (Toolbar) parentView.findViewById(R.id.toolbar); } private void initData() { toolbar.setTitle("Post"); //Set naviagtion icon to back button drawable toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha); toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // handle back button naviagtion dismiss(); } }); } } 
+13
source

For DialogFragment to receive calls on onOptionsItemSelected(MenuItem item) , you need to set setHasOptionsMenu(true); in the onCreate () method of the fragment.

Another potential solution is to handle the up action in the onOptionsItemSelected(MenuItem item) action onOptionsItemSelected(MenuItem item) . Something like that:

 @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: // Respond to the action bar Up/Home button onBackPressed(); return true; } } 
+2
source

Just delegate the action up from the component that receives it (i.e. the parent action or fragment) into your DialigFragment. When the meeting occurs, check if your DialogFragment dialog is displayed, and if so, call the appropriate method.

+2
source

Detailed description here .

I solved this problem by adding the following encoding.

 @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: onBackPressed(); return true; } return super.onOptionsItemSelected(item); } 

In MainActivity :

Earlier

public class MainActivity extends BaseActivity

Then i turn to

public class MainActivity extends FragmentActivity

Most likely you need to expand MainActivity to FragmentActivity.

+2
source

The following code worked for me:

 @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { // Respond to the action bar Up/Home button case android.R.id.home: NavUtils.navigateUpFromSameTask(this); return true; } return super.onOptionsItemSelected(item); } 

Hope this helps :)

+1
source

define the toolbar in the layout and call it on the dialog fragment

 Toolbar toolbar=dialog.findViewById(R.id.toolbar); toolbar.setNavigationIcon(R.drawable.ic_arrow_back_black_24dp); toolbar.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { dialog.dismiss(); } }); 
0
source

Source: https://habr.com/ru/post/970602/


All Articles