Skip to content

Commit b2807bf

Browse files
committed
UNC: use SearchView in Action bar instead of explicit edit text view
1 parent 406d91a commit b2807bf

File tree

3 files changed

+69
-40
lines changed

3 files changed

+69
-40
lines changed

res/layout/led_control_activity.xml

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,7 @@
1616
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
1717
android:layout_width="match_parent"
1818
android:layout_height="match_parent"
19-
android:orientation="vertical"
20-
android:focusable="true"
21-
android:focusableInTouchMode="true" >
22-
23-
<EditText
24-
android:id="@+id/input_search"
25-
android:layout_width="match_parent"
26-
android:layout_height="wrap_content"
27-
android:hint="@string/app_picker_search"
28-
android:inputType="text"
29-
android:saveEnabled="false"
30-
android:visibility="gone" />
19+
android:orientation="vertical" >
3120

3221
<ListView
3322
android:id="@android:id/list"

res/menu/led_control_activity_menu.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,17 @@
1414
* limitations under the License.
1515
-->
1616
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
17+
18+
<item android:id="@+id/search"
19+
android:title="@string/search"
20+
android:icon="@android:drawable/ic_menu_search"
21+
android:showAsAction="collapseActionView|ifRoom"
22+
android:actionViewClass="android.widget.SearchView" />
23+
1724
<item android:id="@+id/lc_activity_menu_show_all"
1825
android:title="@string/lc_activity_menu_show_all" />
26+
1927
<item android:id="@+id/lc_activity_menu_show_active"
2028
android:title="@string/lc_activity_menu_show_active" />
29+
2130
</menu>

src/com/ceco/oreo/gravitybox/ledcontrol/LedControlActivity.java

Lines changed: 59 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2017 Peter Gregus for GravityBox Project (C3C076@xda)
2+
* Copyright (C) 2018 Peter Gregus for GravityBox Project (C3C076@xda)
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
55
* You may obtain a copy of the License at
@@ -29,68 +29,94 @@
2929
import android.content.pm.PackageManager;
3030
import android.os.AsyncTask;
3131
import android.os.Bundle;
32-
import android.text.Editable;
33-
import android.text.TextWatcher;
3432
import android.view.Menu;
3533
import android.view.MenuItem;
3634
import android.view.View;
3735
import android.widget.AdapterView;
38-
import android.widget.EditText;
3936
import android.widget.AdapterView.OnItemClickListener;
37+
import android.widget.SearchView.OnQueryTextListener;
4038
import android.widget.ListView;
39+
import android.widget.SearchView;
4140

4241
public class LedControlActivity extends GravityBoxListActivity implements ListItemActionHandler, OnItemClickListener {
4342

4443
private static final int REQ_SETTINGS = 1;
44+
private static final String KEY_ACTIVE_ONLY = "showActiveOnly";
45+
private static final String KEY_SEARCH_QUERY = "searchQuery";
4546

4647
private ListView mList;
4748
private AsyncTask<Void, Void, ArrayList<LedListItem>> mAsyncTask;
4849
private ProgressDialog mProgressDialog;
4950
private LedListItem mCurrentItem;
50-
private EditText mSearchEditText;
5151
private boolean mShowActiveOnly;
52+
private String mSearchQuery;
53+
private SearchView mSearchView;
5254

5355
@Override
5456
public void onCreate(Bundle savedInstanceState) {
5557
super.onCreate(savedInstanceState);
5658

5759
if (savedInstanceState != null) {
58-
mShowActiveOnly = savedInstanceState.getBoolean("showActiveOnly", false);
60+
mShowActiveOnly = savedInstanceState.getBoolean(KEY_ACTIVE_ONLY, false);
61+
mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY, null);
5962
}
6063

6164
setContentView(R.layout.led_control_activity);
6265

6366
mList = getListView();
6467
mList.setOnItemClickListener(this);
6568

66-
mSearchEditText = (EditText) findViewById(R.id.input_search);
67-
mSearchEditText.addTextChangedListener(new TextWatcher() {
68-
@Override
69-
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
70-
71-
@Override
72-
public void onTextChanged(CharSequence s, int start, int before, int count) {
73-
if (mList.getAdapter() != null) {
74-
((LedListAdapter)mList.getAdapter()).getFilter().filter(s);
75-
}
76-
}
77-
78-
@Override
79-
public void afterTextChanged(Editable s) { }
80-
});
81-
8269
setData();
8370
}
8471

8572
@Override
8673
public void onStop() {
8774
cancelSetData();
75+
mSearchView.clearFocus();
8876
super.onStop();
8977
}
9078

9179
@Override
9280
public boolean onCreateOptionsMenu(Menu menu) {
9381
getMenuInflater().inflate(R.menu.led_control_activity_menu, menu);
82+
83+
final MenuItem search = menu.findItem(R.id.search);
84+
mSearchView = (SearchView) search.getActionView();
85+
86+
if (mSearchQuery != null) {
87+
mSearchView.setQuery(mSearchQuery, false);
88+
}
89+
90+
mSearchView.setOnQueryTextListener(new OnQueryTextListener() {
91+
@Override
92+
public boolean onQueryTextChange(String text) {
93+
mSearchQuery = text;
94+
if (mList.getAdapter() != null) {
95+
((LedListAdapter)mList.getAdapter()).getFilter().filter(mSearchQuery);
96+
}
97+
return true;
98+
}
99+
@Override
100+
public boolean onQueryTextSubmit(String text) {
101+
mSearchView.clearFocus();
102+
return true;
103+
}
104+
});
105+
106+
int closeBtnResId = getResources().getIdentifier(
107+
"android:id/search_close_btn", null, null);
108+
if (closeBtnResId != 0) {
109+
View closeBtn = mSearchView.findViewById(closeBtnResId);
110+
if (closeBtn != null) {
111+
closeBtn.setOnClickListener(new View.OnClickListener() {
112+
@Override
113+
public void onClick(View v) {
114+
search.collapseActionView();
115+
}
116+
});
117+
}
118+
}
119+
94120
return true;
95121
}
96122

@@ -139,11 +165,12 @@ protected ArrayList<LedListItem> doInBackground(Void... arg0) {
139165
@Override
140166
protected void onPostExecute(ArrayList<LedListItem> result) {
141167
dismissProgressDialog();
142-
mSearchEditText.setText("");
143-
mList.setAdapter(new LedListAdapter(LedControlActivity.this, result,
144-
LedControlActivity.this));
145-
((LedListAdapter)mList.getAdapter()).notifyDataSetChanged();
146-
mSearchEditText.setVisibility(mShowActiveOnly ? View.GONE : View.VISIBLE);
168+
LedListAdapter adapter = new LedListAdapter(LedControlActivity.this, result,
169+
LedControlActivity.this);
170+
if (mSearchQuery != null) {
171+
adapter.getFilter().filter(mSearchQuery);
172+
}
173+
mList.setAdapter(adapter);
147174
}
148175
}.execute();
149176
}
@@ -213,6 +240,10 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
213240

214241
@Override
215242
public void onSaveInstanceState(Bundle bundle) {
216-
bundle.putBoolean("showActiveOnly", mShowActiveOnly);
243+
bundle.putBoolean(KEY_ACTIVE_ONLY, mShowActiveOnly);
244+
if (mSearchQuery != null) {
245+
bundle.putString(KEY_SEARCH_QUERY, mSearchQuery);
246+
}
247+
super.onSaveInstanceState(bundle);
217248
}
218249
}

0 commit comments

Comments
 (0)