0

I need to get ActionBar instance from AppCompatDialogFragment.

public class EditTextFragment extends AppCompatDialogFragment{
    EditText etContent;
    TextEditedListener textEditedListener;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black);
        //get ActionBar belongs to EditTextFragment 
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if(etContent == null){
            etContent = (EditText) inflater.inflate(R.layout.et_content,null);
        }
        return  etContent;
    }
     interface TextEditedListener{
        void onTextEdited(String txt);
    }
}

I have try this,but it seems not work.

 ActionBar actionBar = ((MyActivity) getContext()).getSupportActionBar();
 setCustomActionBar(actionBar);

Thanks for any help

Cyrus
  • 8,995
  • 9
  • 31
  • 58
  • please add description about, what is operation perform on actionbar. – Muhammad Waleed May 05 '17 at 03:35
  • @Javacoder ok,I need two ImageButton and a title TextView on ActionBar. One Button for dismiss dialogFragment, another is to invoke callback method. – Cyrus May 05 '17 at 03:40

2 Answers2

1

you can get action bar using typecasting to ActionBarActivity context.

Try this,

ActionBar actionBar = ((AppCompatActivity)getActivity()).getSupportActionBar();
santosh kumar
  • 2,952
  • 1
  • 15
  • 27
  • ActionBarActivity is deprecated; use `AppCompatActivity` instead. MyActivity is extend AppCompatActivity and it not work. – Cyrus May 05 '17 at 05:37
0

I find the likely answer here.So i solve the problem this way .

1.Create My own toolbar //fake_action_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/fake_action_bar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    app:navigationIcon="@android:drawable/ic_menu_manage"
    android:background="@color/background_material_dark"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" >
    <TextView
        android:id="@+id/actionbar_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:maxLines="1"
        android:clickable="false"
        android:focusable="false"
        android:longClickable="false"
        android:textStyle="bold"
        android:text="title"
        android:textSize="18sp"
        android:textColor="#FFFFFF" />
    </android.support.v7.widget.Toolbar>

2.Add fake_action_bar to my content view

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

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

</LinearLayout>

3.Find the action_bar in AppCompatDialogFragment and init it.

 public class EditTextFragment extends AppCompatDialogFragment {
        ViewGroup mRootView;
        Toolbar toolbar;        
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setStyle(DialogFragment.STYLE_NORMAL, R.style.AppTheme_NoTitleBar_Blue);           
        }
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            if (mRootView == null) {
                mRootView = (ViewGroup) inflater.inflate(R.layout.et_content, null);
                etContent = (EditText) mRootView.findViewById(R.id.et_content);

                toolbar = (Toolbar) mRootView.findViewById(R.id.fake_action_bar);
                toolbar.inflateMenu(R.menu.menu_add);
                toolbar.setNavigationOnClickListener(v -> dismiss());
                toolbar.setOnMenuItemClickListener(item -> {
                    switch (item.getItemId()) {
                        case android.R.id.home:
                            dismiss();
                            return true;
                        case R.id.item_test:
                            if (textEditedListener != null) {
                                textEditedListener.onTextEdited(etContent.getText().toString());
                            }
                            dismiss();
                            return true;
                    }
                    return true;
                });
            }
            return mRootView;
        }
    }
Community
  • 1
  • 1
Cyrus
  • 8,995
  • 9
  • 31
  • 58