How to add floating label on Spinner

前端 未结 10 1722
Happy的楠姐
Happy的楠姐 2020-12-12 14:46

After using the Android Design Support Library\'s TextInputLayout to place a floating label above an EditText component, I was wondering if there is a way to add a floating

相关标签:
10条回答
  • 2020-12-12 15:26

    Here is a library that I use for the Floating label spinner rey5137 Material Library

    Also, for future reference, here is a list of some great libraries. UI Libraries Core Libraries

    0 讨论(0)
  • 2020-12-12 15:31

    I modified Rodrigo's solution to use an adapter, i.e. more like a standard Spinner https://gist.github.com/smithaaron/d2acd57937d7a4201a79

    0 讨论(0)
  • 2020-12-12 15:33

    Here is my trick,

    the good things is that everything will work like you want,

    but the bad is that it is increasing layout hierarchy, and you have to handle functionality in code, and it is an ugly solution:

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <android.support.design.widget.TextInputLayout
                android:id="@+id/til"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
                <EditText
                    android:id="@+id/edt"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/edt_height"
                    android:hint="@string/create_gcc_visa_txt_step" />
    
            </android.support.design.widget.TextInputLayout>
    
            <Spinner
                android:id="@+id/spn"
                style="@style/MyAppTheme.Base.Spinner"
                android:layout_height="@dimen/edt_height"
                android:layout_alignBottom="@id/til" />
    
        </RelativeLayout>
    

    and override adapter for spinner, to make selected values transparent

    public class MySpinnerAdapter extends SimpleAdapter {
        Context mContext;
    
        public MySpinnerAdapter(Context context, List<String> data, int resource, String[] from, int[] to) {
            super(context, data, resource, from, to);
            mContext = context;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = super.getView(position, convertView, parent);
    
            TextView tv = (TextView) convertView.findViewById(android.R.id.text1);
                tv.setTextColor(ContextCompat.getColor(mContext, R.color.transparent));
            return convertView;
        }
    }
    

    and after selecting in spinner, just get selected text and set it to EditText and it will have the same effect with animation

    yourSpinnerView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<String> adapterView, View view, int i, long l) {
                //get your selected text from adapter or from where you want 
                String selectedText = adapterView.getItemAtPosition(i));
    
                if (i != 0) { 
                    edt.setText(selectedText);
                } else { 
                    // if in case your spinner have first empty text, 
                    // then when spinner selected, just empty EditText.
                    edt.setText("");
                }
            }
    
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
    
            }
        });
    

    if you have any question Ask me

    0 讨论(0)
  • 2020-12-12 15:34

    You can achieve this :

    With new Material library styles like this:

    <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/fullNameLay"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
    
        <androidx.appcompat.widget.AppCompatAutoCompleteTextView
                android:id="@+id/fullNameEt"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
    </com.google.android.material.textfield.TextInputLayout>
    

    for more information: https://material.io/develop/android/components/menu/

    0 讨论(0)
提交回复
热议问题