Hint on spinner on android spinner with Kotlin - android

I'm trying to put a spinner on a layout for android app. Unlike other components, the spinner does not come with an option to give a hint. How can I do it?
This is my spinner:
<Spinner
android:id="#+id/spinner_gender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/gender"
android:background="#android:color/white" />
When it's not selected I want to show a text like "Gender". When we select it i want that we can't select the option gender and that we only select Male or Female.
The options inside spinner are in an xml called where I keep the arrays.
Thanks.

Unfortunately Spinner don't have this feature built-in. Please check this post

This feature can be easily implemented through the library with so much customization:
https://github.com/Mobile-Innowise-Group/HintedSpinner
Usage of the library and with a lot of customized params:
<com.innowisegroup.hintedspinner.HintedSpinner
android:id="#+id/hintedSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="20dp"
app:hintTextSize="24sp"
app:hintTextColor="#color/red"
app:hint="Custom hint"
app:withDivider="true"
app:dividerColor="#color/dark_green"
app:arrowDrawable="#drawable/example_arrow_4"
app:arrowTint="#color/colorAccent"
app:popupBackground="#color/light_blue"
app:items="#array/text" />

Related

How to show spinner with limited number of item in drop down?

I want to use spinner in my Activity in android application.
My requirement is, I want to show only 6 items in drop down list at one time if their are more that 6 items then scroll bar should be visible. How to achieve this requirement.
Below is the sample code I am trying for UI in android activity.xml.
<Spinner
android:id="#+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:spinnerMode="dropdown"
android:dropDownVerticalOffset="40dp"
android:forceHasOverlappingRendering="false" />
you can visit this link for more details
How to limit the height of Spinner drop down view in Android
You can use this awesome library MaterialSpinner that will do all the hard work for you.
Download: implementation 'com.jaredrummler:material-spinner:1.3.1'
inside your xml file.
<com.jaredrummler.materialspinner.MaterialSpinner
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:ms_dropdown_max_height="400dp"/>
set height using app:ms_dropdown_max_height="400dp"
Add to your xml spinner element:
android:dropDownHeight="100dp"

Changing a TextView's style and text dynamically

I'm trying to make an app like the one in this mockup:
Supermarket
It's a very simple supermarket app. As you can see, there's a TextView at the bottom of the screen that tells me whether my Cart is empty, or has items in it. If the cart is not empty, the user is shown the total price he/she must pay. You can also notice that said TextView's style and text change according to a variable (in this case, "totalPrice").
How can I do this in Android? I know I can use simple if statements (if totalPrice == 0, then backgroundColor = grey and text = "EmptyCart", for example), but this seems somewhat... hardcoded. Is there a better way to do it? As you can see, the TextView's style and values also change when on the "Subproducts" activity (some Products have Subproducts, which you can see after clicking on them).
Thanks in advance!
I think Databinding is the best way rather than boilerplate code.
create a model class and change background of the view using ternary operation in databinding also manage visibility of price text using this.
To set a textview's text, you use textView.setText("new text"); To set its background, its textView.setBackgroundColor(color) where the color is the hexcode you want as an integer- for example 0xFFFF0000 for reg. Obviously these can be variables as well as hard coded.
It can simply be achieved with two TextViews in a RelativeLayout, and of course, by using basic TextView's methods. As an example layout:
<RelativeLayout
android:layout_height="75dp"
android:layout_width="match_parent"
android:background="#1EC4F3">
<TextView
android:text="PAY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:typeface="sans"
android:textColor="#FFFFFF"
android:textSize="18sp"/>
<TextView
android:layout_height="wrap_content"
android:text="$25.79"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="8dp"
android:typeface="monospace"
android:textSize="14sp"
android:textColor="#FFFFFF"/>
</RelativeLayout>

How to bring spinner arrow closer to selected text?

I know this question is repeated but I don't get the Proper solutions.
I want to bring arrow closer to the Spinner (That means i want to decrease the width of spinner and decrease the space between text and down arrow) I have used default spinner. How can I do that? Please, guide me to get Solution.
Here is the XML code
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:gravity="center"
android:textAlignment="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/relativeLayout15">
<Spinner
android:id="#+id/languageDropdown"
style="#style/SpinnerTheme"
android:layout_width="66dp"
android:layout_height="wrap_content"
android:entries="#array/Languages" />
</RelativeLayout>
As i am understanding your question, many time we face requirement to customise spinner UI and it is too tough to achieve this. I generally use this solution for such situations:
Solution: Hide your actual Spinner and show a TextView which just looks like our desired Spinner, Please have a look at below image for better understanding:
In above image I have created a phone number field where I am getting country code from user using Spinner. I created a TextView having text as country code with drawable[right] of down arrow. Please check below XML code for better understanding:
In above image Spinner width is 0dp so your Spinner will not be visible. Just remember one thin you have to handle click event of TextView and call performClick() method for Spinner. Please refer below image for this code:
On the click event of your TextView, just follow below way:
textViewLabelCodeRegister.id -> {
spinnerCountriesRegister.performClick()
}
That's It.
Make PopupWindow instead of Spinner.

SearchView by EditText android

I want to implement searchView like this link and link2 but I want to make a search from own EditText not in ActioBar or in menu.
I want to know what is the code in the main layout xml file. consider that I'm not want to use ActionBar with menu item because it isn't supported for api less that level 11, I want to use the search and get result in the same Activity, all what I want to know how can I get the search bar when press a button to give it a word t search in sqlite and get the result in the same Activity?
Hope anyone got my mean.
Thanks in advance.
Do not use a EditText, it is much easier to use a SearchView. If you don't want to have search interface in the action bar, then you can use a search widget, which is an instance of SearchView.
The basics are here : http://developer.android.com/guide/topics/search/search-dialog.html
If you really do not want the ActionBar and have to support older API levels than API level 11 (Honeycomb) and do not want to use a search dialog, you have to do everything on your own.
You could use a AutoCompleteTextView to display search suggestions and start an activity / fragment to display a list of results with the query entered whenever the user triggers your search. That's not different from any other Activity that has to react to user input.
But I strongly suggest to use ActionBarCompat and the support library to provide an ActionBar on older devices and use the SearchView within the ActionBar. That's what users expect nowadays anyway and it makes development that much easier.
If you want to create a search box using EditText (not in Action Bar), you may refer to the following XML code:
<RelativeLayout
android:id="#+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal" >
<EditText
android:id="#+id/inputSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:hint="Search...."
android:inputType="textCapCharacters" />
<ImageButton
android:id="#+id/searchButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_menu_search" />
</RelativeLayout>
But, I would suggest use ActionBar for the search box, it would look neat and easier as well. For search box using ActionBar, you may refer the following link:
http://javapapers.com/android/android-searchview-action-bar-tutorial/

Custom spinner like android 4.0

I try to make custom spinner looks like in android 4.0, but I dont know how to replase sprinner dropdown selection list. Any help is welcome.
I get resource files from android 4.0 sdk, but dopdown selection list does not change.
<Spinner
android:id="#+id/update_frequency_spinner"
android:layout_width="75dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:spinnerMode="dropdown"
android:background="#drawable/spinner_background_holo_light"
android:dropDownSelector="#drawable/list_selector_holo_light"
android:popupBackground="#drawable/menu_dropdown_panel_holo_light">
</Spinner>
This How to customize style from spinner dropdown CHOICES? may answer your question.
You could also look into using HoloEverywhere if you want a holo-themed app for pre holo devices https://github.com/ChristopheVersieux/HoloEverywhere

Categories

Resources