user5637048
user5637048

Reputation:

String variables becomes null outside setOnClickListener() function

I have a simple android app in which I want to get the user input (String). So I have an "EditText" box and an "Enter" button. When the user types something and hits Enter, I am trying to display that text and it works as long as I do all that inside the 'setOnClickListener()' function. The string variables becomes null outside the function. Here is my code:

public class MainActivity extends AppCompatActivity {

 Button hitButton;
 EditText userInput;
 String myVariable;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    hitButton = (Button)findViewById(R.id.button);
    hitButton.setOnClickListener(new View.OnClickListener() {
      @Override
       public void onClick(View view) {
          userInput = (EditText)findViewById(R.id.userInput);
          myVariable= userInput.getText().toString();

       }
    });

     TextView textView = (TextView)findViewById(R.id.textView);
     textView.setText(myVariable); // THIS IS THE PROBLEM
     // IT DISPLAYS NULL WHICH MEANS THAT String myVariable = null
     }
  }

If I put TextView.setText(myVariable) inside the setOnClickListener function then it works perfectly but outside the function, myVariable becomes null. And I need to user that variable outside the function.

Thanks in advance :)

Upvotes: 1

Views: 1437

Answers (5)

Gaurav Vyas
Gaurav Vyas

Reputation: 344

If you want it outside the onClick method, Use SharedPreferences for this like

public class MainActivity extends AppCompatActivity {

 Button hitButton;
 EditText userInput;
 String myVariable;
 public static final String MyPREFERENCES = "MyPrefs" ;
 public static final String KEY = "Key";
 SharedPreferences sharedpreferences;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    hitButton = (Button)findViewById(R.id.button);

    sharedpreferences = getActivity().getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);        

    hitButton.setOnClickListener(new View.OnClickListener() {
      @Override
       public void onClick(View view) {
          userInput = (EditText)findViewById(R.id.userInput);
          myVariable= userInput.getText().toString();

          SharedPreferences.Editor editor = sharedpreferences.edit();
          editor.putString(KEY, myVariable);
          editor.commit();
       }
    });

     TextView textView = (TextView)findViewById(R.id.textView);
     textView.setText(sharedpreferences.getString(KEY, null)); // THIS IS THE PROBLEM
     // IT DISPLAYS NULL WHICH MEANS THAT String myVariable = null
     }
  }

Hope it will help you

Upvotes: 1

ismail alaoui
ismail alaoui

Reputation: 6073

IT DISPLAYS NULL WHICH MEANS THAT String myVariable = null , its because you try to settext with a null variable only updated when onClick triggered. You have to do this to make it work : Declare and instantiate your EditText userIput outside your onclick method just after you instantiate your button and give your myVariable a default value "" in declaration .

Upvotes: 0

Dimitris P.
Dimitris P.

Reputation: 383

The method onCreate is called once when your Activity launches. So this is where you want to initialise your TextView like you do:

TextView textView = (TextView)findViewById(R.id.textView); 

But in order to set the value of the variable myVariable when the user clicks the button and set the text of your TextView, you have to call the setText method of your TextView inside the onClick method.

public void onClick(View view) {
          userInput = (EditText)findViewById(R.id.userInput);
          myVariable= userInput.getText().toString();
          textView.setText(myVariable);    
       }

On user's click the variable myVariable is set and it is available outside the function as well, because you have declare it on top.

Hope that helps!

Upvotes: 1

Vincz777
Vincz777

Reputation: 688

To complete Stallion's answer,

textView.setText(myVariable);

as you put it, is currently executed at the creation of your activity, as it is located in the onCreate method (which is why myVariable is still null) but never again after.

Upvotes: 0

Sreehari
Sreehari

Reputation: 5655

onClick is the method which will trigger only when you click button.

So if your textView needs to get value updated when clicking button, It should be inside onClick than outside like this.

public void onClick(View view) {
          userInput = (EditText)findViewById(R.id.userInput);
          myVariable= userInput.getText().toString();
          textView.setText(myVariable);    
       }

Upvotes: 0

Related Questions