Skip to content

Commit 3a64dcd

Browse files
authored
Merge pull request #7 from DonMat/develop
Update lib to 1.1.0
2 parents e46532d + 37495c9 commit 3a64dcd

24 files changed

+319
-92
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@
88
/build
99
/captures
1010
.externalNativeBuild
11+
/.idea/markdown.xml
12+
/.idea/shelf/

.idea/compiler.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/gradle.xml

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/jarRepositories.xml

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 17 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/runConfigurations.xml

Lines changed: 0 additions & 12 deletions
This file was deleted.

README.md

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,78 @@ Step 2. Add the dependency
2929

3030
# Usage
3131
```xml
32-
<pl.utkala.searchablespinner.SearchableSpinner
33-
android:layout_width="wrap_content"
34-
android:layout_height="wrap_content"
3532

36-
app:closeText="Zamknij"
37-
app:dialogTitle="Wybierz z listy"/>
33+
<pl.utkala.searchablespinner.SearchableSpinner android:layout_width="wrap_content" android:layout_height="wrap_content"
3834

35+
app:closeText="Zamknij" app:dialogTitle="Wybierz z listy" />
36+
37+
```
38+
39+
```java
40+
searchableSpinner.setDialogTitle("Wybierz z listy");
41+
searchableSpinner.setDismissText("Zamknij");
42+
```
43+
44+
## Set hint for SearchableSpinner
45+
46+
To set hint on spinner you can use prepared **StringHintArrayAdapter** or create your own spinner adapter to handle hint for custom object.
47+
48+
Hint value can be passed directly to _StringHintArrayAdapter_ after set **showHint** to true
49+
50+
```xml
51+
app:showHint="true"
52+
```
53+
54+
or
55+
56+
```java
57+
searchableSpinner.showHint=true
58+
searchableSpinner.adapter=StringHintArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,users,"Select Item")
59+
```
60+
61+
## Set custom OnSearchableItem listener
62+
63+
You can set your own listener when user select filtered result from dialog.
64+
65+
```java
66+
searchableSpinner.onSearchableItemClick=object:OnSearchableItemClick<Any?>{
67+
override fun onSearchableItemClicked(item:Any?,position:Int){
68+
if(position>0){
69+
searchableSpinner.setSelection(position)
70+
}else{
71+
searchableSpinner.setSelection(Spinner.INVALID_POSITION)
72+
}
73+
}
74+
}
3975
```
4076

77+
## Change dialog background color
78+
79+
You can set your own dialog background passing _Drawable_ to **setDialogBackground**
80+
4181
```java
42-
searchableSpinner.setDialogTitle("Wybierz z listy");
43-
searchableSpinner.setDismissText("Zamknij");
82+
searchableSpinner.setDialogBackground(ColorDrawable(Color.RED))
83+
```
84+
85+
## Set custom dialog adapter
86+
87+
You can set your own list adapter. It can be used to add custom filters or custom list item views.
88+
89+
```java
90+
searchableSpinner.setCustomDialogAdapter(T:ArrayList<*>)
4491
```
4592

4693
# Changelog
47-
* **1.0.1**
48-
* Fix lost state after screen rotate
49-
* **1.0.0**
50-
* Initial Release
94+
95+
* **1.1.0**
96+
* Add hint for spinner
97+
* Add OnSearchableItem setter
98+
* Add custom dialog background setter
99+
* Add custom dialog adapter setter
100+
* **1.0.1**
101+
* Fix lost state after screen rotate
102+
* **1.0.0**
103+
* Initial Release
51104

52105
# License
53106

app/build.gradle

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ apply plugin: 'kotlin-android'
55
apply plugin: 'kotlin-android-extensions'
66

