Create custom dialog for BottomSheetDialogFragment
This commit is contained in:
parent
4dac033eae
commit
546d87bf14
4 changed files with 157 additions and 28 deletions
18
OsmAnd/res/layout/bottom_sheet_dialog.xml
Normal file
18
OsmAnd/res/layout/bottom_sheet_dialog.xml
Normal file
|
@ -0,0 +1,18 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<android.support.design.widget.CoordinatorLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<View
|
||||
android:id="@+id/touch_outside"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"/>
|
||||
|
||||
<FrameLayout
|
||||
android:id="@+id/content_container"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center_horizontal|bottom"/>
|
||||
|
||||
</android.support.design.widget.CoordinatorLayout>
|
|
@ -256,6 +256,7 @@
|
|||
</style>
|
||||
|
||||
<style name="OsmandLightTheme.BottomSheet">
|
||||
<item name="android:windowIsFloating">true</item>
|
||||
<item name="android:dialogTheme">@style/BottomSheet.Dialog</item>
|
||||
<item name="android:windowBackground">@color/color_transparent</item>
|
||||
<item name="android:backgroundDimAmount">0.3</item>
|
||||
|
@ -263,6 +264,7 @@
|
|||
</style>
|
||||
|
||||
<style name="OsmandDarkTheme.BottomSheet">
|
||||
<item name="android:windowIsFloating">true</item>
|
||||
<item name="android:dialogTheme">@style/BottomSheet.Dialog</item>
|
||||
<item name="android:windowBackground">@color/color_transparent</item>
|
||||
<item name="android:backgroundDimAmount">0.3</item>
|
||||
|
|
127
OsmAnd/src/net/osmand/plus/base/BottomSheetDialog.java
Normal file
127
OsmAnd/src/net/osmand/plus/base/BottomSheetDialog.java
Normal file
|
@ -0,0 +1,127 @@
|
|||
package net.osmand.plus.base;
|
||||
|
||||
import android.app.Dialog;
|
||||
import android.content.Context;
|
||||
import android.content.res.TypedArray;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup.LayoutParams;
|
||||
import android.view.Window;
|
||||
import android.view.WindowManager;
|
||||
import android.widget.FrameLayout;
|
||||
|
||||
import net.osmand.plus.R;
|
||||
|
||||
public class BottomSheetDialog extends Dialog {
|
||||
|
||||
private boolean cancelable = true;
|
||||
private boolean canceledOnTouchOutside = true;
|
||||
private boolean canceledOnTouchOutsideSet;
|
||||
|
||||
public BottomSheetDialog(@NonNull Context context) {
|
||||
this(context, 0);
|
||||
}
|
||||
|
||||
public BottomSheetDialog(@NonNull Context context, int themeResId) {
|
||||
super(context, themeResId);
|
||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||
}
|
||||
|
||||
protected BottomSheetDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
|
||||
super(context, cancelable, cancelListener);
|
||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||
this.cancelable = cancelable;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
Window window = getWindow();
|
||||
if (window != null) {
|
||||
if (Build.VERSION.SDK_INT >= 21) {
|
||||
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
|
||||
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
|
||||
}
|
||||
window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setContentView(int layoutResID) {
|
||||
super.setContentView(wrapInContainer(layoutResID, null, null));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setContentView(@NonNull View view) {
|
||||
super.setContentView(wrapInContainer(0, view, null));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setContentView(@NonNull View view, @Nullable LayoutParams params) {
|
||||
super.setContentView(wrapInContainer(0, view, params));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCancelable(boolean flag) {
|
||||
super.setCancelable(flag);
|
||||
cancelable = flag;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCanceledOnTouchOutside(boolean cancel) {
|
||||
super.setCanceledOnTouchOutside(cancel);
|
||||
if (cancel && !cancelable) {
|
||||
cancelable = true;
|
||||
}
|
||||
canceledOnTouchOutside = cancel;
|
||||
canceledOnTouchOutsideSet = true;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private View wrapInContainer(int layoutResId, View view, LayoutParams params) {
|
||||
final View res = View.inflate(getContext(), R.layout.bottom_sheet_dialog, null);
|
||||
final FrameLayout container = (FrameLayout) res.findViewById(R.id.content_container);
|
||||
|
||||
if (layoutResId != 0 && view == null) {
|
||||
view = getLayoutInflater().inflate(layoutResId, container, false);
|
||||
}
|
||||
if (params == null) {
|
||||
container.addView(view);
|
||||
} else {
|
||||
container.addView(view, params);
|
||||
}
|
||||
|
||||
res.findViewById(R.id.touch_outside).setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
if (cancelable && isShowing() && shouldWindowCloseOnTouchOutside()) {
|
||||
cancel();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
container.setOnTouchListener(new View.OnTouchListener() {
|
||||
@Override
|
||||
public boolean onTouch(View view, MotionEvent motionEvent) {
|
||||
// Consume the event and prevent it from falling through
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
private boolean shouldWindowCloseOnTouchOutside() {
|
||||
if (!canceledOnTouchOutsideSet) {
|
||||
TypedArray a = getContext().obtainStyledAttributes(new int[]{android.R.attr.windowCloseOnTouchOutside});
|
||||
canceledOnTouchOutside = a.getBoolean(0, true);
|
||||
a.recycle();
|
||||
canceledOnTouchOutsideSet = true;
|
||||
}
|
||||
return canceledOnTouchOutside;
|
||||
}
|
||||
}
|
|
@ -5,14 +5,13 @@ import android.graphics.drawable.Drawable;
|
|||
import android.os.Bundle;
|
||||
import android.support.annotation.ColorRes;
|
||||
import android.support.annotation.DrawableRes;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.app.DialogFragment;
|
||||
import android.view.Gravity;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.Window;
|
||||
import android.view.WindowManager;
|
||||
|
||||
import net.osmand.plus.OsmandApplication;
|
||||
import net.osmand.plus.OsmandSettings;
|
||||
|
@ -20,19 +19,18 @@ import net.osmand.plus.R;
|
|||
|
||||
public abstract class BottomSheetDialogFragment extends DialogFragment {
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||
boolean isLightTheme = getMyApplication()
|
||||
.getSettings().OSMAND_THEME.get() == OsmandSettings.OSMAND_LIGHT_THEME;
|
||||
int themeId = isLightTheme ? R.style.OsmandLightTheme_BottomSheet
|
||||
: R.style.OsmandDarkTheme_BottomSheet;
|
||||
final Dialog dialog = new Dialog(getActivity(), themeId);
|
||||
dialog.setCanceledOnTouchOutside(true);
|
||||
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
|
||||
if (!getMyApplication().getSettings().DO_NOT_USE_ANIMATIONS.get()) {
|
||||
dialog.getWindow().getAttributes().windowAnimations = R.style.Animations_PopUpMenu_Bottom;
|
||||
OsmandSettings settings = getMyApplication().getSettings();
|
||||
int themeId = settings.isLightContent() ? R.style.OsmandLightTheme_BottomSheet : R.style.OsmandDarkTheme_BottomSheet;
|
||||
|
||||
BottomSheetDialog dialog = new BottomSheetDialog(getContext(), themeId);
|
||||
Window window = dialog.getWindow();
|
||||
if (!getMyApplication().getSettings().DO_NOT_USE_ANIMATIONS.get() && window != null) {
|
||||
window.getAttributes().windowAnimations = R.style.Animations_PopUpMenu_Bottom;
|
||||
}
|
||||
dialog.setCanceledOnTouchOutside(true);
|
||||
|
||||
return dialog;
|
||||
}
|
||||
|
||||
|
@ -40,21 +38,6 @@ public abstract class BottomSheetDialogFragment extends DialogFragment {
|
|||
@Override
|
||||
public abstract View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
|
||||
if (getDialog() != null) {
|
||||
final Window window = getDialog().getWindow();
|
||||
WindowManager.LayoutParams params = window.getAttributes();
|
||||
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
|
||||
params.gravity = Gravity.BOTTOM;
|
||||
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
|
||||
window.setAttributes(params);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected OsmandApplication getMyApplication() {
|
||||
return (OsmandApplication) getActivity().getApplication();
|
||||
}
|
||||
|
@ -66,5 +49,4 @@ public abstract class BottomSheetDialogFragment extends DialogFragment {
|
|||
protected Drawable getContentIcon(@DrawableRes int drawableRes) {
|
||||
return getMyApplication().getIconsCache().getThemedIcon(drawableRes);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue