AlertDialog. When to change the style of the message? - android

I want to have the message of an alertdialog displayed with a monospace font, so I wrote this code, which work nice:
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setMessage("message");
...
AlertDialog dialog = builder.create();
dialog.show();
TextView messageView = dialog.findViewById(android.R.id.message);
messageView.setTypeface(Typeface.MONOSPACE);
My question is about the order of the calls: you need dialog.show() to be called in order to call dialog.findViewById, otherwise you get a null pointer.
It does not sound logical to show something and then to change it. I would have preferred to build it with the right style and then show it.
Is there a way to do it like that ?

Prepare your own TextView or any View Programmatically or in XML as a custom View the show it like:
builder.setView(getLayoutInflater().inflate(R.layout.YourLayout, null))
Now YourLayout should have all the styles you like from now. Otherwise it will be hard to call findViewById without the Dialog view on the Screen!!

Related

Xamarin Android Simple Input Text Popup

I have the ability to make my own popup, with dialogs, but I don't need anything complex. I was wondering if there was a simple function I could call that would make a popup where the user entered text and then return to me that text for use.
Sorta like these popups
But with one where the user would enter text.
Fairly new so if there is something like this I wouldn't mind an example to go with it. Thanks.
If you are using Android native UI, then you can easily create an AlertDialog and add a EditText to the dialog.
EditText et = new EditText(this);
AlertDialog.Builder ad = new AlertDialog.Builder (this);
ad.setTitle ("Type text");
ad.setView(et); // <----
ad.show();

Why isn't my EditText styled to match my AlertDialog?

I am trying to create an AlertDialog with a single text field prompt. Here is the code I am using to create it:
final EditText url = new EditText(this);
new AlertDialog.Builder(this, AlertDialog.THEME_DEVICE_DEFAULT_DARK)
.setTitle(R.string.mirror_title)
.setView(url)
.setPositiveButton(...)
.setNegativeButton(...)
.show();
When I run that against API level 22, the buttons style using Material as expected, but the EditText does not:
What do I need to do to get the new style EditText here?
You are specifying #android:style/Theme.DeviceDefault as your alert dialog theme, but your EditText is using whatever theme was set on the Context represented by this.
To ensure consistency between your alert dialog's decor and contents, you should always create the contents using the alert dialog's themed context.
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this, ...)
.setTitle(R.string.mirror_title)
.setPositiveButton(...)
.setNegativeButton(...);
Context dialogContext = dialogBuilder.getContext();
EditText url = new EditText(dialogContext);
dialogBuilder.setView(url).show();
As a side note, it looks like you may need to specify an activity theme in your AndroidManifest.xml and check that your targetSdkVersion is specified correctly. You shouldn't be seeing Gingerbread-styled widgets unless you are explicitly targeting the Gingerbread themes (ex. Theme or Theme.Light).

How to auto-capitalize an EditText inside an AlertDalog?

I want to show an AlertDialog containing an EditText that auomtically capitalizes words.
Following this question and that one, I managed to get the AlertDialog show the keyboard automatically when the dialog is shown, and also capitalize the first letter when the user clicks on the EditText. But until the user clicks, the keyboard shows in lowercase mode.
How can I make the keyboard open automatically in upper-case (auto-capitalize words) mode?
My relevant code is as follows:
input = new EditText(context);
input.setInputType(InputType.TYPE_TEXT_FLAG_CAP_WORDS);
dialog.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
Also tried to requestFocus() in the dialog's onShowListener but it didn't help.
You just need to use the following code after your AlertDialog declaration.
AlertDialog.Builder alert = new AlertDialog.Builder(this, AlertDialog.THEME_DEVICE_DEFAULT_DARK);
final EditText edittext = new EditText(getApplicationContext());
// Just use it here, there is much more options in the link below
edittext.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
Look for other options here
A workaround that worked for me was to avoid using setMessage(), and adding the message into my own view instead (using setView()).

Difference between AlertDialog.builder's obj.create() vs obj.show() vs obj.create().show()

Is there any difference between .create() and .show() methods of AlertDialog's builder class? Like when we create an alert dialog using:
AlertDialog.Builder builder = new
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("");
builder.setPositiveButton(....)
builder.setNegativeButton(....)
What is recommended pratice to use and why?
builder.create() //I have seen this creates and displays the dialog
OR
builder.show() //this also displays the dialog
OR
builder.create().show() //well same thing
I have read the documentation. But was unable to make any sense from it. Any ideas ?
obj.create()-For create Dialog
obj.show()-For show Dialog <- without it you cant show dialog if you created.
and
obj.create().show()-create and show Dialog i mean both same as above two in one statement.
builder.show() returns an AlertDialog object and displays it immediately. As stated in the documentation, calling this method is functionally identical to:
AlertDialog dialog = builder.create();
dialog.show();
builder.create() returns an AlertDialog object with the arguments supplied to the builder, without showing it. This might be useful if you want to create and store the AlertDialog object for additional processing, after which dialog.show() may be called.

Get AlertDialog view of WebView

I'm creating a scenario test framework for Android Cordova/PhoneGap applications that builds on JUnit. There is one scenario I would like to test: you click on some button in the webpage (shown in an Android WebView) and it opens a popup (an AlertDialog). I want to check the message on this popup and click one of the buttons. Therefore I need the view object of this AlertDialog.
I know you can use findViewById, but you have to give an id as parameter, which I don't have since the dialog is created with the following code:
AlertDialog.Builder dlg = new AlertDialog.Builder(ctx.getContext());
dlg.setMessage(message);
dlg.setTitle(title);
....
dlg.create();
dlg.show();
Any idea how I can access the correct view?
Thanks!
The TextView containing the AlertDialog's message is always identified by android.R.id.message. If you capture the result of dlg.create(), you should be able to get a reference to the message TextView by calling .findViewById(android.R.id.message) on it, from which you could then get the text.

Categories

Resources