Can I align SearchView in AppCompat 21 using the toolbar?

I want to align SearchView on tablets when using the new material theme and AppCompat 21. Is this possible or should I add my own layout to the toolbar?

enter image description here

@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_search, menu); mnuSearch = menu.findItem(R.id.menu_search); android.support.v7.widget.SearchView searchView = (SearchView) MenuItemCompat.getActionView(mnuSearch); searchView.setGravity(Gravity.LEFT); searchView.setIconifiedByDefault(false); searchView.setQueryHint(getString(R.string.search)); searchView.requestFocus(); return true; } 
+6
source share
1 answer

I would rather define your SearchView in the layout and then call

 View v = findViewById(R.id.your_search_view); getSupportActionBar().setCustomView(v); 

Thus, it is already aligned to the left, and if not, you can set its LayoutParams .

If you use the Toolbar with AppCompat, everything is simpler because it acts like a ViewGroup .

 <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:abc="http://schemas.android.com/apk/res-auto" android:id="@+id/toolbar" android:layout_height="?attr/actionBarSize" android:layout_width="match_parent"> <your.SearchView /> // here you have control over layout_gravity </android.support.v7.widget.Toolbar> 
+2
source

Source: https://habr.com/ru/post/980638/


All Articles