0

I am building a Login page for my app. I have two EditText fields and a Button. When the app is launched, clicking on the button the first time provides no response,logcate is also not showing anyting, but when clicked the second time, it works. I don't know what is happening. Please help me.

activity_login.xml

<Button android:id="@+id/buttonLogin" style="@style/Button1" android:onClick="onLoginClick" android:text="continue" /> 

LoginActivity.java

 buttonLogin= (Button) findViewById(R.id.buttonLogin); buttonLogin.setFocusable(true); buttonLogin.setFocusableInTouchMode(true); buttonLogin.requestFocus(); buttonLogin.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { hideSoftKeyboard(LoginActivity.this, v); Log.d("helo", "heoolll"); String username = mUserEditText.getText().toString(); String password = mPassEditText.getText().toString(); String location = mLocationData.get(mLocationSpinner.getSelectedItemPosition()).toLowerCase(); if(username.isEmpty()||password.isEmpty()){ //CreatorMessenger.getInstance().showMessage(this,"Error!!","You need to enter username and password both to continue!!"); popbox(); return; } /*buttonLogin.requestFocus(); buttonLogin.setFocusableInTouchMode(true);*/ User user; user = new User(username); user.setLocation(location); AppManager.getInstance().setLoggedInUser(user); APICaller.getInstance().login(username, password, location); } }); } 
7
  • Put a Log.i statement in onLoginClick method and check if it is called. Also, check if there is any error. If so, post logcat. And I guess you have initialized your button and Edittexts in onCreate properly. Commented Dec 10, 2013 at 8:03
  • Is there any exception that occurs ? Commented Dec 10, 2013 at 8:04
  • I put the Log.i statement, and i got that when first time I am clicking it is not going into onCLickListener(), and logcate is also not showing anything. But second time it is going into onCLickListener(). Please help me @Anamika Commented Dec 10, 2013 at 10:00
  • No there is no exception occuring @Hitman. Commented Dec 10, 2013 at 10:01
  • Did you see this answer on your question? I think that is right. Commented Dec 10, 2013 at 10:03

5 Answers 5

1

Remove android:onClick="onLoginClick" from xml and in your activity set onClicklistener.

Button login = (Button)findViewById(R.id.buttonLogin); login.setOnClickListener(new OnClickListener......) 
Sign up to request clarification or add additional context in comments.

3 Comments

But what is issue if it is defined in xml? I have used like this many times. And it has worked pretty well for me.
I have read somewhere someone had problems with it. So give it try.
Yes you was right @vandzi, it was the problem because:onClick="onLoginClick". So I implement setOnClickListener(new OnClickListener......) and setOnFocusChangeListener();. Thanks a lot..:-)
1

Yes Yes I got the answer, after a lot of RND, I got the solution, I just need to implement setOnFocusChangeListener(). So I am putting here the solution.

ActivityLogin.java

 buttonLogin= (Button) findViewById(R.id.buttonLogin); buttonLogin.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Log.d("hello", "hellow"); String username = mUserEditText.getText().toString(); String password = mPassEditText.getText().toString(); String location = mLocationData.get(mLocationSpinner.getSelectedItemPosition()).toLowerCase(); if(username.isEmpty()||password.isEmpty()){ popbox(); return; } User user; user = new User(username); user.setLocation(location); AppManager.getInstance().setLoggedInUser(user); APICaller.getInstance().login(username, password, location); } }); buttonLogin.setOnFocusChangeListener(new View.OnFocusChangeListener() { public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { v.performClick(); } } }); } 

activity_login.xml

 <Button android:id="@+id/buttonLogin" style="@style/Button1" android:text="continue" /> 

Comments

1

Set this -

android:focusableInTouchMode="true" 

to this -

android:focusableInTouchMode="false" 

on button.

Explanation - If you'll make the button focusable then on first click the focus is passed to the button, then the click is passed on second touch. EditText is a focusable View which gains focus first and therefore other views must first regain the focus from EditText, unless they do not need focus to work, just like buttons. If you just need the OnClick function, then you don't need focus, so you can spre one extra click.

PS: Although it shouldn't require, but setting android:focusable to false will help too, if the first one doesn't work.

Comments

0

Is there any exception that occurs ? Also try to login with user A and password "a" and push the button, after that try to use to login with user B and password "b" and see if that works. Maybe the setLoggedInUser or the login method is taking to long, and that is why it works the second time with the same username and password

Comments

0

The 1st one the button gets the focus and the 2nd one triggers the login method. So the solution is to give the focus to button and the problem is solved.

1 Comment

I give the focus to button but nothing happen. buttonLogin= (Button) findViewById(R.id.buttonLogin); buttonLogin.setFocusable(true); buttonLogin.requestFocus(); buttonLogin.setOnClickListener(new OnClickListener() {}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.