Dim Screen and Block Interaction with BottomSheets - android

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

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

Assign Dialog box view to another view

I am creating a dialog and setting setContentView of a layout. And I am programmatically adding buttons, images to layout in dialog setContentView . Now how I can assign dialog box view to another view.
That is a layout is assigned to a a view like below
View getview=R.layout.tamil_alphabet_speak_word;
Similarly how can I assign the dialog box view to another view. Since I am adding all elements to the view "TamilAlphabets" programmatically the child are null it returns for the below code.
Alphbetdialog=new Dialog(TamilAlphabets.this);
Alphbetdialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
Alphbetdialog.setContentView(R.layout.tamil_alphabetsdialog);
(adding elements to the layout "TamilAlphabets" code
..............
)
LayoutInflater inflator=(LayoutInflater)TamilAlphabets.this.getSystemService
(TamilAlphabets.this.LAYOUT_INFLATER_SERVICE);
View row=inflator.inflate(tamil_alphabetsdialog, Parent,false);
LinearLayout l1=(LinearLayout)row.findViewById(R.id.alphabetlayout1);
ViewGroup vg=(ViewGroup)l1;
vg.getChildCount();
So I need to assign the dialog box view to another view how do I do that.
I need something like this
View getview=<I need dialog box view>
You should avoid using Dialog class directly and instead use Dialog subclass's or DialogFragment
https://developer.android.com/guide/topics/ui/dialogs.html:
The Dialog class is the base class for dialogs, but you should avoid instantiating Dialog directly. Instead, use one of the following subclasses:
AlertDialog
A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout.
DatePickerDialog or TimePickerDialog
A dialog with a pre-defined UI that allows the user to select a date or time.
In any case im guessing that what you want is a custom dialog and what is recomendaded is using the DialogFragment class in that case here is an example
Dialog fragment layout
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent">
Dialog class
public class DialogExampleFragment extends DialogFragment {
private static final String ARG_PARAM = "extra:PARAM";
private String mText;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle arguments = getArguments();
mText = arguments.getString(ARG_PARAM);
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.setTitle("title");
return dialog;
}
public static DialogExampleFragment newInstance(String message) {
Bundle args = new Bundle();
args.putSerializable(ARG_PARAM, message);
DialogExampleFragment fragment = new DialogExampleFragment();
fragment.setArguments(args);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_dialog_example, container, false);
TextView t = (TextView) root.findViewById(R.id.textView1);
t.setText(mText);
return root;
}
}
To show as a dialog
DialogExampleFragment.newInstance("Message")
.show(getFragmentManager(), "dialog");
Note since a DialogFragment is a fragment it has de advantage of being able to be shown as a Dialog or as a regular fragment you can get all the information on the link i posted above

Get info from another layout to Activity