77
android {
8-
compileSdkVersion 27
8+
compileSdkVersion 33
99
defaultConfig {
1010
applicationId "pl.utkala.searchablespinnerdemo"
1111
minSdkVersion 21
12-
targetSdkVersion 27
12+
targetSdkVersion 33
1313
versionCode 1
1414
versionName "1.0"
1515
}
@@ -22,13 +22,13 @@ android {
2222
lintOptions {
2323
abortOnError false
2424
}
25+
namespace 'pl.utkala.searchablespinnerdemo'
2526
}
2627

2728
dependencies {
2829
implementation fileTree(dir: 'libs', include: ['*.jar'])
29-
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
30-
implementation 'com.android.support:appcompat-v7:27.1.1'
31-
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
30+
implementation 'androidx.appcompat:appcompat:1.5.1'
31+
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
3232

3333
implementation project(':searchablespinner')
3434

app/src/main/AndroidManifest.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
package="pl.utkala.searchablespinnerdemo">
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
43

54
<application
65
android:allowBackup="true"
@@ -9,7 +8,9 @@
98
android:roundIcon="@mipmap/ic_launcher_round"
109
android:supportsRtl="true"
1110
android:theme="@style/AppTheme">
12-
<activity android:name=".MainActivity">
11+
<activity
12+
android:name=".MainActivity"
13+
android:exported="true">
1314
<intent-filter>
1415
<action android:name="android.intent.action.MAIN" />
1516

app/src/main/java/pl/utkala/searchablespinnerdemo/MainActivity.kt

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,31 @@
1717
package pl.utkala.searchablespinnerdemo
1818

1919
import android.os.Bundle
20-
import android.support.v7.app.AppCompatActivity
21-
import android.widget.ArrayAdapter
20+
import android.widget.Spinner
21+
import androidx.appcompat.app.AppCompatActivity
2222
import kotlinx.android.synthetic.main.activity_main.*
23+
import pl.utkala.searchablespinner.OnSearchableItemClick
24+
import pl.utkala.searchablespinner.StringHintArrayAdapter
2325

2426
class MainActivity : AppCompatActivity() {
2527

2628
override fun onCreate(savedInstanceState: Bundle?) {
2729
super.onCreate(savedInstanceState)
2830
setContentView(R.layout.activity_main)
2931

30-
val users = listOf("Veniam penatibus", "reprehenderit", "fusce", "Ullamcorper lacinia", "Etiam dis")
31-
searchableSpinner.adapter = ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, users)
32+
val users = listOf("John Doe", "Ellen Cunningham", "Carmen Walker", "Mike Walker", "Edgar Bourn", "Richard Robson", "Ralph Poe", "Max Smith")
33+
searchableSpinner.showHint = true
34+
searchableSpinner.adapter = StringHintArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, users, "Select Item")
35+
searchableSpinner.onSearchableItemClick = object : OnSearchableItemClick<Any?> {
36+
override fun onSearchableItemClicked(item: Any?, position: Int) {
37+
if (position > 0) {
38+
searchableSpinner.setSelection(position)
39+
} else {
40+
searchableSpinner.setSelection(Spinner.INVALID_POSITION)
41+
}
42+
}
43+
}
44+
45+
searchableSpinner.setCustomDialogAdapter(StartWithArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, users))
3246
}
3347
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package pl.utkala.searchablespinnerdemo
2+
3+
import android.content.Context
4+
import android.widget.ArrayAdapter
5+
import android.widget.Filter
6+
7+
class StartWithArrayAdapter(context: Context, resourceId: Int, private val items: List<String>) : ArrayAdapter<String>(context, resourceId, items) {
8+
private var mData = items
9+
10+
override fun getCount(): Int {
11+
return mData.size
12+
}
13+
14+
override fun getItem(position: Int): String {
15+
return mData[position]
16+
}
17+
18+
override fun getFilter(): Filter {
19+
return object : Filter() {
20+
override fun performFiltering(constraint: CharSequence?): FilterResults {
21+
val result = FilterResults()
22+
if (!constraint.isNullOrBlank()) {
23+
synchronized(this) {
24+
val filteredItems = ArrayList<String>()
25+
for (i in (items.indices)) {
26+
if (items[i].startsWith(constraint, ignoreCase = true))
27+
filteredItems.add(items[i])
28+
}
29+
result.count = filteredItems.size
30+
result.values = filteredItems
31+
}
32+
} else {
33+
synchronized(this) {
34+
result.values = items
35+
result.count = items.size
36+
}
37+
}
38+
return result
39+
}
40+
41+
override fun publishResults(constraint: CharSequence?, results: FilterResults?) {
42+
if (results?.values != null) {
43+
mData = results.values as List<String>
44+
notifyDataSetChanged()
45+
}
46+
}
47+
}
48+
}
49+
}

app/src/main/res/layout/activity_main.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
android:layout_margin="16dp"
2323
android:layout_gravity="center_horizontal"
2424

25+
app:showHint="true"
2526
app:closeText="@string/search_dialog_close"
2627
app:dialogTitle="@string/search_dialog_title" />
2728

0 commit comments

Comments
 (0)