in my current project I have a view with some textview in the upper third, an edittext in the middle and a button at the bottom, all taking roughly the same space. Now when I want to enter something in the edittext, the keyboard pops up and either the view is not resized, so the keyboard hides half of the edittext, which looks crappy, or the view is resized, shrinked to half its size, which looks…very… crappy. In the iphone version of my app I made it so when I tapped the edittext it would grow and enlarge, filling the whole view, so only the edittext and the keyboard are shown. The problem is, I have absolutely no clue how to do it in Android. I already rebuild my xml layout to use relativelayout instead of linearlayout, as I thought it would be easier.
Then I put the change in the onFocusChange method like so:
et = (EditText)v.findViewById(R.id.etItems);
et.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View arg0, boolean arg1) {
if(arg0==et) {
if(arg1==true) {
Log.i("EditText","got focus");
enlargeEditText();
}
} else {
Log.i("EditText","lost focus");
}
}
});
private void enlargeEditText() {
RelativeLayout.LayoutParams rllp = (LayoutParams) v.getLayoutParams();
rllp.setMargins(0, 0, 0, 0);
v.setLayoutParams(rllp);
}
The enlargeEditText method isnt final, I justed wanted to see if there is something changing, but…it isn’t…instead I get :
08-22 17:26:22.108: E/AndroidRuntime(20662): at de.lochmann.einkaufsliste.FragCreateList$1.onFocusChange(FragCreateList.java:88)
08-22 17:26:22.108: E/AndroidRuntime(20662): at android.view.View.onFocusChanged(View.java:3938)
08-22 17:26:22.108: E/AndroidRuntime(20662): at android.widget.TextView.onFocusChanged(TextView.java:8465)
08-22 17:26:22.108: E/AndroidRuntime(20662): at android.widget.EditText.onFocusChanged(EditText.java:136)
08-22 17:26:22.108: E/AndroidRuntime(20662): at android.view.View.handleFocusGainInternal(View.java:3760)
08-22 17:26:22.108: E/AndroidRuntime(20662): at android.view.View.requestFocus(View.java:5453)
08-22 17:26:22.108: E/AndroidRuntime(20662): at android.view.View.requestFocus(View.java:5403)
08-22 17:26:22.108: E/AndroidRuntime(20662): at android.view.View.requestFocus(View.java:5381)
What’s the problem? Any ideas? Thanks in advance.
The error is a ClassCastException. When you adjust the layout parameters of a view you must use the parent view’s parameters. The view you want to manipulate is the RelativeLayout, but the parameters probably belong to FrameLayout. I don’t know for certain because you have only posted part of your logcat.
This is a full LogCat trace:
You should notice that at the top there is the exception: the cause of the problem, the most vital piece of information a LogCat can give you. Also you seemed to have skipped a couple lines because I’m unaware of how the line
enlargeEditText();can compile but throw an exception…Anyway
Simply change the LayoutParameters:
It would also be nice to see how
vis defined in the LayoutInflater but I’m guessing that FrameLayout.LayoutParameters is the class that you want.