개발/안드로이드 개발
SearchView iconifiedByDefault를 false로 했을 때 자동으로 키보드 올라오는 것 막기
룬아님
2020. 1. 21. 18:41
클릭을 해야 활성화 되는 SearchView가 아니라 바로 입력창이 노출되는 SearchView를 사용할 때에 iconifiedByDefault를 false로 해주면 icon 상태가 되지 않기 때문에 바로 입력을 할수가 있다.
<androidx.appcompat.widget.SearchView
android:id="@+id/search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/spacing_normal"
android:background="@drawable/searchview_bgr"
app:defaultQueryHint="@string/search"
app:iconifiedByDefault="false" />
하지만 특정 상황 또는 OS에서 Activity 진입 시에 SearchView에 자동으로 focus가 가게 되어 키보드가 자동으로 올라오는데
android:windowSoftInputMode="stateAlwaysHidden"
stateAlwaysHidden을 적용하였음에도 키보드가 자동으로 올라오는 것을 막을 수 없었다.
이를 해결하기 위해
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:focusableInTouchMode="true">
<androidx.appcompat.widget.SearchView
android:id="@+id/search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/spacing_normal"
android:background="@drawable/searchview_bgr"
app:defaultQueryHint="@string/search"
app:iconifiedByDefault="false" />
</FrameLayout>
SearchView를 감싸고 있는 뷰에 focusableInTouchMode를 true로 넣어 강제적으로 focus를 가져가도록 하였고 더 이상 키보드가 자동으로 올라오지 않았다.
반응형