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.
Related
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();
Google decided to make a single-threaded user interface that doesn't have modal dialogs. I'm sure most of you have found that nothing updates until your function returns because everything is event driven on a single thread (by "law").
If I have a simple alert-box, such as "Are You Sure?" (example only), with a Yes and No button, then I have to assign callbacks to the buttons rather than having a simple return value (no modal dialogs). That's fine, even though a return value would vastly simplify my problem (arguments stay local to the caller), although this would stop the calling activity from responding (modal).
Imagine now if I have a list of items and the user attempts to perform some operation. The dialog must now have some way to pass WHICH item I want to perform the operation on to the button's callback, but I can't seem to find any mechanism in the API for passing this along to the onclick handler. Using non-local variables is a work-around, but messy.
How can I pass this information along cleanly? Does anyone have some sort of hack that would somehow "fake" a modal dialog that can return a value (I'm not seeing how).
Create a custom dialog that extends the default android Dialog and add the information you need and pass on the constructor.
See more here: How can I pass values between a Dialog and an Activity?
I am not sure what exactly what do you want to achieve. Not sure if your problems is in the communication between the activity to the dialog or dialog to the activity or both.
Anyway, I have some experience on Android and I really recommend you to achieve the communication between activities, fragments, even dialog (DialogFragments) to use one of these libraries. At the beggining could be a little bit hard to understand how work, but the result is faster and cleaner code, of course offers you more flexibility.
Take a look to:
https://github.com/beworker/tinybus --> less used but it is awesome
https://github.com/greenrobot/EventBus --> more extended and used for the community
Hope to help you!
In a situation like this, I Created a new string array entry in the strings.xml in values folder like this:
<string-array name="array">
<item>1</item>
<item>2</item>
</string-array>
And then create a dialog using Dialog builder like this:
AlertDialog.Builder dialog=new AlertDialog.Builder(this);
LayoutInflater infl=this.getLayoutInflater();
Resources res=getResources();
dialog.setSingleChoiceItems(R.array.alphabets, 0,new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
mReturnVariable=which;
}
});
dialog.create().show();
So the mReturnVariable contains the user selected item index .Hope that solves the problem
I passed the required arguments to the Alert Dialog using View Binding in Android Latest version.
private ConnectDialogBinding connectDialogBinding;
private String chargerID;
private void connectDialog() {
// Create the object of
// AlertDialog Builder class
AlertDialog.Builder builder = new AlertDialog.Builder(ConnectActivity.this);
connectDialogBinding = ConnectDialogBinding.inflate(getLayoutInflater());
builder.setView(connectDialogBinding.getRoot());
connectDialogBinding.txtID.setText(chargerID);
builder.setCancelable(false);
// Create the Alert dialog
AlertDialog alertDialog = builder.create();
// Show the Alert Dialog box
alertDialog.show();
connectDialogBinding.cancelBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDialog.cancel();
}
});
}
enter image description here
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).
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()).
I've got a problem. Since I am a newbie to develop Android apps, I suppose that I did not get all the concepts right.
The problem is:In my dialog, after entering all the information and pushing the "Ok" button, I would like to get what I just entered into the TextEdit field. But when I try to
get that EditText on okButton.onClickListener through findViewById(R.id.myTextEditId) I got Null instead of instance.
Here is some code:
Dialog's XML (part where I define EditText) :
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:inputType="textMultiLine"
android:maxLines="3"
android:hint="#string/add_new_note_hint">
<requestFocus />
</EditText>
Here is the code, where NPE accours:
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
View dialogView = getLayoutInflater().inflate(R.layout.add_note_dialog,null);
dialogBuilder.setTitle(R.string.add_dialog_title)
.setPositiveButton(R.string.add_dialog_pos_but, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
EditText et = (EditText) findViewById(R.id.editText1); //this is where I get null instead of real instance.
}
})
Why am I getting this and what is the right way (best practice) to process widget's data in some callback method ? (how to fix)
Thank you very much, for your time on my issue!
dialogView.findViewById will solve this
The problem is that findViewById searches the current view hierarchy. If you call this from your Activity, then it searches in your current activity's view hierarchy whereas you actually want to search your dialog's:
EditText et = (EditText) dialogView.findViewById(R.id.editText1);
You'll probably need to make dialogView final as well.