How can I clear keyboard focus on a page when I exit and return? - xamarin.forms

I have a Xamarin.Forms (3.4) app with several Content pages, some of which have several entry boxes. If go to a page, then edit an entry, so far everything is fine. But when I "exit" the page (by doing a Navigation.PopModalAsync() ), and then return to the page, on iOS there is immediately a cursor in the entry, and also the keyboard pops-up. This is not what my user will expect.
On Android, the cursor is in the entry, but it does not pop up the keyboard automatically. This too is not desireable.
For refernce, on UWP it doesn't show focus when returning to the page.
How can I avoid this behavior? Is there a way to have the page "clear" the focus?

You should do something like:
override protected void OnAppearing()
{
entry.Unfocus();
}

Related

Onscreen Keyboard Touch Screen popup up with or without focus in Qt WASM

I am working on an application to be deployed as a wasm app and a windows application.
we are using a windows 10 OS touch screen tablet and google chrome to access the web app. am using an empty new qt project to demonstrate the problem :
The onscreen Keyboard popups up regardless of focus meaning it will pop up wherever i touch the screen:
if btn is pressed
if lineedit is selected
if empty widget space is touched even though there is no focus object behind it.
i include a link to this Behaviour Video so you can see the problem.
the onscreen Keyboard popup without focus problem occurs only if i compile for webassembly, works fine on the same tablet for MSVC.
what i tried :
catch the events then ignore them using :
ui->centralwidget->installEventFilter(this);
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
//print event to qdebug
static int eventEnumIndex = QEvent::staticMetaObject.indexOfEnumerator("Type");
QString TEXT_Event = QEvent::staticMetaObject.enumerator(eventEnumIndex).valueToKey(event->type());;
qDebug()<<"TEXT EVENT="<<TEXT_Event;
if(TEXT_Event.contains("Paint")){
//dont show print event
}else{
ui->Main_PlainTextEdit->appendPlainText(obj->objectName()+"=>"+TEXT_Event);
}
if( event->type()==QEvent::MouseButtonPress|| event->type()==QEvent::MouseButtonRelease)
{
// handle on-screen keyboard
event->ignore();
event->accept();
}
return true;
}
setAttribute(Qt::WA_TransparentForMouseEvents);
not OK as it deactivates all mouse input => no interaction possible,
setAttribute(Qt::WA_AcceptTouchEvents);
This only changes the event from Mouse event to touch event.
Maybe there is an option that i need to tick in the form editor or touchscreen option that needs to be activated, maybe the way to catch and ignore event i implemented is wrong.
I don't know what i am doing wrong but all my attempts to fix this didn't work, please help guide me ?
Thank you in advance.
Here is how I solved it in Qt 5.15. To prevent the keyboard from popping up, I edited the default index.html. Bu default, there is a line:
<canvas id="qtcanvas" oncontextmenu="event.preventDefault()" contenteditable="true"></canvas>
I added inputmode="none", meaning I changed it to:
<canvas id="qtcanvas" oncontextmenu="event.preventDefault()" contenteditable="true" inputmode="none"></canvas>
What this does, is it prevent the onscreen keyboard from popping up. But note that it will not popup even when a line edit is in focus, so one must use either a physical keyboard or a custom virtual keyboard widget if typing text is required in the UI.
Hopefully it will get better in future Qt versions, I see there is currently some fixes scheduled for Qt 6.4 https://bugreports.qt.io/browse/QTBUG-83064

How to keep the software keyboard in Android from hiding after a TextBox loses focus in Uno Platform?

I have a TextBox and a Button. When the button is clicked, the TextBox loses focus and the keyboard hides. How do I keep the keyboard from hiding when the button is clicked?
I first thought to use AllowFocusOnInteraction, but this is not supported on Android at this time.
Although AllowFocusOnInteraction isn't implemented, Control.IsTabStop is. IsTabStop likewise prevents the control from receiving focus. Use IsTabStop instead.
Uno Platform is using the Android input behavior here, so when the input loses focus, the programmatic keyboard automatically disappears. However, if you want to show it again immediately, you can give it focus again from within the button Click handler.
XAML:
<TextBox x:Name="InputBox" />
<Button Click="TestClick">Test</Button>
C#:
private void TestClick(object sender, RoutedEventArgs e)
{
InputBox.Focus(FocusState.Programmatic);
}
The input pane starts disappearing, but is immediately requested again, so it is almost imperceptible.
You can also try to trigger the soft keyboard programmatically without having any TextBox in place, by writing platform-specific code. This SO question for example includes quite a few solutions for Android. In general, they utilize the INPUT_METHOD_SERVICE and call ShowSoftInput on it.

Xamarin.Forms: Get if side menu from master detail is currently visible?

I am trying to change a button in the side menu drawer, depening on whether the user is logged in or not. I hoped I could simply just check everytime the side menu is opened via the burger icon by overriding OnAppearing():
protected override void OnAppearing()
{
base.OnAppearing();
SetButtonAccordingToPosition();
}
Unfortunately, this function is only called ONCE when the app starts and then never again anymore.
I need to find out when the side menu is visible. How would i do that?

Xamarin App Shell - closing the flyout menu in code

The design of my app requires that I have a button in the Flyout Header, a separate view, which navigates to a page
Unlike the flyout items themselves though, when I click on the button, the page loads under the flyout header which stays open
Is there a way to have a button mimic exactly what happens when navigating within the flyout contents themselves?
The page I'm trying to navigate to is registered as a route in AppShell
The code in the view referenced in FlyoutHeader calls it from the button click like so
await Shell.Current.GoToAsync("thepage");
As mentioned above the flyout menu is open at this point in order to access the button but when clicked it loads the desired page but I want it to automatically shut the menu
Is there a way to do this please?
As Ricardo says above.
You can use Shell.Current.FlyoutIsPresented = false;
//===event click or command===
private async void Button_Clicked(object sender, System.EventArgs e)
{
await Shell.Current.GoToAsync($"your others pages route");
Shell.Current.FlyoutIsPresented = false;
}

android don't lose Fragment when back button is pressed

The user create and add some Fragments to the Activity identified by a tag. I noticed that on pressing the back button the Fragment is destroyed. How can I don't lose all the fragments when the back button is pressing? So that I can navigate without recreating all the time the fragments. I'm asking for a properly way to do this, indeed for now my idea is to override the onBackPressed() and save the Fragment in a List global variable of Fragments before destroy it.
While creating the fragment's instead of FragmentTranscation.replace, use FragmentTranscation.addTobackStack() to add the fragment to backstack and do nothing onBackPressed.
On pressing back button FragmentTranscation will take care of navigating to the previous fragments.
You can try this in the onBackPressed() function.
if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
getSupportFragmentManager().popBackStack();
} else {
super.onBackPressed();
}

Resources