Skip to content

Commit 1d20845

Browse files
author
Alexander Vakrilov
authored
Use AppCompat SearchView (NativeScript#4371)
* Use AppCompat SearchView * GetNative elements for appcompat * Classes renamed * test modified
1 parent 4319ca5 commit 1d20845

File tree

3 files changed

+37
-23
lines changed

3 files changed

+37
-23
lines changed

apps/app/ui-tests-app/search-bar/issue-4147.xml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
<NavigationButton text="Go Back" android.systemIcon="ic_menu_back" tap="onNavBtnTap"/>
66
</ActionBar>
77
</Page.actionBar>
8-
<StackLayout>
9-
<!--The background-color shouldn't be inherit but in Android it is-->
10-
<SearchBar id="bg-color" hint="bg-color" text="" textFieldHintColor="green" />
11-
<SearchBar class="color" hint="color" text="color" />
12-
<SearchBar hint="Hint Text" id="searchBar" textFieldHintColor="#FFFFFF" style="color:#FFFFFF;background-color: #795548;height:60" />
13-
<SearchBar hint="Hint Text" textFieldHintColor="#FFFFFF" textFieldBackgroundColor="red" style="background-color:green;" />
14-
</StackLayout>
8+
<GridLayout rows="auto auto auto auto *">
9+
<SearchBar row="0" id="bg-color" hint="bg-color" text="" textFieldHintColor="green" />
10+
<SearchBar row="1" class="color" hint="color" text="color" />
11+
<SearchBar row="2" hint="Hint Text" id="searchBar" textFieldHintColor="#FFFFFF" style="color:#FFFFFF;background-color: #795548;height:60" />
12+
<SearchBar row="3" hint="Hint Text" textFieldHintColor="#FFFFFF" textFieldBackgroundColor="red" style="background-color:green;" />
13+
</GridLayout>
1514
</Page>

tests/app/ui/search-bar/search-bar-tests-native.android.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import * as utils from "tns-core-modules/utils/utils";
44

55
function getTextView(bar: android.widget.SearchView): android.widget.TextView {
66
if (bar) {
7-
var id = bar.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
7+
const pkgName = bar.getContext().getPackageName();
8+
var id = bar.getContext().getResources().getIdentifier("search_src_text", "id", pkgName);
89
if (id) {
9-
return <android.widget.TextView> bar.findViewById(id);
10+
return <android.widget.TextView>bar.findViewById(id);
1011
}
1112
}
1213

tns-core-modules/ui/search-bar/search-bar.android.ts

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ const SEARCHTEXT = Symbol("searchText");
1111
const QUERY = Symbol("query");
1212

1313
interface QueryTextListener {
14-
new (owner: SearchBar): android.widget.SearchView.OnQueryTextListener;
14+
new (owner: SearchBar): android.support.v7.widget.SearchView.OnQueryTextListener;
1515
}
1616

1717
interface CloseListener {
18-
new (owner: SearchBar): android.widget.SearchView.OnCloseListener;
18+
new (owner: SearchBar): android.support.v7.widget.SearchView.OnCloseListener;
1919
}
2020

2121
let QueryTextListener: QueryTextListener;
@@ -26,8 +26,8 @@ function initializeNativeClasses(): void {
2626
return;
2727
}
2828

29-
@Interfaces([android.widget.SearchView.OnQueryTextListener])
30-
class QueryTextListenerImpl extends java.lang.Object implements android.widget.SearchView.OnQueryTextListener {
29+
@Interfaces([android.support.v7.widget.SearchView.OnQueryTextListener])
30+
class CompatQueryTextListenerImpl extends java.lang.Object implements android.support.v7.widget.SearchView.OnQueryTextListener {
3131
constructor(private owner: SearchBar) {
3232
super();
3333
return global.__native(this);
@@ -58,8 +58,8 @@ function initializeNativeClasses(): void {
5858
}
5959
}
6060

61-
@Interfaces([android.widget.SearchView.OnCloseListener])
62-
class CloseListenerImpl extends java.lang.Object implements android.widget.SearchView.OnCloseListener {
61+
@Interfaces([android.support.v7.widget.SearchView.OnCloseListener])
62+
class CompatCloseListenerImpl extends java.lang.Object implements android.support.v7.widget.SearchView.OnCloseListener {
6363
constructor(private owner: SearchBar) {
6464
super();
6565
return global.__native(this);
@@ -71,12 +71,14 @@ function initializeNativeClasses(): void {
7171
}
7272
}
7373

74-
QueryTextListener = QueryTextListenerImpl;
75-
CloseListener = CloseListenerImpl;
74+
QueryTextListener = CompatQueryTextListenerImpl;
75+
CloseListener = CompatCloseListenerImpl;
7676
}
7777

7878
export class SearchBar extends SearchBarBase {
79-
nativeView: android.widget.SearchView;
79+
nativeView: android.support.v7.widget.SearchView;
80+
private _searchTextView: android.widget.TextView;
81+
private _searchPlate: android.widget.LinearLayout;
8082

8183
public dismissSoftInput() {
8284
ad.dismissSoftInput(this.nativeView);
@@ -93,7 +95,7 @@ export class SearchBar extends SearchBarBase {
9395

9496
public createNativeView() {
9597
initializeNativeClasses();
96-
const nativeView = new android.widget.SearchView(this._context);
98+
const nativeView = new android.support.v7.widget.SearchView(this._context)
9799
nativeView.setIconified(false);
98100

99101
const queryTextListener = new QueryTextListener(this);
@@ -118,6 +120,8 @@ export class SearchBar extends SearchBarBase {
118120
const nativeView: any = this.nativeView;
119121
nativeView.closeListener.owner = null;
120122
nativeView.queryTextListener.owner = null;
123+
this._searchPlate = null;
124+
this._searchTextView = null;
121125
super.disposeNativeView();
122126
}
123127

@@ -208,12 +212,22 @@ export class SearchBar extends SearchBarBase {
208212
}
209213

210214
private _getTextView(): android.widget.TextView {
211-
const id = this.nativeView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
212-
return <android.widget.TextView>this.nativeView.findViewById(id);
215+
if (!this._searchTextView) {
216+
const pkgName = this.nativeView.getContext().getPackageName();
217+
const id = this.nativeView.getContext().getResources().getIdentifier("search_src_text", "id", pkgName);
218+
this._searchTextView = <android.widget.TextView>this.nativeView.findViewById(id);
219+
}
220+
221+
return this._searchTextView;
213222
}
214223

215224
private _getSearchPlate(): android.widget.LinearLayout {
216-
const id = this.nativeView.getContext().getResources().getIdentifier("android:id/search_plate", null, null);
217-
return <android.widget.LinearLayout>this.nativeView.findViewById(id);
225+
if (!this._searchPlate) {
226+
const pkgName = this.nativeView.getContext().getPackageName();
227+
const id = this.nativeView.getContext().getResources().getIdentifier("search_plate", "id", pkgName);
228+
this._searchPlate = <android.widget.LinearLayout>this.nativeView.findViewById(id);
229+
}
230+
231+
return this._searchPlate;
218232
}
219233
}

0 commit comments

Comments
 (0)