Skip to content

Commit f02cda4

Browse files
committed
Make it possible to translate accessibility content descriptions
1 parent 0c41bdd commit f02cda4

File tree

7 files changed

+47
-5
lines changed

7 files changed

+47
-5
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ And react to search results in your Activity:
5757
## Translations
5858

5959
This library currently contains only a limited number of translations. If you want to translate
60-
the texts shown by the library together with your app's other strings, you can just override
61-
the strings defined in `lib/src/main/res/values/strings.xml` in your own application by copying
62-
those lines to your app's `strings.xml`.
60+
the texts shown by the library together with your app's other strings, you can override
61+
the strings in the preference xml file using attributes like `search:textNoResults`.
62+
Refer to [`attrs.xml`](lib/src/main/res/values/attrs.xml) for details.
63+
You can also overwrite the strings when constructing the SearchConfiguration object.

lib/src/main/java/com/bytehamster/lib/preferencesearch/SearchConfiguration.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public class SearchConfiguration {
2828
private static final String ARGUMENT_TEXT_HINT = "text_hint";
2929
private static final String ARGUMENT_TEXT_CLEAR_HISTORY = "text_clear_history";
3030
private static final String ARGUMENT_TEXT_NO_RESULTS = "text_no_results";
31+
private static final String ARGUMENT_TEXT_CLEAR_INPUT = "text_clear_input";
32+
private static final String ARGUMENT_TEXT_MORE = "text_more";
3133

3234
private ArrayList<SearchIndexItem> filesToIndex = new ArrayList<>();
3335
private ArrayList<PreferenceItem> preferencesToIndex = new ArrayList<>();
@@ -43,6 +45,8 @@ public class SearchConfiguration {
4345
private String textClearHistory;
4446
private String textNoResults;
4547
private String textHint;
48+
private String textClearInput;
49+
private String textMore;
4650

4751
SearchConfiguration() {
4852

@@ -87,6 +91,8 @@ private Bundle toBundle() {
8791
arguments.putString(ARGUMENT_TEXT_HINT, textHint);
8892
arguments.putString(ARGUMENT_TEXT_CLEAR_HISTORY, textClearHistory);
8993
arguments.putString(ARGUMENT_TEXT_NO_RESULTS, textNoResults);
94+
arguments.putString(ARGUMENT_TEXT_CLEAR_INPUT, textClearInput);
95+
arguments.putString(ARGUMENT_TEXT_MORE, textMore);
9096
arguments.putString(ARGUMENT_HISTORY_ID, historyId);
9197
return arguments;
9298
}
@@ -103,6 +109,8 @@ static SearchConfiguration fromBundle(Bundle bundle) {
103109
config.textHint = bundle.getString(ARGUMENT_TEXT_HINT);
104110
config.textClearHistory = bundle.getString(ARGUMENT_TEXT_CLEAR_HISTORY);
105111
config.textNoResults = bundle.getString(ARGUMENT_TEXT_NO_RESULTS);
112+
config.textClearInput = bundle.getString(ARGUMENT_TEXT_CLEAR_INPUT);
113+
config.textMore = bundle.getString(ARGUMENT_TEXT_MORE);
106114
config.historyId = bundle.getString(ARGUMENT_HISTORY_ID);
107115
return config;
108116
}
@@ -298,6 +306,22 @@ public void setTextHint(String textHint) {
298306
this.textHint = textHint;
299307
}
300308

309+
public String getTextClearInput() {
310+
return textClearInput;
311+
}
312+
313+
public void setTextClearInput(String textClearInput) {
314+
this.textClearInput = textClearInput;
315+
}
316+
317+
public String getTextMore() {
318+
return textMore;
319+
}
320+
321+
public void setTextMore(String textMore) {
322+
this.textMore = textMore;
323+
}
324+
301325
/**
302326
* Adds a given R.xml resource to the search index
303327
*/

lib/src/main/java/com/bytehamster/lib/preferencesearch/SearchPreference.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ private void parseAttrs(AttributeSet attrs) {
5050
searchConfiguration.setTextNoResults(a.getText(0).toString());
5151
}
5252
a.recycle();
53+
a = getContext().obtainStyledAttributes(attrs, new int[] {R.attr.textClearInput});
54+
if (a.getText(0) != null) {
55+
searchConfiguration.setTextClearInput(a.getText(0).toString());
56+
}
57+
a.recycle();
58+
a = getContext().obtainStyledAttributes(attrs, new int[] {R.attr.textMore});
59+
if (a.getText(0) != null) {
60+
searchConfiguration.setTextMore(a.getText(0).toString());
61+
}
62+
a.recycle();
5363
}
5464

5565
@Override

lib/src/main/java/com/bytehamster/lib/preferencesearch/SearchPreferenceFragment.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
7373
if (searchConfiguration.getTextNoResults() != null) {
7474
viewHolder.noResults.setText(searchConfiguration.getTextNoResults());
7575
}
76+
if (searchConfiguration.getTextClearInput() != null) {
77+
viewHolder.clearButton.setContentDescription(searchConfiguration.getTextClearInput());
78+
}
79+
if (searchConfiguration.getTextMore() != null) {
80+
viewHolder.moreButton.setContentDescription(searchConfiguration.getTextMore());
81+
}
7682
viewHolder.moreButton.setOnClickListener(v -> {
7783
PopupMenu popup = new PopupMenu(getContext(), viewHolder.moreButton);
7884
popup.getMenuInflater().inflate(R.menu.searchpreference_more, popup.getMenu());

lib/src/main/res/layout/searchpreference_list_item_history.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
android:layout_marginStart="8dp"
1717
android:layout_marginRight="24dp"
1818
android:layout_marginEnd="24dp"
19-
android:contentDescription="@string/searchpreference_history_entry"
19+
android:importantForAccessibility="no"
2020
app:tint="?android:attr/textColorPrimary"
2121
app:srcCompat="@drawable/searchpreference_ic_history" />
2222

lib/src/main/res/values/attrs.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@
44
<attr name="textHint" format="string" />
55
<attr name="textNoResults" format="string" />
66
<attr name="textClearHistory" format="string" />
7+
<attr name="textClearInput" format="string" />
8+
<attr name="textMore" format="string" />
79
</declare-styleable>
810
</resources>

lib/src/main/res/values/strings.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<resources>
22
<string name="searchpreference_search">Search…</string>
33
<string name="searchpreference_clear">Clear</string>
4-
<string name="searchpreference_history_entry">History entry</string>
54
<string name="searchpreference_more">More</string>
65
<string name="searchpreference_clear_history">Clear history</string>
76
<string name="searchpreference_no_results">No results</string>

0 commit comments

Comments
 (0)