• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.pump.widget;
18 
19 import android.content.Context;
20 import android.content.res.Resources;
21 import android.text.Spannable;
22 import android.text.SpannableString;
23 import android.text.style.UnderlineSpan;
24 import android.util.AttributeSet;
25 import android.view.View;
26 import android.view.ViewGroup;
27 import android.widget.ArrayAdapter;
28 import android.widget.TextView;
29 
30 import androidx.annotation.AttrRes;
31 import androidx.annotation.NonNull;
32 import androidx.annotation.Nullable;
33 import androidx.annotation.UiThread;
34 import androidx.appcompat.widget.AppCompatSpinner;
35 
36 import com.android.pump.R;
37 
38 @UiThread
39 public class SortOrderSpinner extends AppCompatSpinner {
SortOrderSpinner(@onNull Context context)40     public SortOrderSpinner(@NonNull Context context) {
41         super(context);
42         initialize();
43     }
44 
SortOrderSpinner(@onNull Context context, int mode)45     public SortOrderSpinner(@NonNull Context context, int mode) {
46         super(context, mode);
47         initialize();
48     }
49 
SortOrderSpinner(@onNull Context context, @Nullable AttributeSet attrs)50     public SortOrderSpinner(@NonNull Context context, @Nullable AttributeSet attrs) {
51         super(context, attrs);
52         initialize();
53     }
54 
SortOrderSpinner(@onNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr)55     public SortOrderSpinner(@NonNull Context context, @Nullable AttributeSet attrs,
56             @AttrRes int defStyleAttr) {
57         super(context, attrs, defStyleAttr);
58         initialize();
59     }
60 
SortOrderSpinner(@onNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr, int mode)61     public SortOrderSpinner(@NonNull Context context, @Nullable AttributeSet attrs,
62             @AttrRes int defStyleAttr, int mode) {
63         super(context, attrs, defStyleAttr, mode);
64         initialize();
65     }
66 
SortOrderSpinner(@onNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr, int mode, @Nullable Resources.Theme popupTheme)67     public SortOrderSpinner(@NonNull Context context, @Nullable AttributeSet attrs,
68             @AttrRes int defStyleAttr, int mode, @Nullable Resources.Theme popupTheme) {
69         super(context, attrs, defStyleAttr, mode, popupTheme);
70         initialize();
71     }
72 
initialize()73     private void initialize() {
74         String[] options = {
75             "name",
76             "modified"
77         };
78         ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(
79                 getContext(), android.R.layout.simple_spinner_item, options) {
80             @Override
81             public @NonNull View getView(int position, @Nullable View convertView,
82                     @NonNull ViewGroup parent) {
83                 View view = super.getView(position, convertView, parent);
84                 TextView textView = view.findViewById(android.R.id.text1);
85 
86                 CharSequence text = textView.getText();
87                 SpannableString content = new SpannableString("Sort by " + text);
88                 content.setSpan(new UnderlineSpan(), content.length() - text.length(),
89                         content.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
90                 textView.setText(content);
91 
92                 return view;
93             }
94         };
95         adapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
96 
97         setAdapter(adapter);
98     }
99 }
100