Android full screen dialog fragment like calendar app - android

I am trying to achieve a full-screen dialog like the below image. I am able to show a full screen dialog but when the dialog is shown the status bar color changes to black and does not keep the primary-dark color.
Heres my dialog fragment
public class IconsDialogFragment extends DialogFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout to use as dialog or embedded fragment
return inflater.inflate(R.layout.fragment_icons_dialog, container, false);
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final RelativeLayout root = new RelativeLayout(getActivity());
root.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
// creating the fullscreen dialog
final Dialog dialog = new Dialog(getActivity());
dialog.setContentView(root);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.YELLOW));
dialog.getWindow().setWindowAnimations(R.style.DialogAnimation);
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
return dialog;
}
}

To get DialogFragment on full screen
Override onStart of your DialogFragment like this:
#Override
public void onStart()
{
super.onStart();
Dialog dialog = getDialog();
if (dialog != null)
{
int width = ViewGroup.LayoutParams.MATCH_PARENT;
int height = ViewGroup.LayoutParams.MATCH_PARENT;
dialog.getWindow().setLayout(width, height);
}
}
In order to setStatusBarColor you need to set the flag: FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS
public void setStatusBarColorIfPossible(int color) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().setStatusBarColor(color);
}
}

One way is to change back your status bar color as theme programmatically whenever you open the dialog.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Your theme color);
}

Related

Android studio Expand Bottom Sheet on button click Inside its Fragment

I have a Bottom Sheet and I want to add a button in which the bottom sheet dialog expands on click
enter image description here
Is it doable to put onclicklistener inside bottom sheet fragment onCreateView and change the state of the dialog?
heres my Bottom Sheet Fragment
public class RepliesToComment extends BottomSheetDialogFragment {
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
BottomSheetDialog dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);
Window window = dialog.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
dialog.getBehavior().setState(BottomSheetBehavior.STATE_COLLAPSED); <--change to STATE_EXPANDED on click-->
return dialog;
}
ImageButton expandBtn;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_replies_to_comment, container, false);
{...}
expandBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
----
}
});
}
I manage to make it work heres my solution
dialog to global then made a boolean identifier to switch the state of the dialog
boolean identifier = true;
BottomSheetDialog dialog;
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);
Window window = dialog.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
return dialog;
}
public void expand(){
if (!identifier){
dialog.getBehavior().setState(BottomSheetBehavior.STATE_COLLAPSED);
}else {
dialog.getBehavior().setState(BottomSheetBehavior.STATE_EXPANDED);
}
}
then made the button into toggle button to change the value of the identifier
//expand
expandBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
identifier = b;
expand();
}
});

Incoming Call Screen obstruction by an Activity (Theme Dialog) over it

I am trying to show an Activity (with Theme Dialog) over an Incoming call Screen to show some information. I am done with the that, but the problem is whenever the call comes, Activity Dialog pops up over it and it covers SLIDER ( for accepting/Rejecting calls) .
I want the Activity Dialog over Incoming call screen but still wants the user to pick/reject calls.
I did use this But now unable to finish activity(Dialog).
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
Warm Regards
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
setContentView(R.layout.notedialog);
this.setFinishOnTouchOutside(false);
initializeContent();
phone_no = getIntent().getExtras().getString("phone_no");
String note = getIntent().getExtras().getString("note");
tv_client.setText(phone_no + " is calling you");
note_mEditText.setText(note);
dialog_ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.this.finish();
// this.setFinishOnTouchOutside(false);
System.exit(0);
}
});
No need for an Activity with dialog style.
Call this method only when you want to show the dialog.
This will create a dialog on top of your phone.
final Dialog dialog;
public void showDialog(){
dialog = new Dialog(this,
android.R.style.Theme_Translucent_NoTitleBar);
// Setting dialogview
Window window = dialog.getWindow();
window.setGravity(Gravity.TOP);
window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
dialog.setTitle(null);
dialog.setContentView(R.layout.notedialog);
dialog.setCancelable(true);
dialog.show();
}
And to cancel the dialog call this method.
public void cancelDialog(){
if(dialog!= null ){
dialog.dismiss()
}
}
OR
If you insist to use an Activity dialog You can use this method to change the position of window.
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
View view = getWindow().getDecorView();
WindowManager.LayoutParams lp = (WindowManager.LayoutParams) view.getLayoutParams();
lp.gravity = Gravity.CENTER | Gravity.TOP
lp.x = 10;
lp.y = 10;
lp.width = 300;
lp.height = 300;
getWindowManager().updateViewLayout(view, lp);
}

How can I make a BottomSheetDialogFragment non-modal?

