There is a way to use AlertDialog inside BottomSheetDialogFragment? - android

i just need an Alert Dialog with title, msg and buttons, but showed as bottom sheet.
where is a way to obtain this (without custom view)?

Instead of using AlertDialog inside of bottomsheetdialogfragment.
Create one bottomsheetdialog which behaves like your requirement.
Please refer
https://medium.com/glucosio-project/moving-from-dialogs-to-bottomsheetdialogs-on-android-15fb8d140295
How to use BottomSheetDialog?

the best way is to use BottomSheetDialogFragment as you said and to set it a custom view that you wants with only title, msg and buttons
BottomSheetDialog dialog = new BottomSheetDialog(YourActivity.this);
dialog.setContentView(YourView);
dialog.show();

The AlertDialog and the BottomSheetDialog extend AppCompatDialog both but have different implementation.
Since the layout is simple (just title, message and buttons), it is easier to use a BottomSheetDialog with a custom layout, rather than to use a AlertDialog and adapt all the behaviour and animations of the BottomSheet.
Just use the BottomSheetDialogFragment (which creates a BottomSheetDialog):
public class MyBottomSheetDialog extends BottomSheetDialogFragment {
#Nullable #Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
//Use your custom layout
View view = inflater.inflate(R.layout.yourLayout, container, false);
return view;
}
}
And then
MyBottomSheetDialog myBottomSheetDialog = new MyBottomSheetDialog();
myBottomSheetDialog.show(getSupportFragmentManager(), "TAG");

I answer to my question: Simply no, you're forced to use a CustomView

Related

How to respond BottomSheetDialogFragment's outside touch events in Android

I want to prevent dialog dismissing and response touch event behide the dialog when I touch outside of the BottomSheetDialogFragment, so I do like this in my BottomSheetDialogFragment class:
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View dialogView = inflater.inflate(R.layout.poi_result_bottom_dialog, container, false);
this.getDialog().setCanceledOnTouchOutside(false);
return dialogView;
}
However, I find if I set 'setCanceledOnTouchOutside(false)', my activity under the dialog can't respond touch event when I touch outside of the dialog.
You should not be using a BottomSheetDialog. What you want is referred as a Persistent Bottom Sheet. It is well described with instructions at androidhive.info by using an embedded View, and setting up a BottomSheetBehavior.

Creating a floating activity like in contacts app

I've been looking for how to implement an activity which won't be in full screen but not a dialog style. It is desired to be as follows:
I've tried some methods like AppTheme Dialog but I'm not getting the result I'm expecting. Any ideas how can I achieve that?
The best solution is to use BottomSheetBehaviour
It is pretty easy to use and has a nice animation. Take a look at a simple guide here. Sorry, I can't post the whole process here in detail. But for a quick implementation.
Create a class
public class CustomBottomSheetDialogFragment extends BottomSheetDialogFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.my_layout, container, false);
return v;
}
}
Fill in your bottom by calling the class
new CustomBottomSheetDialogFragment().show(getSupportFragmentManager(), "Dialog");

Dim Screen and Block Interaction with BottomSheets

BottomSheetBehavior has been introduced in Android Design Support Library 23.2, however it does not dim the rest of the screen and does not block interaction with the rest of the UI. Is there anyway this can be achieved?
public class BottomSheetDimmedFragment extends BottomSheetDialogFragment {
public static final String TAG = BottomSheetDimmedFragment.class.getSimpleName();
#NonNull
#Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {
final BottomSheetDialog dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);
final View view = View.inflate(getContext(), R.layout.test, null);
dialog.setContentView(view);
return dialog;
}
public void show(final FragmentActivity fragmentActivity) {
show(fragmentActivity.getSupportFragmentManager(), TAG);
}
}
In your activity:
BottomSheetDimmedFragment sheet = new BottomSheetDimmedFragment();
sheet.show(this);
Now, you will have a dim and also when clicked on a dim the dialog will close.
Implementation taken from here.
Use the bottom sheet with a fragment instead of a view :)
Note that there are two implementations:
BottomSheetBehavior and BottomSheetDialogFragment.
Use BottomSheetDialogFragment to get the functionality you need.
Also when using BottomSheetBehavior set the layout's android:clickable="true". That way clicks don't go through when you click on empty space. (For clarity: clickable is set on the layout containing the tag app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior")

How center custom dialog title on Android

I'm writing a custom dialog on android.
I did this using the onCreateView method.
public class CheckpointLongPressDialog extends DialogFragment {
public void CheckpointLongPressDialog() {}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_checkpoint_long_press_dialog, container);
getDialog().setTitle("TITLE");
return view;
}
How can i center the title programmatically?
Maybe its not the best way, I use a custom title TextView.
TextView title = new TextView(mainActivity);
title.setText(alertTitle);
title.setBackgroundResource(R.drawable.gradient);
title.setPadding(10, 10, 10, 10);
title.setGravity(Gravity.CENTER); // this is required to bring it to center.
title.setTextSize(22);
getDialog().setCustomTitle(title);
I solve the problem using a builder and inflating the xml layout.
private AlertDialog.Builder builder;
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.fragment_checkpoint_long_press_dialog, null));
// Create the AlertDialog object and return it
return builder.create();
}
Try this..
final int titleId = getActivity().getResources().getIdentifier("alertTitle", "id", "android");
TextView title = (TextView) getDialog().findViewById(titleId);
if (title != null) {
title.setGravity(Gravity.CENTER);
}
What if you use the whole layout to inflate also your custom title?. Instead of getDialog().setTitle("TITLE"); you can also include a TextView in your custom layout for the title.
The title view is using default theme. You have 2 ways to do what you want, first one is better for having a more customized experience:
Use this to have a dialog without title, and then make custom title bar in the layout of this fragment.
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
Extend the default theme for the dialog and update it, then set it in this dialog.

