Reputation: 43893
In my android app, on the login page, there are two edittext fields username,password. The problem is when I click on password, the keyboard pops up and then most of the password field gets covered by the keyboard.
I want to basically have an animation that moves the two fields up (smoothly) when the keyboard pops up, and when the keyboard goes away, the fields should go back down smoothly. Does anyone know how I can do this?
Also I want to avoid putting in fixed amount of pixels to move, because then it will be device dependent. If possible maybe use something like ems units, so it works for all screen densities.
Thanks
Upvotes: 1
Views: 288
Reputation: 21937
To do this you have to implement *addOnGlobalLayoutLitener. Wrap up the whole child view using scrollview and then scroll the layout. Test your app by changing the scrolling value. *
final View activityRootView = findViewById(R.id.signinRootView);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int heightDiff = activityRootView.getRootView()
.getHeight() - activityRootView.getHeight();
if (heightDiff > 100) {
((ScrollView) findViewById(R.id.scrollview))
.scrollTo(0, findViewById(R.id.et_password)
.getBottom() + 80);
} else
((ScrollView) findViewById(R.id.scrollview))
.scrollTo(0, 0);
}
});
**
Upvotes: 2