Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In NativeScript on Android, how do I prevent a SearchBar from gaining focus on page load?

Tags:

nativescript

I have a SeachBar inside a ScrollView. In iOS all is good. On Android the ScrollView automatically scrolls to the SearchBar, adds focus to it and displays the soft keyboard. I can hide the softkeyboard by adding android:windowSoftInputMode="stateHidden" as an activity in the AndroidManifest.xml file but I can't work out how to prevent the focus (and hence the auto scroll). Any help would be much appreciated.

like image 366
Raef Akehurst Avatar asked Mar 08 '16 13:03

Raef Akehurst


1 Answers

Using Angular2:

app/my.component.html

<StackLayout (loaded)="onSearchLayoutLoaded($event)">
    <SearchBar hint="search here" (loaded)="onSearchBarLoaded($event)">
    </SearchBar>
</StackLayout>

app/my.component.ts

    onSearchLayoutLoaded(event) {
        if (event.object.android) {
            event.object.android.setFocusableInTouchMode(true);
        }
    }

    onSearchBarLoaded(event) {
        if (event.object.android) {
            event.object.android.clearFocus();
        }
    }

This eliminates unnecessarily having to use template reference variables.

like image 161
EricRobertBrewer Avatar answered Oct 14 '22 20:10

EricRobertBrewer