INTRODUCTION
I'm practicing with canvas-graphics on android. For this I created an app where I can write a line with the finger, and erase it.
When I click the color palette, a dialog window appears with some colors. So I have 2 layouts:
1- The main layout
2- The palette layout
QUESTION
First I got the color palette in the main activity, and for each color button I have an onClick which calls to a method on main Activity.
The thing is that now I'm not able to do this onClick functionality. I think that it has something to do with that now I start this layout as a view instead as a layout, so the onClick functionality of each button doesn't work
This is palette.xml layout's button example:
<ImageButton
android:layout_width="#dimen/large_brush"
android:layout_height="#dimen/large_brush"
android:background="#FF660000"
android:onClick="paintClicked"
android:tag="#FF660000" />
When I click on each button, it starts the paintClicked method an passes the color tag.
So, how do I have to initiate the palette layout when i click on the palette button, to be able to have the information passing from this layout to the main activity?
UPDATE -- Current method to call palette.xml
final Dialog paletteDialog = new Dialog(this);
paletteDialog.setTitle("Colores:");
paletteDialog.setContentView(R.layout.palette);
LinearLayout paletteLayout = (LinearLayout) paletteDialog.findViewById(R.id.paint_colors);
bnColor = (ImageButton) paletteLayout.getChildAt(0);
bnColor.setImageDrawable(getResources().getDrawable(R.drawable.button_pressed));
paletteDialog.show();
Logcat:
java.lang.NoSuchMethodException: paintClicked [class android.view.View]
This is the paintClicked method reference:
public void paintClicked(View view){
I suggest you look at DialogFragment (if you aren't using it already). You can create a simple callback interface so that when a color is clicked, it calls back to the enclosing Activity with this information. Make the enclosing Activity implement the interface (enforced by the onAttach lifecycle method below) and have the dialog fragment call it when a color is picked.
public class ColorPickerDialog extends DialogFragment implements View.OnClickListener {
// Define a callback interface
public interface OnColorSelectedListener {
public void onColorSelected(int color);
}
private OnColorSelectedListener listener;
#Override
public void onAttach(Activity activity) {
if (!(activity instanceof OnColorSelectedListener)) {
throw new IllegalStateException("Activity must implement OnColorSelectedListener!");
}
listener = (OnColorSelectedListener) activity;
}
#Override
public void onDetach() {
listener = null;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = LayoutInflater.from(getActivity());
View view = inflater.inflate(R.layout.palette, container, false);
// call view.findViewById(...) for all your color buttons and
// set the OnClickListener
view.findViewById(...).setOnClickListener(this);
...
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Colores:");
builder.setView(view);
return builder.create();
}
#Override
public void onClick(View v) {
/*
* Determine the color from the view that was clicked. You can use a
* switch statement on v.getId() if they all have IDs, but there are
* other possibilities as well.
*/
int color = ...;
listener.onColorSelected(color);
dismiss();
}
}
android:onClick only works if the Context which expanded the view contains the function ("paintClicked" here).
So if you didn't expand this layout with your main Activity it could the reason it doesn't work.
It might be better for you to set Ids to the ImageButtons and use some findViewById(R.id.btnX).setOnClickListener(myListener); after displaying the buttons.

Make an AlertDialog without using AlertDialog.Builder

I'm trying to create a custom AlertDialog that doesn't use the system style defaults when it uses the Theme.Holo.Light.Dialog theme. I want it to use that theme, but I want to to have the same style as a ListActivity I have using that same theme. Different classes have different styles for the same theme, so it appears I need to create a subclass of the DialogFragment. Another restriction is that I want this dialog to be general. That is, I want to be able to conditionally add buttons, message, title, icon, and items. Hence, it seems that I can't just inflate a DialogFragment from an xml file (or I may be able to if I can create all possible elements I'd want, and then hide the ones I don't want. Is it possible to programmatically build a DialogFragment without inflating it from a single xml file?
EDIT
It looks like this could help: Add controls to custom dialog programatically
I'm working on something using this answer: Dynamically add table row in table and display it in dialog box in android
Why doesn't the button appear when I use this code?
The xml elements I added in layout do appear.
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
return dialog;
}
#Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View contentView = inflater.inflate(R.layout.post_dialog, container);
RelativeLayout layout = (RelativeLayout) contentView.findViewById(R.id.post_dialog_layout);
Button testButton = new Button(getActivity());
testButton.setText("success");
testButton.setLayoutParams(new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
layout.addView(testButton);
return contentView;
}
Everything you need is here and here. Basically in order to build the content of your dialog you should override onCreateView(...), but if you want more control on the Dialog itself you can also override onCreateDialog(...).
Builder pattern is there to help and smooth things, but if you prefer to build stuff your own you can build Dialog instance as well as its content view full-programmatically, without even inflating XML and simply instantiating layout elements at runtime.
You can set arguments Bundle to created DialogFragment and then use them to configure your Dialog. Some of them may be optional and you can use it to detect which option dialog should contain(title, additional buttons, icon).
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import ru.daoffice.R;
public class AlertDialogFragment extends DialogFragment {
private static final String ARG_TITLE = "ArgTitle";
private static final String ARG_MESSAGE = "ArgMessage";
public static DialogFragment newInstance(String title, String message) {
Bundle argumnets = new Bundle();
argumnets.putString(ARG_TITLE, title);
argumnets.putString(ARG_MESSAGE, message);
DialogFragment dialogFragment = new AlertDialogFragment();
dialogFragment.setArguments(argumnets);
return dialogFragment;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setIcon(R.drawable.alert_dialog_icon)
.setTitle(getArguments().getString(ARG_TITLE))
.setMessage(getArguments().getString(ARG_MESSAGE))
.setPositiveButton(android.R.string.ok, null)
.create();
}
}

How can I customize the header layout of a dialog

In Android, is it possible to customize the header layout (the icon + a text) layout of a dialog? Or can I just set a custom string value of the title text?
Thank you.
It's possible to change the header of the Dialog if you set a custom layout for both the dialog and the header. I've only ever used this method to remove the header entirely, but this ought to work for a custom header:
dialog = new Dialog(context);
Window window = dialog.getWindow();
window.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
dialog.setContentView(R.layout.my_dialog_layout);
window.setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.my_custom_header);
This is all a tad more complicated (as you have to setup the dialog's layout as well) but it's easier than subclassing Dialog.
the original Dialog class seems to lack the ability to set an icon, but you can easily extend AlertDialog and set a custom view (the same you would use for your Dialog instance), you just need something like this
class MyDialog extends AlertDialog {
public MyDialog(Context ctx) {
super(ctx);
LayoutInflater factory = LayoutInflater.from(context);
View view = factory.inflate(R.layout.dialog_layout, null);
setView(view);
setTitle("MyTitle");
setIcon(R.drawable.myicon);
}
}

Categories

Resources