Cyrus
Cyrus

Reputation: 9425

How could i get ActionBar instance from AppCompatDialogFragment?

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

Upvotes: 0

Views: 225

Answers (2)

Cyrus
Cyrus

Reputation: 9425

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

Upvotes: 0

santosh kumar
santosh kumar

Reputation: 2962

you can get action bar using typecasting to ActionBarActivity context.

Try this,

ActionBar actionBar = ((AppCompatActivity)getActivity()).getSupportActionBar();

Upvotes: 1

Related Questions