Skip to content

Commit

Permalink
[ISSUE #345] - Added Hide option for the Search Bar (#347)
Browse files Browse the repository at this point in the history
* [ISSUE #345] - Added Show/Hide option for the Search Bar

* lint issue
  • Loading branch information
ferranpons authored Apr 21, 2022
1 parent e9ecfb3 commit ea6d241
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ val locationPickerIntent = LocationPickerActivity.Builder()
.withGoogleTimeZoneEnabled()
.withVoiceSearchHidden()
.withUnnamedRoadHidden()
.withSearchBarHidden()
.build(applicationContext)

startActivityForResult(locationPickerIntent, MAP_BUTTON_REQUEST_CODE)
Expand Down Expand Up @@ -409,6 +410,14 @@ Now you can hide or show the text returned by the google service with "Unnamed R
intent.putExtra(LocationPickerActivity.UNNAMED_ROAD_VISIBILITY, false)
```

##### Hide/Show the Search Bar

Now you can hide or show the search bar that helps you to search for locations

```kotlin
intent.putExtra(LocationPickerActivity.SEARCH_BAR_HIDDEN, false)
```

#### Tracking

Optionally, you can set a tracking events listener. Implement LocationPickerTracker interface, and set it in your Application class as follows:
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/adevinta/mappicker/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class MainActivity : AppCompatActivity() {
.withGoogleTimeZoneEnabled()
// .withVoiceSearchHidden()
.withUnnamedRoadHidden()
// .withSearchBarHidden()
.build(applicationContext)

// this is optional if you want to return RESULT_OK if you don't set the
Expand Down
47 changes: 34 additions & 13 deletions leku/src/main/java/com/adevinta/leku/LocationPickerActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import android.util.TypedValue
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.View.GONE
import android.view.WindowManager
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
Expand Down Expand Up @@ -100,6 +101,7 @@ const val TIME_ZONE_DISPLAY_NAME = "time_zone_display_name"
const val MAP_STYLE = "map_style"
const val UNNAMED_ROAD_VISIBILITY = "unnamed_road_visibility"
const val WITH_LEGACY_LAYOUT = "with_legacy_layout"
const val SEARCH_BAR_HIDDEN = "search_view_hidden"
private const val GEOLOC_API_KEY = "geoloc_api_key"
private const val PLACES_API_KEY = "places_api_key"
private const val LOCATION_KEY = "location_key"
Expand Down Expand Up @@ -180,6 +182,8 @@ class LocationPickerActivity :
private var mapStyle: Int? = null
private var isLegacyLayoutEnabled = false
private var isSearchLayoutShown = false
private var isSearchBarHidden = false

private lateinit var toolbar: MaterialToolbar
private lateinit var timeZone: TimeZone

Expand Down Expand Up @@ -385,20 +389,24 @@ class LocationPickerActivity :

private fun setUpSearchView() {
searchView = findViewById(R.id.leku_search)
searchView?.setOnEditorActionListener { v, actionId, _ ->
var handled = false
if (actionId == EditorInfo.IME_ACTION_SEARCH && v.text.toString().isNotEmpty()) {
retrieveLocationFrom(v.text.toString())
closeKeyboard()
handled = true
if (isSearchBarHidden) {
searchEditLayout?.visibility = GONE
} else {
searchView?.setOnEditorActionListener { v, actionId, _ ->
var handled = false
if (actionId == EditorInfo.IME_ACTION_SEARCH && v.text.toString().isNotEmpty()) {
retrieveLocationFrom(v.text.toString())
closeKeyboard()
handled = true
}
handled
}
handled
}
createSearchTextChangeObserver()
if (!isLegacyLayoutEnabled) {
searchView?.setOnFocusChangeListener { _: View?, hasFocus: Boolean ->
if (hasFocus) {
showSearchLayout()
createSearchTextChangeObserver()
if (!isLegacyLayoutEnabled) {
searchView?.setOnFocusChangeListener { _: View?, hasFocus: Boolean ->
if (hasFocus) {
showSearchLayout()
}
}
}
}
Expand Down Expand Up @@ -933,6 +941,9 @@ class LocationPickerActivity :
if (savedInstanceState.keySet().contains(WITH_LEGACY_LAYOUT)) {
isLegacyLayoutEnabled = savedInstanceState.getBoolean(WITH_LEGACY_LAYOUT, false)
}
if (savedInstanceState.keySet().contains(SEARCH_BAR_HIDDEN)) {
isSearchBarHidden = savedInstanceState.getBoolean(SEARCH_BAR_HIDDEN, false)
}
}

private fun getTransitionBundleParams(transitionBundle: Bundle) {
Expand Down Expand Up @@ -986,6 +997,9 @@ class LocationPickerActivity :
if (transitionBundle.keySet().contains(WITH_LEGACY_LAYOUT)) {
isLegacyLayoutEnabled = transitionBundle.getBoolean(WITH_LEGACY_LAYOUT, false)
}
if (transitionBundle.keySet().contains(SEARCH_BAR_HIDDEN)) {
isSearchBarHidden = transitionBundle.getBoolean(SEARCH_BAR_HIDDEN, false)
}
}

private fun setLayoutVisibilityFromBundle(transitionBundle: Bundle) {
Expand Down Expand Up @@ -1460,6 +1474,7 @@ class LocationPickerActivity :
private var mapStyle: Int? = null
private var unnamedRoadVisible = true
private var isLegacyLayoutEnabled = false
private var isSearchBarHidden = false

fun withLocation(latitude: Double, longitude: Double): Builder {
this.locationLatitude = latitude
Expand Down Expand Up @@ -1555,6 +1570,11 @@ class LocationPickerActivity :
return this
}

fun withSearchBarHidden(): Builder {
this.isSearchBarHidden = true
return this
}

fun build(context: Context): Intent {
val intent = Intent(context, LocationPickerActivity::class.java)

Expand Down Expand Up @@ -1592,6 +1612,7 @@ class LocationPickerActivity :
intent.putExtra(ENABLE_VOICE_SEARCH, voiceSearchEnabled)
intent.putExtra(UNNAMED_ROAD_VISIBILITY, unnamedRoadVisible)
intent.putExtra(WITH_LEGACY_LAYOUT, isLegacyLayoutEnabled)
intent.putExtra(SEARCH_BAR_HIDDEN, isSearchBarHidden)

return intent
}
Expand Down

0 comments on commit ea6d241

Please sign in to comment.