Support selected item state in Bilingual Adapter.

Support highlighting of selected locale.
Day theme: https://hsv.googleplex.com/4914118977912832
Night: https://hsv.googleplex.com/4816141579976704

Test: atest, flashed to device
Bug: 214045093
Change-Id: Ia02331bff908a8a7193c3087c4341bfb22f5545b
This commit is contained in:
Anna Zhuravleva
2022-03-23 17:15:48 +00:00
parent c4e0aa3376
commit ca2576868f
9 changed files with 139 additions and 10 deletions

View File

@@ -20,6 +20,8 @@ import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.android.internal.R;
@@ -36,11 +38,21 @@ public class BilingualSuggestedLocaleAdapter extends SuggestedLocaleAdapter {
private final Locale mSecondaryLocale;
private final int mSecondaryLocaleTextDir;
private final boolean mShowSelection;
private LocaleStore.LocaleInfo mSelectedLocaleInfo;
public BilingualSuggestedLocaleAdapter(
Set<LocaleStore.LocaleInfo> localeOptions,
boolean countryMode,
Locale secondaryLocale) {
this(localeOptions, countryMode, secondaryLocale, false);
}
public BilingualSuggestedLocaleAdapter(
Set<LocaleStore.LocaleInfo> localeOptions,
boolean countryMode,
Locale secondaryLocale,
boolean showLastSelected) {
super(localeOptions, countryMode);
mSecondaryLocale = secondaryLocale;
if (TextUtils.getLayoutDirectionFromLocale(secondaryLocale) == View.LAYOUT_DIRECTION_RTL) {
@@ -48,6 +60,7 @@ public class BilingualSuggestedLocaleAdapter extends SuggestedLocaleAdapter {
} else {
mSecondaryLocaleTextDir = View.TEXT_DIRECTION_LTR;
}
mShowSelection = showLastSelected;
}
@Override
@@ -90,11 +103,55 @@ public class BilingualSuggestedLocaleAdapter extends SuggestedLocaleAdapter {
}
LocaleStore.LocaleInfo item = (LocaleStore.LocaleInfo) getItem(position);
if (mShowSelection) {
setItemState(isSelectedLocaleInfo(item), convertView);
}
setLocaleToListItem(convertView, item);
}
return convertView;
}
/**
* Set locale info as selected. Selected info can be the only one. Passing null would result to
* nothing is selected.
*/
public void setSelectedLocaleInfo(LocaleStore.LocaleInfo info) {
mSelectedLocaleInfo = info;
notifyDataSetChanged();
}
/** Return selected locale info. */
public LocaleStore.LocaleInfo getSelectedLocaleInfo() {
return mSelectedLocaleInfo;
}
private boolean isSelectedLocaleInfo(LocaleStore.LocaleInfo item) {
return item != null
&& mSelectedLocaleInfo != null
&& item.getId().equals(mSelectedLocaleInfo.getId());
}
private void setItemState(boolean selected, View itemView) {
RelativeLayout background = (RelativeLayout) itemView;
ImageView indicator = itemView.findViewById(R.id.indicator);
TextView textNative = itemView.findViewById(R.id.locale_native);
TextView textSecondary = itemView.findViewById(R.id.locale_secondary);
if (indicator == null || textNative == null || textSecondary == null) {
return;
}
textNative.setSelected(selected);
textSecondary.setSelected(selected);
if (selected) {
background.setBackgroundResource(R.drawable.language_picker_item_bg_selected);
indicator.setVisibility(View.VISIBLE);
} else {
background.setBackgroundResource(0);
indicator.setVisibility(View.GONE);
}
}
private void setHeaderText(
TextView textView, int languageStringResourceId, int regionStringResourceId) {
if (mCountryMode) {
@@ -114,7 +171,7 @@ public class BilingualSuggestedLocaleAdapter extends SuggestedLocaleAdapter {
textNative.setTextLocale(localeInfo.getLocale());
textNative.setContentDescription(localeInfo.getContentDescription(mCountryMode));
TextView textSecondary = (TextView) itemView.findViewById(R.id.locale_secondary);
TextView textSecondary = itemView.findViewById(R.id.locale_secondary);
textSecondary.setText(localeInfo.getLocale().getDisplayLanguage(mSecondaryLocale));
textSecondary.setTextDirection(mSecondaryLocaleTextDir);
if (mCountryMode) {

View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp" android:height="24dp"
android:viewportWidth="24" android:viewportHeight="24">
<path android:fillColor="@color/language_picker_item_selected_indicator"
android:pathData="M9.55,18l-5.7,-5.7 1.425,-1.425L9.55,15.15l9.175,-9.175L20.15,7.4z"/>
</vector>

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:bottom="-5dp"
android:right="-5dp"
android:top="-5dp">
<shape android:shape="rectangle" >
<solid android:color="@color/language_picker_item_selected_bg" />
<stroke
android:width="2dp"
android:color="@color/language_picker_item_selected_stroke" />
</shape>
</item>
</layer-list>

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:color="@color/language_picker_item_text_color_secondary_selected" />
<item android:color="@color/language_picker_item_text_color_secondary" />
</selector>

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:color="@color/language_picker_item_text_color_selected" />
<item android:color="@color/language_picker_item_text_color" />
</selector>

View File

@@ -1,20 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/language_item"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:paddingTop="12dp"
android:paddingBottom="12dp"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
>
<LinearLayout
android:id="@+id/frame"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:orientation="vertical">
android:orientation="vertical"
>
<TextView
android:id="@+id/locale_native"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/language_picker_item_text_color"
android:textColor="@drawable/language_picker_item_text_color_selector"
android:textSize="18sp"
android:textAppearance="?android:attr/textAppearanceListItem"
/>
@@ -23,9 +30,20 @@
android:id="@+id/locale_secondary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/language_picker_item_text_color_secondary"
android:textColor="@drawable/language_picker_item_text_color2_selector"
android:textSize="16sp"
android:textAppearance="?android:attr/textAppearanceListItem"
/>
</LinearLayout>
</LinearLayout>
<ImageView
android:id="@+id/indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:adjustViewBounds="true"
android:layout_marginEnd="10dp"
android:layout_centerVertical="true"
android:src="@drawable/bilingual_language_item_selection_indicator"
android:visibility="gone" />
</RelativeLayout>

View File

@@ -41,6 +41,11 @@
<!-- Lily Language Picker language item view colors -->
<color name="language_picker_item_text_color">#F1F3F4</color>
<color name="language_picker_item_text_color_secondary">#BDC1C6</color>
<color name="language_picker_item_text_color_selected">#202124</color>
<color name="language_picker_item_text_color_secondary_selected">#202124</color>
<color name="language_picker_item_selected_indicator">#202124</color>
<color name="language_picker_item_selected_bg">#92B3F2</color>
<color name="language_picker_item_selected_stroke">#185ABC</color>
<!-- Color for side fps toast dark theme-->
<color name="side_fps_toast_background">#2E3132</color>

View File

@@ -454,6 +454,11 @@
<!-- Lily Language Picker language item view colors -->
<color name="language_picker_item_text_color">#202124</color>
<color name="language_picker_item_text_color_secondary">#5F6368</color>
<color name="language_picker_item_text_color_selected">#202124</color>
<color name="language_picker_item_text_color_secondary_selected">#5F6368</color>
<color name="language_picker_item_selected_indicator">#4285F4</color>
<color name="language_picker_item_selected_bg">#E8F0FE</color>
<color name="language_picker_item_selected_stroke">#4C8DF6</color>
<!-- Color for side fps toast light theme -->
<color name="side_fps_toast_background">#F7F9FA</color>

View File

@@ -97,10 +97,12 @@
<java-symbol type="id" name="icon" />
<java-symbol type="id" name="image" />
<java-symbol type="id" name="increment" />
<java-symbol type="id" name="indicator" />
<java-symbol type="id" name="internalEmpty" />
<java-symbol type="id" name="inputExtractAccessories" />
<java-symbol type="id" name="inputExtractAction" />
<java-symbol type="id" name="issued_on" />
<java-symbol type="id" name="language_item" />
<java-symbol type="id" name="left_icon" />
<java-symbol type="id" name="leftSpacer" />
<java-symbol type="id" name="line1" />
@@ -3122,6 +3124,12 @@
<java-symbol type="id" name="locale_search_menu" />
<java-symbol type="layout" name="language_picker_item" />
<java-symbol type="layout" name="language_picker_bilingual_item" />
<java-symbol type="color" name="language_picker_item_text_color" />
<java-symbol type="color" name="language_picker_item_text_color_selected" />
<java-symbol type="color" name="language_picker_item_text_color_secondary_selected" />
<java-symbol type="drawable" name="language_picker_item_text_color_selector" />
<java-symbol type="drawable" name="language_picker_item_text_color2_selector" />
<java-symbol type="drawable" name="language_picker_item_bg_selected" />
<java-symbol type="layout" name="language_picker_section_header" />
<java-symbol type="layout" name="language_picker_bilingual_section_header" />
<java-symbol type="menu" name="language_selection_list" />