Android Input Dialog Return Input Value

16,305

Solution 1

Indeed, you can't get to just anything inside your onClick, which is an anonymous method I presume. What you can do is get the context and find your views from there. It would look something like this:

yourButton.setOnClickListener(new View.OnClickListener() {
        public boolean onClick(View v) {
               name = findViewById(R.id.yourInputId)).getText().toString();
               return true;
        }
);

You could also use the v argument, for instance by calling v.getContext() in your listener.

Solution 2

You could make routerName a member variable.

Share:
16,305
Infiniti Fizz
Author by

Infiniti Fizz

Updated on June 16, 2022

Comments

  • Infiniti Fizz
    Infiniti Fizz almost 2 years

    I'm pulling my hair out over this one.

    I have an app that when you press a menu item, I want it to show an input alert dialog. When the user taps "OK" then the text they've entered into the EditText in the dialog wants to be returned to be used later in the activity.

    So I thought putting:

    name = input.getText().toString(); //input is an EditView which is the setView() of the dialog
    

    Inside the onClick of the "Ok" button would work but it doesn't. Eclipse tells me I cant set name inside the onClick because it isn't final, but if I change name's definition to final it can't be changed obviously and so can't be set inside the onClick().

    Here is the full code for this bit:

    String routeName = "";
    
            AlertDialog.Builder alert = new AlertDialog.Builder(this);  
    
            alert.setTitle("Title");  
            alert.setMessage("Message");  
    
            // Set an EditText view to get user input   
            final EditText inputName = new EditText(this);  
            alert.setView(inputName);  
    
            alert.setPositiveButton("Set Route Name", new DialogInterface.OnClickListener() {  
            public void onClick(DialogInterface dialog, int whichButton) {  
                routeName = inputName.getText().toString();  
              }  
            }); 
    
            alert.show();
    

    I must be doing something really stupid here because I've googled around for ages and found no-one else with the same problem.

    Can anyone please enlighten me?

    Thank you for your time,

    InfinitiFizz

  • Infiniti Fizz
    Infiniti Fizz over 13 years
    Thanks for the reply Nanne, but where would I define "yourButton"? Is that the "Ok" button? Because it seems AlertDialog has a specific setPositiveButton() method for creating that button. Do you mean I could create a custom view for a custom version of the dialog with a button defined in that view and have yourButton findViewById that button? Sorry I'm still pretty new to proper android development.
  • Infiniti Fizz
    Infiniti Fizz over 13 years
    Woah! That was simple! Thanks foobaa, but would it be nicer/cleaner to get @Nanne 's way working using findViewById etc?
  • Nanne
    Nanne over 13 years
    I didn't see the addition to the question, but in this case it was just an example, to have something to show. You could just ignore that first part and read from the "setOnclickListener" part ;)
  • Infiniti Fizz
    Infiniti Fizz over 13 years
    Okay the problem with this method was that the rest of the code ran while the user was typing the input as dialogs are asynchronous. My solution was to do what I've replied to Nanne.
  • Infiniti Fizz
    Infiniti Fizz over 13 years
    I started doing this but still didn't know how to get the name variable out of the setOnClickListener and so to solve this problem I stuck everything that was meant to come as a result of the dialog in a function and that function gets called inside the onPositive() listener.