codinginflow / CountryAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
xml version = » 1.0 » encoding = » utf-8 » ?> |
RelativeLayout xmlns : android = » http://schemas.android.com/apk/res/android « |
xmlns : app = » http://schemas.android.com/apk/res-auto « |
xmlns : tools = » http://schemas.android.com/tools « |
android : layout_width = » match_parent « |
android : layout_height = » match_parent « |
tools : context = » com.codinginflow.customspinnerexample.MainActivity » > |
Spinner |
android : id = » @+id/spinner_countries « |
android : layout_width = » wrap_content « |
android : layout_height = » wrap_content « |
android : layout_alignParentTop = » true « |
android : layout_centerHorizontal = » true « |
android : layout_marginTop = » 18dp »/> |
RelativeLayout > |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
xml version = » 1.0 » encoding = » utf-8 » ?> |
RelativeLayout xmlns : android = » http://schemas.android.com/apk/res/android « |
android : layout_width = » match_parent « |
android : layout_height = » match_parent » > |
ImageView |
android : id = » @+id/image_view_flag « |
android : layout_width = » 150dp « |
android : layout_height = » 100dp « |
android : src = » @drawable/india »/> |
TextView |
android : id = » @+id/text_view_name « |
android : layout_width = » wrap_content « |
android : layout_height = » wrap_content « |
android : layout_alignBottom = » @+id/image_view_flag « |
android : layout_alignParentTop = » true « |
android : layout_margin = » 16dp « |
android : layout_toEndOf = » @+id/image_view_flag « |
android : gravity = » center « |
android : text = » India « |
android : textColor = » @android:color/black « |
android : textSize = » 30sp »/> |
RelativeLayout > |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
package com.codinginflow.customspinnerexample ; |
import android.content.Context ; |
import android.support.annotation.NonNull ; |
import android.support.annotation.Nullable ; |
import android.view.LayoutInflater ; |
import android.view.View ; |
import android.view.ViewGroup ; |
import android.widget.ArrayAdapter ; |
import android.widget.ImageView ; |
import android.widget.TextView ; |
import java.util.ArrayList ; |
public class CountryAdapter extends ArrayAdapter CountryItem > < |
public CountryAdapter ( Context context , ArrayList CountryItem > countryList ) < |
super (context, 0 , countryList); |
> |
@NonNull |
@Override |
public View getView ( int position , @Nullable View convertView , @NonNull ViewGroup parent ) < |
return initView(position, convertView, parent); |
> |
@Override |
public View getDropDownView ( int position , @Nullable View convertView , @NonNull ViewGroup parent ) < |
return initView(position, convertView, parent); |
> |
private View initView ( int position , View convertView , ViewGroup parent ) < |
if (convertView == null ) < |
convertView = LayoutInflater . from(getContext()) . inflate( |
R . layout . country_spinner_row, parent, false |
); |
> |
ImageView imageViewFlag = convertView . findViewById( R . id . image_view_flag); |
TextView textViewName = convertView . findViewById( R . id . text_view_name); |
CountryItem currentItem = getItem(position); |
if (currentItem != null ) < |
imageViewFlag . setImageResource(currentItem . getFlagImage()); |
textViewName . setText(currentItem . getCountryName()); |
> |
return convertView; |
> |
> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
package com.codinginflow.customspinnerexample ; |
public class CountryItem < |
private String mCountryName; |
private int mFlagImage; |
public CountryItem ( String countryName , int flagImage ) < |
mCountryName = countryName; |
mFlagImage = flagImage; |
> |
public String getCountryName () < |
return mCountryName; |
> |
public int getFlagImage () < |
return mFlagImage; |
> |
> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
package com.codinginflow.customspinnerexample ; |
import android.support.v7.app.AppCompatActivity ; |
import android.os.Bundle ; |
import android.view.View ; |
import android.widget.AdapterView ; |
import android.widget.Spinner ; |
import android.widget.Toast ; |
import java.util.ArrayList ; |
public class MainActivity extends AppCompatActivity < |
private ArrayList CountryItem > mCountryList; |
private CountryAdapter mAdapter; |
@Override |
protected void onCreate ( Bundle savedInstanceState ) < |
super . onCreate(savedInstanceState); |
setContentView( R . layout . activity_main); |
initList(); |
Spinner spinnerCountries = findViewById( R . id . spinner_countries); |
mAdapter = new CountryAdapter ( this , mCountryList); |
spinnerCountries . setAdapter(mAdapter); |
spinnerCountries . setOnItemSelectedListener( new AdapterView . OnItemSelectedListener () < |
@Override |
public void onItemSelected ( AdapterView parent , View view , int position , long id ) < |
CountryItem clickedItem = ( CountryItem ) parent . getItemAtPosition(position); |
String clickedCountryName = clickedItem . getCountryName(); |
Toast . makeText( MainActivity . this , clickedCountryName + » selected » , Toast . LENGTH_SHORT ) . show(); |
> |
@Override |
public void onNothingSelected ( AdapterView parent ) < |
> |
>); |
> |
private void initList () < |
mCountryList = new ArrayList<> (); |
mCountryList . add( new CountryItem ( » India » , R . drawable . india)); |
mCountryList . add( new CountryItem ( » China » , R . drawable . china)); |
mCountryList . add( new CountryItem ( » USA » , R . drawable . usa)); |
mCountryList . add( new CountryItem ( » Germany » , R . drawable . germany)); |
> |
> |
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Источник
Custom spinner java android github
The best Android spinner library for your android application with more customization
Available on Play Store
Dropdown Mode | Dialog Mode | Searchable Mode |
---|---|---|
- Dropdown mode ( android:spinnerMode=»dropdown» ). By default it is dropdown mode
- Dialog mode ( android:spinnerMode=»dialog» )
- Searchable mode ( app:smsp_isSearchable=»true» )
- smsp_isSearchable : Default is false. Set it to true to enable this feature.
- smsp_enableSearchHeader : Default is true. Set it to false to hide search header.
- smsp_searchHeaderText : Text display as header text
- smsp_searchHeaderTextColor : For changing header text color
- smsp_searchHeaderBackgroundColor : For changing header background color
- smsp_searchHint : To set query text hint
- smsp_searchHintColor : To set hint color to query search text
- smsp_searchTextColor : To set color to query search text
- smsp_searchBackgroundColor : To set background color to searchview
- smsp_itemListBackgroundColor : To set background color to search item list
- smsp_enableDismissSearch : To enable/disable dismiss button on search dialog
- smsp_dismissSearchText : To set dismiss button text for search dialog
- smsp_dismissSearchColor : To set dismiss button text color for search dialog
- smsp_searchDropdownView : To custom search view item
- smsp_itemSize : To set spinner item size
- smsp_itemColor : To set spinner item color
- smsp_hint : To set spinner hint text
- smsp_hintSize : To set spinner hint text size
- smsp_hintColor : To set spinner hint color
- smsp_itemListHintColor : To set spinner hint text color (dropdown hint)
- smsp_itemListHintBackgroundColor : To set spinner hint background (dropdown hint)
- smsp_enableFloatingLabel : Default is true. Set it to false to disable floating label.
- smsp_alwaysShowFloatingLabel : Default is false. Set it to true for always display.
- smsp_floatingLabelText : Update it to what text you want. If the value is not set, it get from hint.
- smsp_floatingLabelSize : For changing floating label size
- smsp_floatingLabelColor : For changing floating label color
- smsp_enableErrorLabel : Default it is true.
- smsp_errorText : Your error message text.
- smsp_errorTextSize : For changing error text size
- smsp_errorTextColor : for changing error text color
- smsp_multilineError : Default is false. Update it to show as single line or multiple line
- smsp_errorTextAlignment : Align error text to left, center or right
Color customization to:
- Item text color ( smsp_itemColor )
- Hint color ( smsp_hintColor )
- Floating Label color ( smsp_floatingLabelColor )
- Error Text color ( smsp_errorTextColor )
- List Item Hint color and background ( smsp_itemListHintColor & smsp_itemListHintBackgroundColor )
- List Item color ( smsp_itemListColor )
- Selected item color ( smsp_selectedItemListColor )
- Arrow or selector color ( smsp_arrowColor )
- Underline color ( smsp_underlineColor )
- Dropdown list/Search ListView background ( smsp_itemListBackgroundColor )
- Search dialog dismiss button color ( smsp_dismissSearchColor )
Empty dropdown clickable
- In case if you want to design view with empty spinner and apply click event and here it is.
- smsp_underlineSize : To set underline size (The bottom line of spinner)
- smsp_underlineColor : To set underline color (The bottom line of spinner)
- smsp_isReSelectable : Allow re-selectable for current selected item. By default it is false
RTL (Right To Left) feature
- smsp_isRtl : To change position of view from right to left or left to right. I will update more on it.
Typeface (Custom font)
- smsp_typeface : Apply custom font to whole spinner view. You can use this attr without specify font extension
- smsp_typeface_xxx : Set typeface to specific part of spinner
Add the dependencies to your gradle file:
Add SmartMaterialSpinner to your layout:
In Kotlin class
Issue and new feature request
Each raised issue, should be provided with some information to be easy for me to replicate it like Device Model , Android version and log message or file
Where to report issue and new feature request
You can create issue on Github to record issue history then to make it fast alert to me, you can send me email or post to Facebook group for any discussion
If you want to contribute in this library, please fork this repository and create pull request.
Источник