In onCreateView() I've tried setting some properties, but the DialogFragment still goes away on tapping outside.
I would like the fragment to remain nearly all the time while the user interacts with other parts of the app.
This is in a class that extends BottomSheetDialogFragment:
#Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
// makes background non-interactive
getDialog().getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
// prevents dimming of background
getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
// no effect?
getDialog().setCanceledOnTouchOutside(false);
this.setCancelable(false);
return view;
}
Code like:
public class MyDialogFragment extends DialogFragment {
...
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle(...)
.setMessage(...)
.setCancelable(false);
...
Dialog dialog = builder.create();
dialog.getWindow();
return dialog;
}
...
}
works for me.

Android BottomSheetDialogFragment that does not take whole width

How do I make BottomSheetDialogFragment that does not cover entire width of the screen (something like "Share via" sheet in Chrome on tablets)?
As a developer suggested in the issue linked by #UfoXp, the problem is BottomSheetDialog.onCreate() setting the window to MATCH_PARENT both ways:
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Dialog dialog = super.onCreateDialog(savedInstanceState);
if ((isTablet(getContext()) || isLandscape(getContext()))) {
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialogINterface) {
dialog.getWindow().setLayout(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.MATCH_PARENT);
}
});
}
return dialog;
}
private boolean isTablet(Context context) {
return (context.getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK)
>= Configuration.SCREENLAYOUT_SIZE_LARGE;
}
private boolean isLandscape(Context context) {
return context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
}
Just use getWindow().setLayout() after showing the Dialog
dialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.MATCH_PARENT);
Looks like this is not possible right now:
https://code.google.com/p/android/issues/detail?id=204670

Can't get transparent DialogFragment

I have a dialog Fragment which look like that.
AlertDialog ad = builder.create();
Drawable d = new ColorDrawable(Color.BLACK);
d.setAlpha(130);
ad.getWindow().setBackgroundDrawable(d);
This code get background semi transparent. But I still got a white part on the bottom. I want to get rid of the white to just have semi transparent background
I already tried a lot of stuff that I saw in other posts.
I don't know what is the object that I must change between the DialogFragment, the AlertDialog and the LinearLayout.
It may not be the LinearLayout, because when I increase margin, nothing is moving.
Here is my code :
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// setStyle(DialogFragment.STYLE_NORMAL, 0);
// setStyle(STYLE_NO_FRAME, R.style.CustomDialog);
// setStyle(STYLE_NO_FRAME, 0);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View view = getActivity().getLayoutInflater().inflate(
R.layout.share_or_die, null);
AlertDialog ad = builder.create();
Drawable d = new ColorDrawable(Color.BLACK);
d.setAlpha(130);
ad.getWindow().setBackgroundDrawable(d);
ad.setCanceledOnTouchOutside(true);
ad.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
ad.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
return ad;
}
I just call it in the mainActivity when user click back button:
#Override
public void onBackPressed() {
if (isUserConnected && !hasShared) {
shareOnExitDialog = new ShareOnExitDialog();
shareOnExitDialog.setCancelable(true);
shareOnExitDialog.show(getSupportFragmentManager(), "Exit");
} else {
finish();
}
}
Any help would be appreciated !
The issue is in default dialog theme. Based on this and this answers it's much easier to achieve your target.
The Activity should be like the following:
public class MyActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
}
#Override
public void onBackPressed() {
MyDialogFragment.newInstance("title title").show(getSupportFragmentManager(), "dialog");
}
}
And fragment:
public class MyDialogFragment extends android.support.v4.app.DialogFragment {
/**
* Create a new instance of MyDialogFragment, providing "title"
* as an argument.
*/
static MyDialogFragment newInstance(String title) {
MyDialogFragment frag = new MyDialogFragment();
Bundle args = new Bundle();
args.putString("title", title);
frag.setArguments(args);
return frag;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = new Dialog(getActivity(),android.R.style.Theme_Translucent_NoTitleBar);
final View view = getActivity().getLayoutInflater().inflate(R.layout.share_or_die, null);
final Drawable d = new ColorDrawable(Color.BLACK);
d.setAlpha(130);
dialog.getWindow().setBackgroundDrawable(d);
dialog.getWindow().setContentView(view);
final WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.gravity = Gravity.CENTER;
dialog.setCanceledOnTouchOutside(true);
return dialog;
}
}
Also, be sure to do the following: before setContentView() in the activity, getWindow().requestFeature(Window.FEATURE_NO_TITLE) should be added in order to use *NoTitleBar style.
The result is (I've used LinearLayout with single TextView inside):
I hope it can help you
#Override
public void onStart() {
super.onStart();
Window window = getDialog().getWindow();
WindowManager.LayoutParams windowParams = window.getAttributes();
windowParams.dimAmount = 0.0f;
window.setAttributes(windowParams);
}
None of the above solutions worked for me. What i found to work was to make sure that I had imported android.support.v7.app.AlertDialog instead of the generic app.alertdialog.
Hope this helps others too.
It works for me:
getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

Categories

Resources