Reputation: 1154
In my application there is a activity for searching event. In the layout i have added a searchview widget (android.support.v7.widget.SearchView) inside toolbar. When the activity is launched the searchview is expanded by default as i have set searchView.setIconified(false).
Searchview when expanded.
The problem is if i close the searchview by taping on the "X" close button, the toolbar title is not visible and the search icon is shown right after the arrow back button.
Searchview after taping on the "X" close button.
EventSearchResultActivity.java file
package xyz.bnayagrawal.android.icsapp.event;
import android.app.SearchManager;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import xyz.bnayagrawal.android.icsapp.R;
public class EventSearchResultActivity extends AppCompatActivity {
private String query_string;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_search_result);
Toolbar toolbar = (Toolbar) findViewById(R.id.search_event_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
SearchView searchView = (SearchView) findViewById(R.id.search_event_searchView);
searchView.setIconified(false);
searchView.clearFocus();
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
query_string = intent.getStringExtra(SearchManager.QUERY);
searchView.setQuery(query_string, false);
//Perform search
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_event_search_result.xml layout file
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/layoutBackground"
tools:context="xyz.bnayagrawal.android.icsapp.event.EventSearchResultActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
android:background="@android:color/white">
<android.support.v7.widget.Toolbar
android:id="@+id/search_event_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Light"
android:elevation="3dp"
app:title="Search Event">
<android.support.v7.widget.SearchView
android:id="@+id/search_event_searchView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="-16dp"
android:paddingStart="-16dp"
android:queryHint="Search event..."
app:queryHint="Search event...">
</android.support.v7.widget.SearchView>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.constraint.ConstraintLayout>
</android.support.design.widget.CoordinatorLayout>
I have set the width of the searchview to match_parent, that's why its hiding the toolbar content. If it were a menu item i could have added [app:showAsAction="collapseActionView|ifRoom"] this line. I want the search icon to be floated left (like if it were a menu item) and the toolbar title visible. How to solve this? Sorry for my bad code formatting.
Upvotes: 3
Views: 584
Reputation: 31
Declare "toolbar" variable in global and use this code for close SearchView
toolbar.collapseActionView()
Upvotes: 3