I'm creating a custom alert dialog
// Biuld the dialog
AlertDialog.Builder alert = new AlertDialog.Builder(this);
// Create the dialog
AlertDialog alertToShow = alert.create();
// Set keyboard to the dialog
alertToShow.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
// Add custom layout to the dialog
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.dialog_view, null);
alert.setView(dialogView);
// Show the dialog
alertToShow.show();
But the following line from above where I add my custom layout
f1.addView(inflater.inflate(R.layout.dialog_view, f1, false));
results in the error
Attempt to invoke virtual method 'void android.widget.FrameLayout.addView(android.view.View)' on a null object reference
Any idea how to fix the error?
You need to find the view after the dialog is displayed or after the layout is inflated. Not before
// Add custom layout to the dialog
LayoutInflater inflater = getLayoutInflater();
View dialogView = inflater.inflate(R.layout.dialog_view, f1, false)
FrameLayout f1 = (FrameLayout) dialogView.findViewById(android.R.id.custom);
f1.addView(dialogView);
// Show the dialog
alertToShow.show();
Though, I think you want alertToShow.setView(dialogView), maybe
I have to add views before I create the dialog
// Add custom layout to the dialog
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.dialog_view, null);
alert.setView(dialogView);
// Create the dialog
AlertDialog alertToShow = alert.create();
// Set keyboard to the dialog
alertToShow.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
// Show the dialog
alertToShow.show();
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 want to create a alert dialog using a layout which is in a xml. I tried this:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
View v = inflater.inflate(R.id.optionsmenu, null);
AlertDialog dialog = builder.setView(v).create();
dialog.show();
It does not work. optionsmenu is the layout that i want to use to create alert dialog. Can i set alert dialog view to this inner layout ?
here is the image's url . You can see the layout that i want to use.
I dont really know how you expect your dialog to look like but you should create it like this:
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.activity_main);
dialog.setTitle("");
//to use a view inside the xml (i.e. a button)
Button button = (Button)dialog.findViewById(R.id.optionsmenu);
dialog.show();
Note that you were trying to inflate not the layout but the view (R.id.optionsmenu) it should have been (R.layout.activity_main)
alertDialog.builder is used to create a dialog without using a xml layout
Does anyone know how to customize items in AlertDialog when multi choice items are set with setMultiChoiceItems(...). I would like to change the text size for the items.
Thanks
Sure, you can use Dialog.setContentView() to set the content of a dialog to be an arbitrary layout.
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.yourLayoutId);
dialog.show();
Make yourself a layout file with a components that you want in it and call setContentView on your dialog, passing the name of your layout file.
If you are deadset on AlertDialog you can do something similar with builder.setView()
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.yourLayoutId, (ViewGroup) findViewById(R.id.yourLayoutRoot));
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setView(layout);
AlertDialog alertDialog = builder.create();
alertDialog.show();
You can create a custom AlertDialog where you can change the text size of any element. A simple generic example: http://android-codes-examples.blogspot.in/2011/03/how-to-display-custom-dialog-and.html
In my application I have used one alert box.. In this alert box, I give another layout to be show.. using setView() method.. I have a problem. How to use layout elements id in my activity?
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.volume, null);
builder.setTitle("Volume");
builder.setView(layout);
builder.setPositiveButton("OK", null);
builder.setNegativeButton("Cancel", null);
AlertDialog alert1 = builder.create();
alert1.show();
Something like this:
In your activity:
....
View layout = inflater.inflate(R.layout.volume, null);
.....
View some = layout.findViewById(YOUR_VIEW_ID);
.....
if you want to refer to view inside your custom layout, then do the following:
EditText editText = (EditText) layout.findViewById(R.id.your_editText);
Button editText = (Button) layout.findViewById(R.id.your_button);
.
.
.
//and so one with other views
This code shows you how to show a custom alert and get a view from that custom layout:
AlertDialog mDialog = null;
Context mContext = getApplicationContext();
final AlertDialog.Builder my_Dialog = new AlertDialog.Builder(this);
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.my_dialog, (ViewGroup) findViewById(R.id.layout_root));
ImageView imageViewInsideLayout = (ImageView) layout.findViewById(R.id.imageviewLayout);
my_Dialog.setView(layout);
mDialog = my_Dialog.create();
mDialog.show();
You can get reference to view by builder.findViewById( R.id.restart );
I'm trying to find a way to create a popup screen for some user input which includes radio button, editText, button. I don't want to start a new activity.
what would be a good option? AlertDialog? Spinner?Popup menu?
Thanks
AlertDialog would be fine for this. You can declare a layout.xml file with all of the components you'll need and then inflate it and set it as the content of your dialog.
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.yourLayoutId, (ViewGroup) findViewById(R.id.yourLayoutRoot));
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setView(layout);
AlertDialog alertDialog = builder.create();
alertDialog.show();