Skip to content

Commit b1a4ee1

Browse files
authored
Merge pull request #104 from winstonsung/dev-104
Add "selected" styles to in-app language picker
2 parents 88e232b + f8b7c4d commit b1a4ee1

File tree

3 files changed

+57
-14
lines changed

3 files changed

+57
-14
lines changed

app/src/main/java/app/opass/ccip/ui/LanguagePreferenceFragment.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.os.Bundle
55
import android.view.LayoutInflater
66
import android.view.View
77
import android.view.ViewGroup
8+
import android.widget.ImageView
89
import android.widget.TextView
910
import androidx.appcompat.app.AppCompatDelegate
1011
import androidx.core.os.LocaleListCompat
@@ -82,6 +83,7 @@ class LanguagePreferenceFragment : DialogFragment() {
8283
)
8384
) { item ->
8485
dialog?.dismiss()
86+
8587
AppCompatDelegate.setApplicationLocales(
8688
(
8789
if (item.languageTag == "x-default") {
@@ -126,12 +128,25 @@ class LanguagePreferenceAdapter(
126128

127129
override fun onBindViewHolder(holder: LanguagePreferenceViewHolder, position: Int) {
128130
val item = items[position]
131+
val isSelected = (
132+
if (item.languageTag == "x-default") {
133+
AppCompatDelegate.getApplicationLocales() == LocaleListCompat.getEmptyLocaleList()
134+
} else {
135+
AppCompatDelegate.getApplicationLocales() == LocaleListCompat.forLanguageTags(item.languageTag)
136+
}
137+
)
138+
129139
holder.localName.text = item.localName
130140
holder.translatedName.text = item.translatedName
141+
142+
if (!isSelected) {
143+
holder.selectedIcon.setImageDrawable(null)
144+
}
131145
}
132146
}
133147

134148
class LanguagePreferenceViewHolder(view: View) : RecyclerView.ViewHolder(view) {
135149
val localName: TextView = view.findViewById(R.id.option_local_name)
136150
val translatedName: TextView = view.findViewById(R.id.option_translated_name)
151+
val selectedIcon: ImageView = view.findViewById(R.id.icon_selected)
137152
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportWidth="24"
5+
android:viewportHeight="24"
6+
android:tint="?attr/colorControlNormal">
7+
<path
8+
android:fillColor="@android:color/white"
9+
android:pathData="M9,16.2L4.8,12l-1.4,1.4L9,19 21,7l-1.4,-1.4L9,16.2z" />
10+
</vector>
Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,46 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
34
xmlns:tools="http://schemas.android.com/tools"
45
android:layout_width="match_parent"
56
android:layout_height="wrap_content"
67
android:background="?attr/selectableItemBackground"
78
android:focusable="true"
8-
android:orientation="vertical"
9+
android:orientation="horizontal"
910
android:paddingStart="32dp"
1011
android:paddingTop="16dp"
11-
android:paddingEnd="8dp"
12+
android:paddingEnd="16dp"
1213
android:paddingBottom="16dp">
1314

14-
<TextView
15-
android:id="@+id/option_local_name"
16-
style="@style/TextAppearance.MaterialComponents.Body1"
17-
android:layout_width="wrap_content"
15+
<LinearLayout
16+
android:layout_weight="1"
17+
android:layout_width="0dp"
1818
android:layout_height="wrap_content"
19-
android:textColor="@color/textColorPrimary"
20-
tools:text="繁體中文" />
19+
android:background="?attr/selectableItemBackground"
20+
android:focusable="true"
21+
android:orientation="vertical">
22+
<TextView
23+
android:id="@+id/option_local_name"
24+
style="@style/TextAppearance.MaterialComponents.Body1"
25+
android:layout_width="wrap_content"
26+
android:layout_height="wrap_content"
27+
android:textColor="@color/textColorPrimary"
28+
tools:text="繁體中文" />
2129

22-
<TextView
23-
android:id="@+id/option_translated_name"
24-
android:layout_width="wrap_content"
25-
android:layout_height="wrap_content"
26-
android:layout_marginTop="4dp"
27-
tools:text="Mandarin - Traditional Han script" />
30+
<TextView
31+
android:id="@+id/option_translated_name"
32+
android:layout_width="wrap_content"
33+
android:layout_height="wrap_content"
34+
android:layout_marginTop="4dp"
35+
tools:text="Mandarin - Traditional Han script" />
36+
</LinearLayout>
37+
38+
<ImageView
39+
android:id="@+id/icon_selected"
40+
android:layout_width="35dip"
41+
android:layout_height="fill_parent"
42+
android:padding="5dp"
43+
android:src="@drawable/ic_baseline_done_24"
44+
app:tint="@color/colorWhite"
45+
tools:tint="@color/colorAccent"/>
2846
</LinearLayout>

0 commit comments

Comments
 (0)