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

Want to add horizontal spacing between cards of CardView in android?
Indoor map in android
Error “No static method isCtrlPressed” in Android
Android.mk File: Relative path of all directories and subdirectories
Android change the name of app which is to installed on homescreen
how to set value from textwatcher to specific multiple textviews in android
ExifInterface: Invalid image.java.io.IOException
Reset all checkbox in Android
How to implement Firebase Google Signin in Android?
Android unzip zip file from internal storage
java.lang.NoSuchMethodError: android.net.http.SslError.b
How to enable stacktrace react-native run-android command?
how to set parameters to volley request (GET request )?
How to perform Push Notification in adobe Phonegap using Firebase Cloud Messaging
Mark existing string.xml values for re-translation in Android Studio?
Android app cannot be updated in Google play store

Categories

HOME
fuelux
linkedin
merge
r-googlesheets
entity-framework-core
visual-studio-emulator
pearson-correlation
drop-down-menu
mel
resultset
symmetricds
cumulocity
screen-readers
internationalization
null-pointer
topology
ag
articulate-storyline
ember-cli
string-concatenation
flask-sqlalchemy
froala
jackson-modules
angularjs-components
ms-access-web-app
paperjs
sqlpackage
constexpr
mobx
rselenium
spring-security4
card.io
android-maps-extensions
carriage-return
python-import
modalviewcontroller
lubridate
docfx
sql-server-ce
google-earth-engine
odroid
skylink
branching-and-merging
file-sharing
signal-strength
double-click
magento-2.0.7
gitlist
rserve
janus
rsa-archer-grc
materialized-path-pattern
cronexpression
usart
cudd
segment-io
lrs
wso2developerstudio
dimension
html-to-pdf
user-management
asyncdisplaykit
phonertc
doctype
dulwich
rdflib
broadband
qtwebengine
apache-spark-1.3
theos
beatsmusic
iso-prolog
cocos3d
formview
strawberry-perl
unix-socket
terminate
windows-phone-8-sdk
ebay-lms
scmmanager
actiondispatch
superscrollorama
mantle
thttpd
virtualpathprovider
workflow-services
uideviceorientation
mathematical-notation
fdf
business-model
pnrp

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