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{
. . .
}
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 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/
i want have my custom header for my CustomAlert.
what do i do for my custom layout for Dialog,s title?
Edit2:
i added my code:
protected Dialog onCreateDialog(int id)
{
switch(id)
{
case Dialog_Reset :
Dialog dialog=new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.about);
dialog.setCanceledOnTouchOutside(true);
return dialog;
}
return super.onCreateDialog(id);
}
and becuse i dont like default header in dialog,i remove it now,while i learn having my custom header.
but i dont understand what is "titleId". is it what really? is it my
special layout designed that i want use for title?
titleId : title identifier in other words a string resource identifier, e.g. R.string.app_name. You can add these in strings.xml found at res > values folders
Android Dev Doc: Dialog.setTitle (int titleId)
More about string resources here
You can set CustomTitle of your Alert Dialog using public AlertDialog.Builder setCustomTitle (View customTitleView) method.
As per documentation
public AlertDialog.Builder setCustomTitle (View customTitleView)
Set the title using the custom view customTitleView. The methods setTitle(int) and setIcon(int) should be sufficient for most titles, but this is provided if the title needs more customization. Using this will replace the title and icon set via the other methods.
Parameters
customTitleView The custom view to use as the title.
Returns
This Builder object to allow for chaining of calls to set methods
example
LayoutInflater inflater = (LayoutInflater)yourClass.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View yourView= inflater.inflate(R.layout.custom_dialog, null);
AlertDialog.Builder ab= new AlertDialog.Builder(this);
ab.setCustomTitle(yourView);
ab.setMessage(message);
...
ab.create();
ab.show();
for details see the documentation
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();
}
}
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);
}
}