android


getting text from edit text in input dialog


I have an alert dialog like this:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
EditText input = new EditText(this);
input.setText(sp.getString("NAME_0",""),TextView.BufferType.EDITABLE);
alertDialogBuilder.setView(input);
String name = input.getText().toString();
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setTitle("Enter your name"); //Set the title of the box
//alertDialogBuilder.setMessage(""); //Set the message for the box
alertDialogBuilder.setPositiveButton("Start", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
dialog.cancel(); //when they click dismiss we will dismiss the box
}
});
AlertDialog alertDialog =alertDialogBuilder.create(); //create the box
alertDialog.show(); //actually show the box
And the problem is that 'name' always seems to be empty. Is there a different way to get the text from an edit text that is inside an alert dialog?
The method works fine you are just assigning the variable too soon, you need to do it for example when the user clicks the button inside the onClick()
Try the following:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
EditText input = new EditText(this);
input.setText(sp.getString("NAME_0",""),TextView.BufferType.EDITABLE);
alertDialogBuilder.setView(input);
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setTitle("Enter your name"); //Set the title of the box
//alertDialogBuilder.setMessage(""); //Set the message for the box
alertDialogBuilder.setPositiveButton("Start", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
String name = input.getText().toString();
dialog.cancel(); //when they click dismiss we will dismiss the box
}
});
AlertDialog alertDialog =alertDialogBuilder.create(); //create the box
alertDialog.show(); //actually show the box
You were trying to the value of the EdtiText at the time of creation of the dialog, which is empty.

Related Links

Re-position MyLocation button inside Google Map fragment
iOS and Android Bluetooth Receiver/Relay?
RxAndroidBle stopped when discovering another BLE device
Proguard duplicate zip entry error in eventbus library
Connect Android emulateur to internet
getActivity in Activity
How to set this html String to textview with textcolor in android? [closed]
Android Studio: java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
How to query on current date and bring back objects in Realm that contains it?
ionic 3 cli 'ionic cordova run android' command throws an error
Meteor `Please add the Android platform` but next `Platform android already added`
Queries regarding firebase realtime database
Android sometimes getSerializableExtra returns null
Facebook like slide menu below toolbar in android?
can't receive data from one fragment to another fragment
How to fall pin first time from top animation on current location on google map like ola android

Categories

HOME
sonarqube
eclipse
design-patterns
redux-form
jmx
camera
composite-primary-key
bitbucket-api
cublas
zip
google-admin-sdk
accessibility
okhttp
arangodb
mql5
octave
selectize.js
imessage
cfml
apache2.4
springfox
ios10.2
postscript
qliksense
roundup
windows-server-2003
pic32
has-and-belongs-to-many
spotipy
scatter3d
login-script
jwplayer6
sharepoint-workflow
news
skylink
portforwarding
movilizer
congestion-control
berkeley-db-je
vaadin-charts
opennms
datamaps
magic-draw
ampps
cubism.js
meteor-packages
builtins
pacemaker
lrs
flex4.6
watch-os-2
jboss-tools
udpclient
lua-telegram-bot
thread-sleep
jaunt-api
r-package
wiredep
forwarding
ansi
git-ftp
actionfilterattribute
cryptographichashfunction
deform
formview
machine-instruction
genymotion-call
mobility
ignite-ui
codeplex
tinyxml
dsym
conditionaltagsupport
android-audiorecord
video-codecs
chart-director
web-notifications
fragmenttransaction
vfs
zope.interface
garbage
objectquery
entity-attribute-value
jqote
rmdir
xui
mtu
undefined-index
instantiationexception
globals
pagemethods
duplex
interop-domino
linfu-dynamicproxy
firefox-3
commercial-application
netbeans6.1

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App