I'm trying to implement a SearchView inside of a layout, not in a menu layout, but when I click on the search input box, the cursor just briefly shows, then disappears, and the keyboard never displays. It seems like its loosing focus possibly? I've been trying to figure this out for a while now with zero luck.
Code is below. Has been edited for clarity.
Thanks for any ideas or suggestions.
layout with SearchView
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
tools:context=".MainActivity">
<androidx.appcompat.widget.SearchView
android:id="@+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:iconifiedByDefault="false"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="always|collapseActionView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.163"
app:queryHint="Search Here" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list_slidermenu"
android:layout_width="313dp"
android:layout_height="362dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/searchView"
app:layout_constraintVertical_bias="0.32" />
</androidx.constraintlayout.widget.ConstraintLayout>
activity_main
<?xml version="1.0" encoding="utf-8"?>
<!--the root view must be the DrawerLayout-->
<androidx.drawerlayout.widget.DrawerLayout 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:id="@+id/my_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="HardcodedText">
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
tools:context=".MainActivity">
</androidx.constraintlayout.widget.ConstraintLayout>
<!--this the navigation view which draws
and shows the navigation drawer-->
<!--include the menu created in the menu folder-->
<!--app:menu="@menu/navigation_menu" this goes in nav view if needed-->
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="350dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:fitsSystemWindows="true"
android:clickable="true"
>
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/some_id_if_needed"
layout="@layout/nav_choose_foods" />
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
MainActivity
public class MainActivity extends AppCompatActivity implements RecyclerViewAdapter_AllFoods.ItemClickListener, RecyclerViewAdapter_Search.ItemClickListener, RecyclerViewAdapter_Nutrients.ItemClickListener, RecyclerViewAdapter_FoodTotals.ItemClickListener{
public DrawerLayout drawerLayout;
public ActionBarDrawerToggle actionBarDrawerToggle;
SearchView searchView;
//JSONObject finalSums = new JSONObject();
static RecyclerViewAdapter_AllFoods adapter;
static RecyclerViewAdapter_Nutrients adapter_nutrients;
static RecyclerViewAdapter_FoodTotals adapter_foodtotals;
static RecyclerViewAdapter_Search searchList_Recycler_Adapter;
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Objects.requireNonNull(getSupportActionBar()).setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.custom_toolbar);
ActionBar action = getSupportActionBar();
action.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
Toolbar toolbar=(Toolbar)action.getCustomView().getParent();
toolbar.setContentInsetsAbsolute(0, 0);
toolbar.getContentInsetEnd();
toolbar.setPadding(0, 0, 0, 0);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
drawerLayout = findViewById(R.id.my_drawer_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.nav_open, R.string.nav_close);
//list = (ListView) findViewById(R.id.list_slidermenu);
// pass the Open and Close toggle for the drawer layout listener
// to toggle the button
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
// to make the Navigation drawer icon always appear on the action bar
//runProgram();
// data to populate the RecyclerView with
RecyclerView recyclerView = findViewById(R.id.list_slidermenu);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
searchList_Recycler_Adapter = new RecyclerViewAdapter_Search(this, LoadData.vegListJSON);
searchList_Recycler_Adapter.setClickListener(this);
recyclerView.setAdapter(searchList_Recycler_Adapter);
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView=(SearchView) findViewById(R.id.searchView);
searchView.setFocusable(true);// searchView is null
searchView.setFocusableInTouchMode(true);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
// set up the RecyclerView
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
Well, I just figured this out. It was the line "android:descendantFocusability="blocksDescendants" that was screwing things up. Not sure how I missed that.