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

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).

Related

AlertDialog. When to change the style of the message?

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!!

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();

Show a float dialog in any screen?

I want to show a dialog in my android phone anywhere. I want to use windowmanager.addView()
to come true it. But It doesn't work because this function only can add views.How to show a dialog in anywhere?
Refer this links http://developer.android.com/reference/android/Manifest.permission.html#SYSTEM_ALERT_WINDOW
Allows an application to open windows using the type TYPE_SYSTEM_ALERT, shown on top of all other applications. Very few applications should use this permission; these windows are intended for system-level interaction with the user.
Constant Value: "android.permission.SYSTEM_ALERT_WINDOW"
Example projects
https://github.com/fouady/SpotifyTray-Android , https://github.com/henrychuangtw/FB-ChatHead
Use code like this (it's from android documentation):
// 1. Instantiate an AlertDialog.Builder with its constructor
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// 2. Chain together various setter methods to set the dialog characteristics
builder.setMessage(R.string.dialog_message)
.setTitle(R.string.dialog_title);
// 3. Get the AlertDialog from create()
AlertDialog dialog = builder.create();
dialog.show();

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()).

android dialog orientation issue

hi i am new developer on android i have written code for display simple dialog,in this dialog i have taken edit text view.when i entered text on edit text then i have changed orientation of the scree then the value of edit text has not appearing!
i have written code as follows
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Title");
alert.setMessage("Message");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText();
// Do something with value!
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
please any one can resolve this?
Basically, this involves overriding the onRetainNonConfigurationInstance method.
See here:
Faster Screen Orientation Change
Excerpt:
"The Activity class has a special
method called
onRetainNonConfigurationInstance().
This method can be used to pass an
arbitrary object your future self and
Android is smart enough to call this
method only when needed. In the case
of Photostream, the application used
this method to pass the downloaded
images to the future activity on
orientation change."
prasad... The editText box does not have an ID and if the view element does not have an ID the view state is not automagically saved on a soft kill when the user changes the orientation of the phone. You might be better off creating a custom dialog using an XML layout, then the edit text box should have and ID and the view state should be automagically saved on a soft kill.
JAL
I have some code here.
Edit: Prototype code taken from the Android Docs that barely works because I do not have the time to work on this. Create an XML layout in res/layout as alert_dialog_text_entry.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText android:text="Stateful"
android:id="#+id/EditText01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</EditText>
</LinearLayout>
And then use this layout to create the alert:
AlertDialog.Builder builder= new AlertDialog.Builder(this);
LayoutInflater inflater= getLayoutInflater();
final View myView= inflater.inflate(R.layout.alert_dialog_text_entry, null);
builder.setTitle("About");
builder.setMessage(alertMessage+"Version: "+versionName);
builder.setView(myView);
AlertDialog alert= builder.create();
Since the editText box has an ID it appears to save state on a soft kill.
This question is old but it is still worth giving a simpler answer. JAL mentions the need to set the ID, but you can go ahead and do that directly in the Java. For example, add this new line into the original code above:
// Set an EditText view to get user input
final EditText input = new EditText(this);
// Id the EditText so the framework will save/restore it for us
input.setId(R.id.my_id_for_alert_box_inputs); // <-------- New line
alert.setView(input);
Under /res/values create a new XML file called ids.xml. In there, define this id we use in the Java:
<?xml version="1.0" encoding="utf-8"?>
<!-- Integer IDs used for tagging Views made in Java programmatically
without clashing with XML-defined views. -->
<resources>
<!-- Used to id the input box in a dialog. Reused in different dialogs. -->
<item type="id" name="my_id_for_alert_box_inputs" />
</resources>
This works because the Android application framework is supposed to save/restore views that have an ID defined. The whole XML part of the above is neat but not really needed. You could just plug a made-up integer into the Java View.setId() call to make this a one-line fix.

Categories

Resources