Skip to content

Instantly share code, notes, and snippets.

@farbodsz
Last active August 3, 2017 16:15
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save farbodsz/a43891f5eef40b1c3141 to your computer and use it in GitHub Desktop.
Save farbodsz/a43891f5eef40b1c3141 to your computer and use it in GitHub Desktop.
A Spinner component with a floating label for Android, similar to EditText components wrapped in a TextInputLayout.

Moved to an Android library...

This Gist has been discontinued and will be updated on a GitHub project here. The files I have removed from this Gist can be found there. Please feel free to open issues, pull requests, etc, so that I can change and improve the library how you like.

If you are already using the Gist, delete the layout XML, values XML, and Java files (the ones I have removed from this Gist), and instead add the following in your app module's build.gradle dependencies:

compile 'com.satsuware.lib:usefulviews:+'

Make sure to change your imports as well in the files that you are using the LabelledSpinner component.

@paolorotolo
Copy link

Hey, awesome work!
I've found a typo.
Line 88:

        String labelText = a.getString(R.styleable.LabelledSpinner_labelText);

Should be:

        String labelText = typedArray.getString(R.styleable.LabelledSpinner_labelText);

Also, what about making an Android library?

@farbodsz
Copy link
Author

farbodsz commented Sep 1, 2015

@paolorotolo Thank you. I have corrected the typo on line 88 now.

I think making an Android library for this would be a good idea, and I probably will in the near future.

@paolorotolo
Copy link

Cool, let me know if you need help. I've already some libraries in maven Central :)

@scottyab
Copy link

Thanks for this, it works well to show the hint. What would also be great is to show error text like the TextInputLayout. I might need that for current project, if so I'll see about contributing it back.

@mcelotti
Copy link

This is a different solution to the same problem:
https://gist.github.com/smithaaron/d2acd57937d7a4201a79
With this you get also the error text (it extends AppCompatEditText)

@farbodsz
Copy link
Author

Apologies for the late reply, @scottyab - I have been working on making this an Android library.

Just to clarify, would you like the error text to be shown if the user makes an invalid selection (i.e. showing error text for specific selections)?

If so, in what way would you like it to be implemented? I have a suggestion below, but let me know if you have any other ideas:

int[] invalidSelectionIndices = {2, 4, 6, 7};
String errorText = "This is a sample error text";
labelledSpinner.setErrorSelection(invalidSelectionIndices, errorText);

@farbodsz
Copy link
Author

Moving to an Android library...

This Gist is now discontinued and will be updated on a GitHub project here (resolving @paolorotolo 's suggestion). Please feel free to open issues, pull requests, etc, so that I can change and improve the library how you like.

If you are already using the Gist, you do not need to make any major changes. Delete all of the files (above) that you have added (layout XML, values XML, and Java file), and instead add the following in your app module's build.gradle dependencies:

compile 'com.farbod.labelledspinner:labelledspinner:+'

Make sure to change your imports as well in the files that you are using the LabelledSpinner.

@farbodsz
Copy link
Author

@mcelotti - I have added the option for an error text for when the first item of the Spinner (e.g. a prompt, or hint) is selected, so the user knows it is an invalid selection.
See the changes in LabelledSpinner.java in the repo.

@paolorotolo - You mentioned that you would be willing to help on the project ;) . As you may (or may not) know, I have moved this to a repo. There are a few issues at the moment, so feel free to make a fork/pull request, or open an issue if you see an improvement/bug/etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment