I am trying to add custom views(chekbox and two radio buttons) as shown in image below in alertdialog but not succeded.
Please suggest me a way to get views as shown in the image.
Thanks in advance!!
Use DialogFragment instead of AlertDialog.
public class CustomDialogFragment extends DialogFragment {
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = LayoutInflater.from(getActivity());
View viewRoot = inflater.inflate(R.layout.dialog_view, null);
//do something with your view
builder.setView(viewRoot);
return builder.create();
}
}
R.layout.dialog_view - it's your view, which you want to display
You can build dialog with custom layout. Here's some tutorial how to do that:
http://www.mkyong.com/android/android-custom-dialog-example/
Related
I use this code to create a custom AlertDialog:
val dialog = AlertDialog.Builder(context)
.setView(R.layout.layout)
.create()
The problem is I cannot get the inflated view. dialog.findViewById(R.id.a_view_in_the_layout) returns null.
Alternatively, I can use .setView(View.inflate(context, R.layout.layout, null) but this sometimes makes the dialog fill the screen and take more space than setView(int layoutResId).
If I remember correctly, create sets up the Dialog, but its layout is not inflated until it needs to be shown. Try calling show first then, then finding the view you're looking for.
val dialog = AlertDialog.Builder(context)
.setView(R.layout.layout)
.create()
dialog.show() // Cause internal layout to inflate views
dialog.findViewById(...)
Just inflate the layout yourself (its Java code but I think you know what to do):
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE );
View view = inflater.inflate( R.layout.layout, null );
dialog.setView(view);
dialog.create().show();
Your inflated view is now view and you can use it to find other views in it like:
EditText editText = view.findViewById(R.id.myEdittext);
Instead of using alert dialog use simple Dialog its Easy and very simple
final Dialog dialog = new Dialog(context);
dialog.setContentView((R.layout.layout);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
TextView tvTitle = (TextView) dialog.findViewById(R.id.tvTitle);
tvTitle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
You don't have to need to inflate the View.
Try this;
View dialogView; //define this as a gobal field
dialogView = LayoutInflater.from(context).inflate(R.layout.your_view, null);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Title");
builder.setView(dialogView);
View yourView = dialogView.findViewById(R.id.a_view_in_the_layout);
TextView yourTextView = dialogView.findViewById(R.id.a_textView_in_the_layout);
Button yourButton = dialogView.findViewById(R.id.a_button_in_the_layout);
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.
THere a tons of question about android.util.AndroidRuntimeException: requestFeature() must be called before adding content. But none of the proposed solutions worked for me.
I have a custom DialogFragment
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity()).create();
}
#Override
public final View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.notification_dialog, null);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//setting up dialog
}
I'm showing it like this
newDialogInstance().show(activity.getFragmentManager(), "tag-dialog-fragment");
And each time I get:
android.util.AndroidRuntimeException: requestFeature() must be called before adding content
at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:226)
at com.android.internal.app.AlertController.installContent(AlertController.java:234)
at android.app.AlertDialog.onCreate(AlertDialog.java:337)
at android.app.Dialog.dispatchOnCreate(Dialog.java:355)
at android.app.Dialog.show(Dialog.java:260)
at android.app.DialogFragment.onStart(DialogFragment.java:490)
at android.app.Fragment.performStart(Fragment.java:1719)
Could someone explain me what is going on here?
This is a late answer but maybe that'll help someone, the problem comes from the fact that you are trying to inflate the dialog from both onCreateDialog and onCreateView. To avoid this you can avoid using onCreateView and inflate your layout in onCreateDialog instead.
You would get this:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
View layout = inflater.inflate(R.layout.notification_dialog, null);
/** Add modifications on your layout if needed */
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Add your custom layout to the builder
builder.setView(layout);
return builder.create();
}
Then just remove onCreateView or use it to do other things like using savedInstanceState as explained in this other answer: https://stackoverflow.com/a/15602648/2206688
You can also review the documentation example:
http://developer.android.com/guide/topics/ui/dialogs.html#CustomLayout
Another late answer and I wrote part of it already as a comment but maybe it's useful for someone else as well.
As Yoann already wrote, the problem is that the View is created twice and, in the case of the dialog, it creates its own window which causes the problem. The official documentation (http://developer.android.com/reference/android/app/DialogFragment.html#AlertDialog) makes it seem that it is possible to overwrite onCreateView and onCreateDialog at the same time and show a custom layout that can be embedded or used AlertDialog-styled:
Instead of (or in addition to) implementing
onCreateView(LayoutInflater, ViewGroup, Bundle) to generate the view
hierarchy inside of a dialog, you may implement onCreateDialog(Bundle)
to create your own custom Dialog object.
This is most useful for creating an AlertDialog, [...]
It is possible to overwrite both methods, just not in combination with the AlertDialog.Builder.
What is working for me so far is this:
public class CustomDialogFragment extends DialogFragment {
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
if (isInLayout())
return super.onCreateDialog(savedInstanceState);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
.setView(R.layout.my_custom_layout)
.setTitle(R.string.my_title)
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO implement
}
});
return builder.create();
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (!isInLayout())
return super.onCreateView(inflater, container, savedInstanceState);
View v = inflater.inflate(R.layout.my_custom_layout, container);
return v;
}
}
I'm using isInLayout() to decide whether to call the super method or use the custom implementation.
EDIT: This example uses the support library classes: http://developer.android.com/reference/android/support/v7/app/AlertDialog.Builder.html#setView(int)
Use like this :
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View view = getActivity().getLayoutInflater().inflate(R.layout.notification_dialog, null);
builder.setView(view);
return builder;
}
When I create my custom dialog it looks like this :
but I want it to look like this :
Create a class which extends Dialog and inflate it your layout
public class CustomDialog extends Dialog
{
public CustomDialog (Context context)
{
//use this Theme, or any other theme you like
super(context, android.R.style.Theme_Translucent_NoTitleBar);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.your_layout);
}
}
You can use custom dialog and inflate your designed xml into it.
final Dialog yourDialog=new Dialog(context);
thumbnail_click.setContentView(R.layout.yourlayout);
You can use your own cutom layout for dialog as in the follwing link
Custom dialog
Custom Dialog PoPup
You can always inflate your own custom dialog box layout.
LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.dialog_layout, (ViewGroup) findViewById(R.id.dialog_layout_root));
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialoglayout);
if you haven't got an answer yet, here is a good toturial: Toturial
You vill need to make your own XML shape-style file, and use the corner function ex:
<corners android:bottomLeftRadius="5dp" android:bottomRightRadius="5dp" />
Is there a way to use my own Dialog-extended class with an AlertBuilder style? Right now I have:
#Override
protected Dialog onCreateDialog(int id) {
LayoutInflater inflater = LayoutInflater.from(this);
switch (id) {
case MY_DIALOG:
View view = inflater.inflate(R.layout.my_dlg_layout, null);
AlertDialog dlg = new AlertDialog.Builder(this)
.setView(view)
.create();
return dlg;
}
return null;
}
But it's getting too crazy, and I'd like to move the dialog definition into its own class. (the above doesn't look too bad, I'm skipping a lot for the sake of clarity - basically I'm using a somewhat complicated custom view with AlertBuilder).
I very much want to use Dialog Fragment, but am not ready to do it yet, I need to build this as a temporary thing.
Thanks
Is there a way to use my own Dialog-extended class with an
AlertBuilder style
class MyClass extends AlertDialog.Builder{
. . .
}