Changing contents of an EditText widget in AlertDialog without onPrepareDialog() - android

I'm using an AlertDialog with a custom view that contains an EditText widget. I want to change the contents of the EditText widget before the AlertDialog is shown. I know that this is usually done by overriding onPrepareDialog() and setting the text there. However, as far as I can see, this will not work in my specific case because I use AlertDialog.show() instead of Activity.showDialog().
So how should I do this with a dialog that is shown using AlertDialog.show()? One solution is to set the text after the dialog has been brought to the front, i.e.:
AlertDialog alertDialog = builder.create();
alertDialog.show();
EditText editText = (EditText) alertDialog.findViewById(R.id.text);
editText.setText("Foo bar");
However, I don't think that this is a nice solution because the dialog is first shown and then the text is set. I'd like to set the text before the dialog is actually shown.
Is there any way to achieve this? I cannot do it before alertDialog.show() because findViewById() returns null when called before alertDialog.show().
Thanks for any help!

AlertDialog alertDialog = builder.create();
alertDialog.show();
Since you have access to the AlertDialog.Builder object, simply change the layout before calling builder.create().
Addition
I have an EditText widget in my XML file which I inflate using builder.setView(inflater.inflate(R.layout.mydialog, null)). How do I change the text of this EditText widget without calling findViewById()?
Break that line into a series of commands. Specifically: inflate the XML, alter the layout, and pass it to setView().
View view = inflater.inflate(R.layout.mydialog, null);
EditText editText = (EditText) view.findViewById(R.id.text);
editText.setText("Foo bar");
builder.setView(view);

Related

Android AlertDialog.Builder Custom title bar with Edit text

