1

Simple question:

How can I anchor a view (specifically EditText) to the top of the soft keyboard when it shows?

I don't want to use adjustPan or adjustResize because I have views at the top of my activity that need to stay visible and I don't want to resize the activity. I only want to place one view just above the keyboard. Unfortunately I haven't found a solution to this problem

Ben Kane
  • 9,331
  • 6
  • 36
  • 58

1 Answers1

1

There is no way to do this. The soft keyboard runs in its own process and is not a view within your app. The system tries to keep the view in focus on screen, and the only thing you can do is instruct the system how it should manipulate your application's window (by resizing or panning).

Karakuri
  • 38,365
  • 12
  • 84
  • 104
  • If there's no way to do it then how does Snap Chat do it? My app is nothing like that app, but I know for Snap Chat when you are editing the text on your image, the view animates and slides down until it is directly above the default soft keyboard. So it must be possible. – Ben Kane Jul 10 '13 at 15:18
  • Perhaps they simply slide it down to the bottom of the window. hat I was saying is you cannot anchor it *to the keyboard*. – Karakuri Jul 10 '13 at 15:20
  • They don't adjust their root view though, so wouldn't the bottom of the window still be the bottom of the screen? – Ben Kane Jul 10 '13 at 15:22
  • I know I can't anchor it to the keyboard, but I also can't get the height of the keyboard and my root view doesn't move so I can't calculate a height difference. – Ben Kane Jul 10 '13 at 15:25
  • I have no idea what they do. For all I know, they could actually be opening a transparent activity (with no transition animation), anchoring a different EditText that appears the same to the bottom of its window, and letting that window resize. – Karakuri Jul 10 '13 at 15:26
  • If you allow your window to resize, there's a hacky way to get the height of the keybaord. See http://stackoverflow.com/a/4737265/1207921 – Karakuri Jul 10 '13 at 15:29
  • I don't know or care what they do either. That idea is interesting, though. If I did that, would I be able affect views in my original activity based on the text changing in the transparent activity? Not sure if that makes sense to you or not. Currently while my `EditText` changes, it sets the text in a `TextView` at the same time. I'd need to keep that functionality. If I can do that, then that is a really good idea. – Ben Kane Jul 10 '13 at 15:34
  • I've been to that thread lots of times. I can't let my window resize, though, so it won't work for me. – Ben Kane Jul 10 '13 at 15:36
  • You could use `startActivityForResult` and have the second activity pass back the new value for the text. You would receive it in `onActivityResult`. – Karakuri Jul 10 '13 at 15:37
  • That will only allow me to get the full text when they hit done on the keyboard, correct? I need to update the `TextView` every time they add or remove a character. Live updates. That's the behavior I have right now. – Ben Kane Jul 10 '13 at 15:41
  • What if you hid the real one while the second activity was showing, and have the second activity show a fake one that updates live? I don't know how this will work out, it's really up to you to find an implementation and decide if the added complexity is really worth the effort for a particular effect. Don't feel compelled to behave exactly like some other app... – Karakuri Jul 10 '13 at 15:46
  • Oh wait I could use that intent in my `TextWatcher`'s `onTextChanged` method and yea wow okay that might work. Have any opinions? Do you think that would actually work? Seems legit but I don't want to code it if it won't work. Then again it doesn't hurt to try. – Ben Kane Jul 10 '13 at 15:48
  • Eh hiding the real one won't work. Too many other views that could have added at any time that need to stay visible. Also, I'm not trying to make it behave like another app, I'm trying to make it behave like the existing iOS version of this app. I was just saying I noticed Snap Chat has done something similar so it must be possible on Android. – Ben Kane Jul 10 '13 at 15:53
  • Okay this might just not be possible on Android. What I need to have happen is have the entire activity slide up actually. That is fine to have it pan (not resize). BUT only slide up if the `TextView` would be under the keyboard. But my text views can be dragged and rotated so I don't know where they will be. I don't want panning my root view up unless the TextView they clicked would be below the keyboard. Starting to think this isn't possible. But anything is possible. – Ben Kane Jul 10 '13 at 16:03
  • Yea I don't know how this is possible. However Snap Chat developers did it is something like what I need. If they clicked a `TextView` near the top I don't want to activity to pan up, but I still need the `EditText` anchored to the top of the keyboard. But if it's near the bottom I need the activity to shift up so they can see the `TextView` update. – Ben Kane Jul 10 '13 at 16:06