title: Searchview focus to move from center to toolbar Android
body: <p>I want to build searchview in center of screen and when I focus I want to move into toolbar. Remove Searchview only from toolbar when I remove focus from SearchView. I don't won't in different activities/fragments. Something similar to this <a href="https://media.giphy.com/media/tWJYT1qAsT6AHVjom8/giphy.gif" rel="nofollow noreferrer">view</a>. I tried some piece of code but lack of knowledge so I didn't succeed. Can someone guide me</p>
  <pre><code>&lt;androidx.coordinatorlayout.widget.CoordinatorLayout 
      xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;&gt;
  
      &lt;com.google.android.material.appbar.AppBarLayout
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;wrap_content&quot;&gt;
  
          &lt;EditText
              android:layout_width=&quot;match_parent&quot;
              android:layout_height=&quot;wrap_content&quot;
              app:layout_scrollFlags=&quot;scroll&quot; /&gt;
      &lt;/com.google.android.material.appbar.AppBarLayout&gt;
  
      &lt;androidx.recyclerview.widget.RecyclerView
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;match_parent&quot; /&gt;
  &lt;/androidx.coordinatorlayout.widget.CoordinatorLayout&gt;
  </code></pre>
  <p><strong>UPDATE</strong></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      android:id=&quot;@+id/coordinatorLayout&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      android:fitsSystemWindows=&quot;true&quot;&gt;
  
      &lt;com.google.android.material.appbar.AppBarLayout
          android:id=&quot;@+id/appBar&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;100dp&quot;
          android:backgroundTint=&quot;@color/orange_lighter&quot;
          android:gravity=&quot;bottom&quot;
          android:theme=&quot;@style/ThemeOverlay.AppCompat.Dark.ActionBar&quot;&gt;
  
          &lt;androidx.appcompat.widget.SearchView
              android:id=&quot;@+id/consultationSearchView&quot;
              android:layout_width=&quot;match_parent&quot;
              android:layout_height=&quot;?attr/actionBarSize&quot;
              android:layout_marginStart=&quot;16dp&quot;
              android:layout_marginTop=&quot;16dp&quot;
              android:layout_marginEnd=&quot;16dp&quot;
             
              android:theme=&quot;@style/SearchViewTheme&quot;
              app:closeIcon=&quot;@drawable/ic_cancel&quot;
              app:layout_scrollFlags=&quot;scroll|exitUntilCollapsed|snap&quot;
              app:searchIcon=&quot;@drawable/ic_search&quot; /&gt;
  
      &lt;/com.google.android.material.appbar.AppBarLayout&gt;
  
      &lt;androidx.constraintlayout.widget.ConstraintLayout
          android:id=&quot;@+id/constraintLayout&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;match_parent&quot;
          app:layout_behavior=&quot;@string/appbar_scrolling_view_behavior&quot;&gt;
  
        // more views
  
      &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  
  &lt;/androidx.coordinatorlayout.widget.CoordinatorLayout&gt;
  </code></pre>
  <p>Activity code</p>
  <pre><code> package com.example.app.consultation
  
  import android.content.Context
  import android.graphics.Rect
  import android.os.Bundle
  import android.view.MotionEvent
  import android.view.View
  import android.view.inputmethod.InputMethodManager
  import androidx.appcompat.widget.SearchView
  import com.example.app.common.BaseActivity
  import com.example.app.databinding.ExploreConsultationsLayoutBinding
  import org.koin.androidx.viewmodel.ext.android.viewModel
  
  class ExploreConsultationsActivity : BaseActivity() {
  
  companion object {
  const val CONSULTATION_COVER_LIST_KEY = &quot;consultation_cover_list&quot;
  }
  
  private val binding by lazy { ExploreConsultationsLayoutBinding.inflate(layoutInflater) }
  val viewModel by viewModel&lt;ExploreConsultationViewModel&gt;()
  
  override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)
  setupViewModel()
  setContentView(binding.root)
  setupView()
  }
  
  fun setupViewModel() {
  viewModel.categoriesList = intent?.getParcelableArrayListExtra(CONSULTATION_COVER_LIST_KEY)
  }
  
  fun setupView() {
  hideActionBar()
  setupSearchView()
  }
  
  fun setupSearchView() {
  binding.consultationSearchView.apply {
  setOnQueryTextListener(object : SearchView.OnQueryTextListener {
  override fun onQueryTextSubmit(query: String?) = false
  override fun onQueryTextChange(newText: String?): Boolean {
  if (newText != null) {
  viewModel.removeSearchViewFocus.value = false
  viewModel.queryText = newText
  }
  return true
  }
  })
  setOnQueryTextFocusChangeListener { _, hasFocus -&gt;
  binding.appBar.setExpanded(!hasFocus)
  isSelected = hasFocus
  }
  }
  }
  
  
  override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
  if (ev?.action == MotionEvent.ACTION_DOWN) {
  val view: View? = currentFocus
  if (view is SearchView) {
  val outRect = Rect()
  view.getGlobalVisibleRect(outRect);
  if (!outRect.contains(ev.rawX.toInt(), ev.rawY.toInt())) {
  view.clearFocus()
  val inputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
  inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
  }
  }
  }
  return super.dispatchTouchEvent(ev)
  }
  }
  </code></pre>