Here is the issue, I want to create a custom alert dialog box with 4 parts,
Title Bar
Header part (Most of the time I plan to use EditText box for
searching purpose)
List item(Single click or Multi Click)
Footer part(Cancel or OK buttons)
I try do this using separate layout, but list view item not showing properly(shrinking). So then I plan to use AlertDialog and inside I have implemented ListView and footer button part in single LinerLayout and set it using setView method, it solved my list view and Footer issue.
Problem is I want to add Header part top of the list view.
Image Link : https://dl.dropboxusercontent.com/u/4048078/device-2013-07-31-141546.png
You can try this
Dialog dialog_help = new Dialog(getActivity());
dialog_help.setContentView(R.layout.title_multi_selectitems_dialog);
EditText et_1 = (EditText) dialog_help.findViewById(R.id.et_help_1);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
dialog_help.setCancelable(true);
dialog_help.setTitle(" Help ");
dialog_help.setCanceledOnTouchOutside(true);
dialog_help.show();
dialog_help.getWindow().setGravity(Gravity.CENTER);
dialog_help.getWindow().setLayout(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
It's not a good practice to set editBox in the title of the alert dialog.
To set title use setTitle and setIcon builder methods.
EditText it the control that's better to place in dialog,s body.
Use AlertDialog.Builder.setView(android.view.View) for this.

Custom Layout for Dialog across different versions?

I have a Dialog with a custom layout (very simple). I set it up using the code below:
public static Dialog createGPSDialog(final Activity activity, boolean isLocationEnabled) {
final Dialog dialog = new Dialog(activity, R.style.Theme_Sherlock_Light_Dialog);
LinearLayout contentView = (LinearLayout) activity.getLayoutInflater().inflate(R.layout.dialog_twobutton, null);
dialog.setContentView(contentView);
return dialog;
}
There's some code I omitted, but it isn't relevant. Anyway, this is how it looks in gingerbread:
and this is how it looks on jellybean (probably ics as well):
The title area for the dialog is kept on JB, even after setting ContentView. Is there a workaround for this?
If you want to be completely custom and get rid of the title bar, try adding this line to your code:
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
The key is defining a theme for your dialog. The constructor for Dialog takes a theme resources id. Just use one of android's android.R.theme.xxx.
When using the AlertDialog.Builder you can define a new theme using the contextthemewrapper.
http://developer.android.com/reference/android/view/ContextThemeWrapper.html

Creating Custom AlertDialog ? What is the root view?

what i am trying to do:
Create a custom Alert Dialog. Buttons just like any Alert Dialog but above are two TextEdit input boxes. I don't want to create a custom Dialog but a customized Alert Dialog
Here is what I am trying #3:
http://developer.android.com/guide/topics/ui/dialogs.html
It says:
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog,
(ViewGroup) findViewById(R.id.layout_root));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
Documentation says:
View layout = inflater.inflate(R.layout.custom_dialog,
(ViewGroup) findViewById(R.id.layout_root));
where the first parameter is the layout resource ID and the second is the ID of the root View.
Problem is I don't know what the layout root is? this is a dialog I am going to kick of in an Activity. Should I use the layout id if the activity? Is layout_root pulled out of a hat?
Also tried:
View layout = inflater.inflate(R.layout.my_custom_layout,
(ViewGroup) findViewById(android.R.id.content).getRootView());
result null pointer.
Even though an older question, this article might be useful for others who search for this answer:
Layout Inflation as Intended:
If you’ve ever written something like the following code using
LayoutInflater in your Android application:
inflater.inflate(R.layout.my_layout, null);
PLEASE read on, because you’re doing it wrong and I want to explain to
you why.
...BUT...
Every Rule Has An Exception
There are of course instances where you can truly justify a null
parent during inflation, but they are few. One such instance occurs
when you are inflating a custom layout to be attached to an
AlertDialog. Consider the following example where we want to use our
same XML layout but set it as the dialog view:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
View content = LayoutInflater.from(context).inflate(R.layout.item_row, null);
builder.setTitle("My Dialog");
builder.setView(content);
builder.setPositiveButton("OK", null);
builder.show();
The issue here is that AlertDialog.Builder supports a custom view, but
does not provide an implementation of setView() that takes a layout
resource; so you must inflate the XML manually. However, because the
result will go into the dialog, which does not expose its root view
(in fact, it doesn’t exist yet), we do not have access to the eventual
parent of the layout, so we cannot use it for inflation. It turns out,
this is irrelevant, because AlertDialog will erase any LayoutParams on
the layout anyway and replace them with match_parent.
The article has an explanation on why you should supply a parent ViewGroup in most other cases than Dialog building.
Ok. The root view in the documentation refers to the element in the custom layout. So the custom layout will have an outermost view called the root view. You need to give this an Id and than you can pass it in as shown. So first argument is the custom view id, the second argument is id of the root layout element within the custom view.
View layout = inflater.inflate(R.layout.custom_dialog,
(ViewGroup) findViewById(R.id.layout_root));
So in this example given in the documentation above, R.id.layout_root refers to the id you give to say for example the outermost LinearLayout within the custom_dialog layout.
Have you tried this?
View layout = inflater.inflate(R.layout.custom_dialog,null);
builder.setView(layout);
layout.getRootView();
Should give LinearLayout.

How to display title of dialog in multi line?

Suppose I have a dialog which is opened from java file . I want to set multiline title of these dialog box.
dialog = new Dialog(context);
dialog.setContentView(R.layout.issue_attachment_preview);
String title=getString(R.string.previewImageDialogTitle)+"\n ["+attachment.filename+"]";
dialog.setTitle(title);
dialog.setCancelable(true);
But it does not display title in multiline , please suggest me any usable link or example.
TextView tv = (TextView) dialog.findViewById(android.R.id.title);
tv.setSingleLine(false);
Add
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
immediately after calling super.onCreate and just before setContentView.
Then add the multiline textview on top of your dialog layout which will work as title

how to make edittext and alertdialog has same width?

a alertdialog include a edittext
how to make edittext's width same with the alertdialog's (NO ABSOLUTE LAYOUT) ?
You can create a custom alertDialog and use the following code:
EditText myEditText = (EditText) activity.getViewById(R.id.myEditText);
myEditText.setLayoutParams(LayoutParams.MATCH_PARENT);
Give that a try.

Categories

Resources