How to respond BottomSheetDialogFragment's outside touch events in Android - 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.

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

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");

Action Button Without Notification - Wear

I want to know if it is possible to create this kind of buttons on android wear inside my app (without a notification like on the link).
http://developer.android.com/training/wearables/notifications/creating.html
You can use this view : ActionPage. You can set the text below, the icon inside, the action to perform on click, etc. Check the documentation for more.
Here is an example :
<android.support.wearable.view.ActionPage
android:id="#+id/action_page"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:maxLines="1"
android:textColor="#color/st_grey" />
I used it in a GridViewPager : it was in a fragment.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_action, container, false);
ActionPage actionPage = (ActionPage) view.findViewById(R.id.action_page);
actionPage.setText(actionText);
actionPage.setImageResource(iconResId);
actionPage.setOnClickListener(onClickListener);
return view;
}

How to get rootview in fragment?

I am using this library for using an emoji keyboard on my app.
https://github.com/ankushsachdeva/emojicon
The Readme states that the topmost view of your activity layout hierarchy has to be used to initialize the popupwindow.
My app is implemented via fragments.
This is the code I am using for testing:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.overview1_layout, container,
false);
// Give the topmost view of your activity layout hierarchy. This will be used to measure soft keyboard height
EmojiconsPopup popup = new EmojiconsPopup(view, getActivity());
//Will automatically set size according to the soft keyboard size
popup.setSizeForSoftKeyboard();
popup.showAtBottom();
return view;
}
If I run this code I am getting following error in logcat:
11-02 22:37:16.685: E/AndroidRuntime(30363): java.lang.RuntimeException: Unable to resume activity {com.Testing.full/com.Testing.full.MainActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
EDIT: I am using SherlockFragment
save view as instance member of fragment and initialize Emojicons Popup in OnViewCreated method. That might solve your problem.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.overview1_layout, container,
false);
this.view = view
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// Give the topmost view of your activity layout hierarchy. This will be used to measure soft keyboard height
EmojiconsPopup popup = new EmojiconsPopup(view, getActivity());
//Will automatically set size according to the soft keyboard size
popup.setSizeForSoftKeyboard();
popup.showAtBottom();
}
But for the question title - check here
In onStart() of the Fragment or other Callbacks-Methods after onCreateView call:
emojiconsPopup = new EmojiconsPopup(**getView()**, getActivity());
emojiconsPopup.setSizeForSoftKeyboard();
An alternative is:
emojiconsPopup = new EmojiconsPopup( getView().getRootView() , getActivity());
emojiconsPopup.setSizeForSoftKeyboard();

Android fragments: open context menu programmatically

I need to open context menu by one click on view. I have already register view for context menu.
When I used activity I simply called openContextMenu([view registred for context menu]); but fragment hasn't this method.
What must i do to open context menu by one click in fragment?
getActivity().openContextMenu should work.
Find your view or just inflate, and then save it
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, container, false);
View myView = view.findViewById(R.id.view);
Then you can open context menu using
view.showContextMenu();

Categories

Resources