How to add view over DialogFragment? - android

I need to show my simple view on the top off all views. There is opened DialogFragment, my view should be over it.
I try
ViewGroup viewGroup = (ViewGroup) activity.getWindow().getDecorView();
viewGroup.addView(myView);
myView.bringToFront();
viewGroup.requestLayout();
viewGroup.invalidate();
But DialogFragment is still over myView.

Show your view as Another DialogFragment .

Related

There is a way to use AlertDialog inside BottomSheetDialogFragment?

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

Android - dynamically adding view to popup dialog

I have an Alert Dialog that shows some text input fields.
When I enter data and click "OK", it saves this data and displays it in a custom view on the Activity that called the dialog.
If I want to edit this data, I click on the custom view and the same alert dialog pops up. I want to pre-populate the data, and allow editing, and adding new data. However, here is where I'm having a problem.
When I try to do add the existing data to the dialog, the views get added to the top of the view, and not the subview I want them added to. See here:
So the alert dialog inflates the view from my Activity:
View dialogView = LayoutInflater.from(this).inflate(R.layout.material_add_dialog, viewGroup, false);
builder.setView(dialogView);
But this R.layout.material_add_dialog is the main/top view.
I have a custom adapter class that I am using as well. Here is the getView() method:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = Objects.requireNonNull(mInflater).inflate(R.layout.add_material_popup_dialog_row, null);
}
Material material = mMaterials.get(position);
EditText etMaterial = convertView.findViewById(R.id.material_add_text_material);
EditText etSupplier = convertView.findViewById(R.id.material_add_text_supplier);
EditText etWeight = convertView.findViewById(R.id.material_add_text_weight);
EditText etAmount = convertView.findViewById(R.id.material_add_text_amount);
etMaterial.setText(material.getMaterial());
etSupplier.setText(material.getSupplier());
etWeight.setText(material.getUnit());
etAmount.setText(material.getAmount());
return convertView;
}
So if you see convertView, this is inflating R.layout.add_material_popup_dialog_row which is the row that is being added at the top.
So really my question is, how can I add this row to a subview of the dialogs view?
Thanks.
You just need to get, or create, the subview. If it exists:
View dialogView = LayoutInflater.from(this).inflate(R.layout.material_add_dialog, viewGroup, false);
View subView = dialogView.findViewById(R.id.mySubViewId);
Or, you can create a new view and add it to the current view:
//You would need to create the appropriate view for your use-case.
View view = new View();
dialogView.addView(view);
you can try to create the container LinearLayout as root then addView(convertView) and return root

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.

Dynamically Add a overlay view on top of a fragment android

I have this requirement where I have a fragment Instance and I want to dynamically add a view on the top of the fragment irrespective of the root viewGroup of the Fragment.
Something like this
:
So is it possible to get the root view of the fragment from its instance? I know for activity I can do this: activity.findViewById(android.R.id.content);
Also since a fragment can have any type of view group as its rootview like LinearLayout, RecyclerView, RelativeLayout, FrameLayout, Is there a generic way to add an overlay view on the top of the fragment?
If it is not possible then should I just add a dummy view on top of the fragment in xml layout and then use that view to add the overlay?
The getView() method returns a View that contains the Fragment's root layout. From there you could either cast it to ViewGroup and leave it at that to add a simple view, use an if-else block with instanceof to determine its type:
First:
ViewGroup viewGroup = (ViewGroup) fragment.getView();
Second:
View view = fragment.getView();
if (view instanceof FrameLayout) {
FrameLayout root = (FrameLayout) view;
} else if (view instanceof RelativeLayout) {
}
//etc...
Third:
Edit: Removed this option. It actually wont work.
You could use a Child Fragment but if you are only interested in the Fragments root view, just retain the View you are returning in the fragments onCreateView() method

How to add dynamic layout in ViewGroup in android?

I created FrameLayout dynamically in my activity. Like this
FrameLayout path_zoom;
path_zoom = new FrameLayout(this);
path_zoom.setLayoutParams(new FrameLayout.LayoutParams(1500,1119));
Now i want to add this dynamic layout in ViewGroup for layout zooming. I need to add my layout like this.
View v = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.ground_zoom, null, false);
Instead of null i want to pass ViewGroup object. How can i do this? Can any body tell me? Thanks in advance.
Try this
ViewGroup parentView = (ViewGroup)SelectorView.your_context.getParent();
LayoutInflater layoutInflater = (LayoutInflater)activityContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.ground_zoom , parentView);

Categories

Resources