1

I am doing a simple login app where i need to pass the user id to subsequent screens. But,i am not getting the username field to a variable.

 public static String user;
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    EditText username = (EditText)findViewById(R.id.etUserName);
    EditText password = (EditText)findViewById(R.id.etPwd);
    Button blogin = (Button)findViewById(R.id.bLogin);


    user = username.getText().toString();// Here user variable is blank!!!!!!!!!!!!!!!

Kindly help!!!

Sourav Das
  • 982
  • 1
  • 15
  • 40

6 Answers6

3

that code should be written inside onClickListener of blogin Button

blogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            user = username.getText().toString();

        }
});
Ravi
  • 34,851
  • 21
  • 122
  • 183
1
user = username.getText().toString();

put this line into a listener like a button click

Veer3383
  • 1,785
  • 6
  • 29
  • 49
  • Thanks. But why does a simple text field needs to be registered under an event. – Sourav Das Aug 25 '16 at 06:26
  • You need to understand the activity lifecycle, Your activity is created the moment your app is started. you input the text at later stage when your activity has already started, So you need a interface to listen for the change in the state of your edittext, hence you need to add it in a button click listener, i hope this helps – Veer3383 Aug 25 '16 at 06:29
1

You must get the user field when blogin is clicked:

blogin.setOnClickListener(new View.OnClickListener()
   {
      public void onClick(View v)
      {
          user = username.getText().toString();
      }
});
Misagh Emamverdi
  • 3,654
  • 5
  • 33
  • 57
1

Add this code on onCreate

public static String user;
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    EditText username = (EditText)findViewById(R.id.etUserName);
    EditText password = (EditText)findViewById(R.id.etPwd);
    Button blogin = (Button)findViewById(R.id.bLogin);


 // On the click of the button get username string.
    blogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              user = username.getText().toString();  
            }
        });
Dentor
  • 590
  • 2
  • 15
1

you get text of username field immediately. and I guess not text has been set in the filed before. when a editText has no text, it's return "". you must create onClickListener event for your blogin button and use getText of username textView after user entered its username and then click on the button

public static String user;
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    EditText username = (EditText)findViewById(R.id.etUserName);
    EditText password = (EditText)findViewById(R.id.etPwd);
    Button blogin = (Button)findViewById(R.id.bLogin);

    blogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          if(username.getText().toString().equals(""){
              Toast.makeText(v.getContext, "enter your username", Toast.LENGTH_SHORT).show();
          }
          else
              user = username.getText().toString();  
        }
    });
}
Saeed Zhiany
  • 2,051
  • 9
  • 30
  • 41
1

You are evaluating value of editText on onCreate(); that will return empty always so just evaluate the editText when you need it. as you want it on button clickListner();

        blogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) 
            {
                user = username.getText().toString();

            }
        });
Amit Bhati
  • 1,405
  • 12
  • 20