Actionbar in a Dialogfragment

ActionBar in a DialogFragment

using the idea from a google group post I was able to pull it off styling an activity. you would want to modify the height and width to a "dynamic" size of your choice preferably. Then set whatever ActionBar buttons you would like

<style name="PopupTheme" parent="android:Theme.Holo.Light.Dialog">
<item name="android:windowIsFloating">false</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowSoftInputMode">stateAlwaysHidden</item>
<item name="android:windowActionModeOverlay">true</item>
<item name="android:windowIsTranslucent">true</item>
</style>

--

public static void showAsPopup(Activity activity) {
//To show activity as dialog and dim the background, you need to declare android:theme="@style/PopupTheme" on for the chosen activity on the manifest
activity.requestWindowFeature(Window.FEATURE_ACTION_BAR);
activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND,
WindowManager.LayoutParams.FLAG_DIM_BEHIND);
LayoutParams params = activity.getWindow().getAttributes();
params.height = 850; //fixed height
params.width = 850; //fixed width
params.alpha = 1.0f;
params.dimAmount = 0.5f;
activity.getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
setContentView(R.layout.activity_main);
}

Up ActionBar action on DialogFragment

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

Instead of getting the ActionBar you can always attach a Layout that will look like an ActionBar and set the functionality on it using menu.

The other way is to create an activity with actionBar as a Dialog you can refer to this post

Full Screen DialogFragment (over ActionBar) in Android

So, after search a lot, i didn't find anyway to make the DialogFragment overlays the actionbar using API 5. But i find a alternative way to do that making the Fragment calling a new activity on the screen with the theme of a dialog. This solution help me a lot, so, im sharing it here:
https://stackoverflow.com/a/12275009/2327056

@StrikeForceZero using the idea from a google group post I was able to pull it off
styling an activity. you would want to modify the height and width to
a "dynamic" size of your choice preferably. Then set whatever
ActionBar buttons you would like

<style name="PopupTheme" parent="android:Theme.Holo.Light.Dialog">
<item name="android:windowIsFloating">false</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowSoftInputMode">stateAlwaysHidden</item>
<item name="android:windowActionModeOverlay">true</item>
<item name="android:windowIsTranslucent">true</item>
</style>

--

public static void showAsPopup(Activity activity) {
//To show activity as dialog and dim the background, you need to declare android:theme="@style/PopupTheme" on for the chosen activity on the manifest
activity.requestWindowFeature(Window.FEATURE_ACTION_BAR);
activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND,
WindowManager.LayoutParams.FLAG_DIM_BEHIND);
LayoutParams params = activity.getWindow().getAttributes();
params.height = 850; //fixed height
params.width = 850; //fixed width
params.alpha = 1.0f;
params.dimAmount = 0.5f;
activity.getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
setContentView(R.layout.activity_main);
}

Make a Dialogfragment in android full screen without actionbar

replace setstyle with this

dialog.getWindow().requestWindowFeature(Window.FEATURE_NO_TITLE);

And put before

dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);

How could i get ActionBar instance from AppCompatDialogFragment?

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


Related Topics



Leave a reply



Submit