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

Android Fragment/ViewPager - ViewGroups losing background color
Force close when back button pressed on fragment
This listview lags even after using viewholder
Is Firebase Cloud Messaging free?
Can Android Speech Recognition work in background?
FirebaseAuth.getInstance().getCurrentUser() returns previously logged out user
RecyclerView adds items multiple times
Uploading an Android app for Beta - Package Error
Is android (AVD) command still supported?
Strength issue while pick Image from gallery in Android
Regarding Fused location Provider in Android to Fetch Location
How to download a pdf file from Inside the apk/res directory
How to use Retrofit2 + GSON -> Class with custom object?
android can't switch nested fragment/ childFragment
Android - Geofence trigger when lost gps
How can I make my toolbar a gradient in colors.xml?

Categories

HOME
url-redirection
dns
apache-jena
fpga
mysql-workbench
fftw
zip
spin
pattern-matching
openstreetmap
speech-synthesis
enterprise-architect
http-post
modbus-tcp
job-scheduling
red5
easyphp
nim
tosca
exchangewebservices
quantlib
tizen-tv
constexpr
complex-numbers
least-squares
lint
jpad
android-augmented-reality
signals-slots
linqpad
avcapturedevice
opencmis
dynamic-linq
yii2-api
globalize
semantic-logging
spring-data-couchbase
git-flow
arules
juice-ui
visual-studio-2008-sp1
chromium-os
angular2-testing
autosar
adafruit
windows-azure-queues
firebase-security
qiime
php-socket
ellucian-scribe
database-relations
beanstalk
jnlua
jbase
directdraw
vhosts
sapi
vigenere
visual-sourcesafe-2005
finalbuilder
haskell-warp
ocra
apache-commons-daemon
google-refine
bufferedinputstream
zend-currency
cakephp-2.7
structuremap3
formview
rmysql
ignite-ui
verold
gnumeric
authlogic
dsym
koala
gwt-openlayers
spring-3
breakout
gpars
web-notifications
curb
urlrewriter
fsevents
filemerge
strsep
quotation-marks
appledoc
luajava
object-database
addchild
product-management

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