Reputation: 1723
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);
}
});
}
Upvotes: 0
Views: 167
Reputation: 20368
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.
Upvotes: 1
Reputation: 1723
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"
/>
Upvotes: 1
Reputation: 1259
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.
Upvotes: 0
Reputation: 3127
Remove android:onClick="onLoginClick" from xml and in your activity set onClicklistener.
Button login = (Button)findViewById(R.id.buttonLogin);
login.setOnClickListener(new OnClickListener......)
Upvotes: 1
Reputation: 598
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
Upvotes: 0