user2210837
user2210837

Reputation: 105

How to detect Virtual Keyboard Events in Android with Delphi

I'm trying to develop an Android app with Rad studio Xe5 with Delphi, and i having the following problem:

There is a Tmemo, which is at the bottom of the screen, and at the time of pressing it for typing some text, the virtual keyboard is showed over the Tmemo which i can't see. I would like to detect the event on show keyboard and move change the position of that Tmemo. I will do the same when the Virtual Keyboard is hide, and bring back the Tmemo to his original position. Does anybody know hot to detect the event on keyboard show and hide?

Best Regards

Upvotes: 2

Views: 4325

Answers (2)

wordermorr
wordermorr

Reputation: 325

I had the same problem with the keyboard over tmemo, try these two functions and the events OnVirtualKeyboardShown and OnVirtualKeyboardHidden

public
    { Public declarations }
    FSavedY: Single;
    FocusControl: TControl;
    ParentedControl: TFMXObject;
    function FocusedControl: TControl;
    function GetFocusedControlOffset(KeyboardRect: TRect): Single;

...

function TfrmFeedBackMobile.FocusedControl: TControl;
begin
  Result := nil;
  if Assigned(Focused) and (Focused.GetObject is TControl) then
    Result := TControl(Focused.GetObject);
end;

function TfrmFeedBackMobile.GetFocusedControlOffset(KeyboardRect: TRect): Single;
var
  Control: TControl;
  ControlPos: TPointF;
  KeyboardTop: Single;
begin
  Result := 0;
  KeyboardTop := Height - (KeyboardRect.Bottom - KeyboardRect.Top) - 66;
  // At least, should be. 66 is the height of the keyboard "done" bar
  Control := FocusedControl;
  if Assigned(Control) then
  begin
    ControlPos := Control.LocalToAbsolute(PointF(0, 0));
    Result := KeyboardTop - ControlPos.Y + Control.Height + 2;
    if Result >= 0 then
      Result := 0;
  end;
end;

procedure TfrmFeedbackMobile.FormVirtualKeyboardHidden(Sender: TObject;
  KeyboardVisible: Boolean; const Bounds: TRect);
begin
  FocusControl.Parent:= ParentedControl;
  FocusControl.AnimateFloat('Position.Y', FSavedY, 0.1);
  FocusControl.Align := TAlignLayout.alClient;
  FocusControl:= nil;
end;

procedure TfrmFeedbackMobile.FormVirtualKeyboardShown(Sender: TObject;
  KeyboardVisible: Boolean; const Bounds: TRect);
begin
  FocusControl:= FocusedControl;
  if not (FocusControl is TMemo) then Exit;

  FocusControl.Align := TAlignLayout.alNone;
  FSavedY := FocusControl.Position.Y;
  FocusControl.Position.Y:= 0;
  FocusControl.AnimateFloat('Position.Y',
    FSavedY + GetFocusedControlOffset(Bounds), 0.1);
  ParentedControl:= FocusControl.Parent;
  FocusControl.Parent:= frmFeedbackMobile;
  FocusControl.BringToFront;
end;

Upvotes: 0

user3428540
user3428540

Reputation:

You can use this to hide the keyboard,

private InputMethodManager mKeyboard;
    mKeyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    mKeyboard.hideSoftInputFromWindow(countryTo.getWindowToken(), 0);

Upvotes: 1

Related Questions