Problem inflating custom view for AlertDialog in DialogFragment

I'm trying to create a DialogFragment using a custom view in an AlertDialog. This view must be inflated from xml. In my DialogFragment class I have:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setTitle("Title")
.setView(getActivity().getLayoutInflater().inflate(R.layout.dialog, null))
.setPositiveButton(android.R.string.ok, this)
.setNegativeButton(android.R.string.cancel, null)
.create();
}
I have tried other inflation methods for .setView() such as:
.setView(getActivity().getLayoutInflater().inflate(R.layout.dialog, (ViewGroup) getView(), false))
and
.setView(getActivity().getLayoutInflater().inflate(R.layout.dialog, (ViewGroup) getTargetFragment().getView(), false))
After setting the target fragment in the fragment that is showing this dialog.
All of these attempts to inflate my custom view result in the following exception:
E/AndroidRuntime(32352): android.util.AndroidRuntimeException: requestFeature() must be called before adding content
E/AndroidRuntime(32352): at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:214)
E/AndroidRuntime(32352): at com.android.internal.app.AlertController.installContent(AlertController.java:248)
E/AndroidRuntime(32352): at android.app.AlertDialog.onCreate(AlertDialog.java:314)
E/AndroidRuntime(32352): at android.app.Dialog.dispatchOnCreate(Dialog.java:335)
E/AndroidRuntime(32352): at android.app.Dialog.show(Dialog.java:248)
E/AndroidRuntime(32352): at android.support.v4.app.DialogFragment.onStart(DialogFragment.java:339)
E/AndroidRuntime(32352): at android.support.v4.app.Fragment.performStart(Fragment.java:1288)
E/AndroidRuntime(32352): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:873)
E/AndroidRuntime(32352): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1041)
E/AndroidRuntime(32352): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:625)
E/AndroidRuntime(32352): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1360)
E/AndroidRuntime(32352): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:411)
E/AndroidRuntime(32352): at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime(32352): at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(32352): at android.os.Looper.loop(Looper.java:132)
E/AndroidRuntime(32352): at android.app.ActivityThread.main(ActivityThread.java:4028)
E/AndroidRuntime(32352): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(32352): at java.lang.reflect.Method.invoke(Method.java:491)
E/AndroidRuntime(32352): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
E/AndroidRuntime(32352): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
E/AndroidRuntime(32352): at dalvik.system.NativeStart.main(Native Method)
While if I try to use the DialogFragment's getLayoutInflator(Bundle) like this:
.setView(getLayoutInflater(savedInstanceState).inflate(R.layout.dialog, null))
I get a StackOverflowError.
Does anyone know how to inflate a custom view for an AlertDialog in a DialogFragment?
The first error line gives me the hint that this is related to how you are creating your dialog - not the dialog itself.
Are you creating the dialog automatically (which could mean this gets called before the views are all set up) or in response to a button click? I initially had problems with fragments due to instantiation order.
I used the same code to set the view as you have, and my result works. I cut out the other setup to make this look cleaner, but it works with or without it.
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_layout, null);
builder.setView(view);
return builder.create();
}
I'm surprised by these answers as none of them solve the problem.
A DialogFragment allows you to reuse the same UI for both a dialog and integrated in your app elsewhere as a fragment. Quite a useful feature. As per google's documentation, you can achieve this by overriding onCreateDialog and onCreateView.
http://developer.android.com/reference/android/app/DialogFragment.html
There are three scenarios here:
Override onCreateDialog only - Works as a dialog but cannot be
integrated elsewhere.
Override onCreateView only - Does not work as a dialog but can be
integrated elsewhere.
Override both - Works as a dialog and can be integrated
elsewhere.
Solution:
The AlertDialog class is calling another class which calls requestFeature. To fix this.. Don't use the AlertDialog, instead use a plain Dialog or whatever super.onCreateDialog returns. This the solution that I have found works best.
Caveat:
Other dialogs such as DatePickerDialog, ProgressDialog, TimePickerDialog all inherit from AlertDialog and will likely cause the same error.
Bottom Line:
DialogFragment is good if you need to create very customized interface that needs to be used in several places. It doesn't appear to work to reuse existing android dialogs.
Avoid request feature crash and use same layout:
public class MyCombinedFragment extends DialogFragment
{
private boolean isModal = false;
public static MyCombinedFragment newInstance()
{
MyCombinedFragment frag = new MyCombinedFragment();
frag.isModal = true; // WHEN FRAGMENT IS CALLED AS A DIALOG SET FLAG
return frag;
}
public MyCombinedFragment()
{
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
if(isModal) // AVOID REQUEST FEATURE CRASH
{
return super.onCreateView(inflater, container, savedInstanceState);
}
else
{
View view = inflater.inflate(R.layout.fragment_layout, container, false);
setupUI(view);
return view;
}
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder alertDialogBuilder = null;
alertDialogBuilder = new AlertDialog.Builder(getActivity());
View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_layout, null);
alertDialogBuilder.setView(view);
alertDialogBuilder.setTitle(“Modal Dialog“);
alertDialogBuilder.setPositiveButton("Cancel", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
dialog.dismiss();
}
});
setupUI(view);
return alertDialogBuilder.create();
}
}
I had the same problem. In my case it was becasue Android Studio created a template onCreateView that re-inflated a new view instead of returning the view created in onCreateDialog. onCreateView is called after onCreateDialog, so the solution was to simply reurnt the fragments view.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return this.getView();
}
Faced the same issue, and it took lot of time to get rid of the error. Finally passing resource ID to setView() method solved the problem. Add set view as below:
.setView(R.layout.dialog)
In your code where you call
create().
Replace with
show().
I haven't inflated from XML but I have done dynamic view generation in a DialogFragment successfully:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
m_editText = new EditText(getActivity());
return new AlertDialog.Builder(getActivity())
.setView(m_editText)
.setPositiveButton(android.R.string.ok,null)
.setNegativeButton(android.R.string.cancel, null);
.create();
}
What you want to do instead is create your custom view in the onCreateView method like you normally would. If you want to do something like change the title of the dialog, you do that in onCreateView.
Here's an example to illustrate what I mean:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getDialog().setTitle("hai");
View v = inflater.inflate(R.layout.fragment_dialog, container, false);
return v;
}
Then you just call:
DialogFragment df = new MyDialogFragment();
df.show(..);
And voila, a dialog with your own custom view.
As i needed long time for solving the same problem (Pop up a simple Text Dialog) i decided to share my solution:
The layoutfile connectivity_dialog.xml contains a simple TextView with the message text:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:text="Connectivity was lost"
android:textSize="34sp"
android:gravity="center"
/>
</RelativeLayout>
The Activity showing the "dialog" implements a inner class (as DialogFragment is a Fragment and not a Dialog; further info see https://stackoverflow.com/a/5607560/6355541). The Activity can activate and deactivate the DialogFragment via two functions. If you're using android.support.v4, you may want to change to getSupportFragmentManager():
public static class ConnDialogFragment extends DialogFragment {
public static ConnDialogFragment newInstance() {
ConnDialogFragment cdf = new ConnDialogFragment();
cdf.setRetainInstance(true);
cdf.setCancelable(false);
return cdf;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.connectivity, container, false);
}
}
private void dismissConn() {
DialogFragment df = (DialogFragment) getFragmentManager().findFragmentByTag("conn");
if (df != null) df.dismiss();
}
private void showConn() {
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment prev = getFragmentManager().findFragmentByTag("conn");
if (prev != null) ft.remove(prev);
ft.addToBackStack(null);
ConnDialogFragment cdf = ConnDialogFragment.newInstance();
cdf.show(ft, "conn");
}

Categories

Resources