How to change the background of the Search View in the Appbar in Android? - java

I want to change the background of the SearchView in the Appbar which is available default in Android using Java.
The below image shows how my current app bar looks like:
The below image shows what kind of SearchView background I want to achieve (Something similar to this):
The below is the code that I have for the search bar:
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
MenuItem item = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) item.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
adapter.getFilter2().filter(newText);
return false;
}
});
The xml code is:
<androidx.appcompat.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#1F2D78"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:titleTextColor="#color/white">
Please help me to figure out how to achieve this

Instead of using menu search view you can use custom edittext inside toolbar.
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBar"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center"
app:elevation="0dp">
<com.google.android.material.appbar.MaterialToolbar
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#color/white"
app:contentInsetEnd="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center">
<ImageView
android:id="#+id/btnBack"
android:layout_width="22dp"
android:layout_height="match_parent"
android:layout_marginEnd="10dp"
android:layout_marginStart="15dp"
android:src="#drawable/ic_back" />
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/searchContainer"
android:layout_weight="1"
app:endIconMode="none"
android:layout_width="0dp">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/edtSearch"
android:drawableStart="#drawable/ic_search"
android:drawablePadding="3dp"
android:hint="Search.."
android:paddingStart="7dp"
android:paddingEnd="7dp"
android:inputType="textPersonName"/>
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
</com.google.android.material.appbar.MaterialToolbar>
</com.google.android.material.appbar.AppBarLayout>
and you can customize your TextInputLayout

Related

Why My hamburger icon in android app is not working it is not opening the navigation menu?

I am working on an app but there is a problem with hamburger icon as it is not working. It is not opening the navigation menu when i am clicking on it. what is the problem please tell me? I am trying to solve it but dont know what is the problem with it. I am new to code please help me.
Here is my fragment code
public class HomeFragment extends Fragment{
NavigationView navigationView;
ActionBarDrawerToggle toggle;
DrawerLayout drawerLayout;
Toolbar toolbar;
private int[] images;
private SliderAdapter sliderAdapter;
public HomeFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
navigationView = view.findViewById(R.id.navmenu);
drawerLayout = view.findViewById(R.id.drawerlayout);
toolbar = view.findViewById(R.id.toolbar);
ActionBar mActionBar;
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
Objects.requireNonNull(((AppCompatActivity) getActivity()).getSupportActionBar()).setTitle("");
toggle = new ActionBarDrawerToggle(getActivity(), drawerLayout, R.string.open, R.string.close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((AppCompatActivity) getActivity()).getSupportActionBar().setHomeAsUpIndicator(R.drawable.menu3);
return view;
}
}
Here is my XML file
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/drawerlayout"
tools:openDrawer="start"
android:background="#color/white"
tools:context=".HomeFragment">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/reddark"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/Theme.AppCompat.Light">
<ImageView
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:src="#drawable/backsssososos" />
</androidx.appcompat.widget.Toolbar>
<RelativeLayout
android:id="#+id/search_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/toolbar"
android:background="#color/reddark"
android:padding="10dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<EditText
android:id="#+id/searchbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/searchbardesign"
android:backgroundTint="#F8F8F8"
android:drawableLeft="#android:drawable/ic_menu_camera"
android:drawableRight="#android:drawable/ic_menu_search"
android:drawablePadding="22dp"
android:gravity="left|center"
android:hint="Enter City,Colony name..."
android:imeOptions="actionSearch"
android:inputType="text"
android:maxLines="1"
android:padding="10dp"
android:textColor="#color/black"
android:textColorHint="#android:color/darker_gray"
android:textSize="16dp" />
</RelativeLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:id="#+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="vertical">
<com.smarteist.autoimageslider.SliderView
android:id="#+id/imageSlider"
android:layout_width="match_parent"
android:layout_height="300dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:sliderAnimationDuration="600"
app:sliderAutoCycleDirection="back_and_forth"
app:sliderAutoCycleEnabled="true"
app:sliderIndicatorAnimationDuration="600"
app:sliderIndicatorGravity="center_horizontal|bottom"
app:sliderIndicatorMargin="15dp"
app:sliderIndicatorOrientation="horizontal"
app:sliderIndicatorPadding="3dp"
app:sliderIndicatorRadius="2dp"
app:sliderIndicatorSelectedColor="#5A5A5A"
app:sliderIndicatorUnselectedColor="#FFF"
app:sliderScrollTimeInSec="1"
app:sliderStartAutoCycle="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text_file_2"></TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text_file_2">
</TextView>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/navmenu"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:itemIconTint="#color/black"
android:theme="#style/NavigationDrawerStyle"
app:headerLayout="#layout/navheader"
app:itemTextColor="#151515"
app:menu="#menu/navigationmenu">
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
you must attach toolbar in ActionBarDrawerToggle
mDrawerToogle = new ActionBarDrawerToggle(this, drawer,toolbar,R.string.navigation_drawer_open, R.string.navigation_drawer_close);
bar.setHomeButtonEnabled(true);
bar.setDisplayHomeAsUpEnabled(true);
mDrawerToogle.syncState();
drawer.setDrawerListener(mDrawerToogle);
you have to handle this action by yourself
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
drawerLayout.openDrawer(Gravity.LEFT);
return true;
}
return super.onOptionsItemSelected(item);
}
also syncState() call should be placed in onPostCreate or even in onResume
btw. your empty constructor should at least call super();

What's this strange white rectangle behind my toolbar?

I've been trying to get rid of it all day to no avail. Tried giving every view and layout around it an invisible background color. Also tried reducing the size of the layouts and views it's in. Those sharp white edges persist. What are they? Also hi this is text filler, website says there's too much code and too little text. "Hi text filler, you good?"
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<include
android:id="#+id/mainToolbar"
layout="#layout/toolbar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/mainToolbar">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/tab_anim_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="#00FFFFFF">
<com.google.android.material.appbar.MaterialToolbar
android:id="#+id/searchToolbar"
style="#style/Widget.MaterialComponents.Toolbar.Primary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:background="#A106A1"
android:elevation="2dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:layout_scrollFlags="scroll|enterAlways">
<!-- dummy to catch focus -->
<LinearLayout
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true" />
<androidx.appcompat.widget.SearchView
android:id="#+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:layoutDirection="rtl"
app:iconifiedByDefault="true"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"/>
</com.google.android.material.appbar.MaterialToolbar>
</com.google.android.material.appbar.AppBarLayout>
<include layout="#layout/content_scrolling"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
The toolbar in code:
//code to make the toolbar edges round
MaterialToolbar searchToolbar = findViewById(R.id.searchToolbar);
MaterialShapeDrawable searchbarBackground = (MaterialShapeDrawable) searchToolbar.getBackground();
searchbarBackground.setShapeAppearanceModel(
searchbarBackground.getShapeAppearanceModel()
.toBuilder()
.setBottomRightCorner(CornerFamily.ROUNDED,100)
.setBottomLeftCorner(CornerFamily.ROUNDED,100)
.build()
);
//code to add search function
SearchView searchView = findViewById(R.id.searchView);
searchView.setImeOptions(EditorInfo.IME_ACTION_DONE);//so that button on keyboard is a check
searchView.setQueryHint("Search...");
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
ItemAdapter.getFilter().filter(newText);
return false;
}
});
It is the shadow of the AppBarLayout. Use:
<com.google.android.material.appbar.AppBarLayout
app:elevation="0dp"
android:background="#android:color/transparent"

Show/Hide BottomNavigationView for specific fragment

In my android application, I want the bottom menu bar to disappear when the user focuses the SearchView (this also pops the soft keyboard up). When the SearchView loses focus, I want to show the bottom navigation bar again.
I have tried using setVisibility() and the view does hide or show, but it always retains its height for some reason.Below is the code for my BottomNavigationView:
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="60dp"
app:menu="#menu/bottom_navigation_menu"
app:elevation="80dp"
app:labelVisibilityMode="labeled"
app:itemTextColor="#color/bottom_nav_color"
app:itemIconTint="#color/bottom_nav_color"
android:background="?attr/backgroundColor">
Code that handles hiding/showing of the navigation bar:
// Needed to close the SearchView when pressing back (instead of just losing focus)
mSearchView.setOnQueryTextFocusChangeListener(
(v, hasFocus) -> {
if (!hasFocus) {
adapter.isSearchMode = false;
bottomNavigationView.setVisibility(View.VISIBLE);
searchMenuItem.collapseActionView();
adapter.notifyDataSetChanged();
} else {
adapter.isSearchMode = true;
bottomNavigationView.setVisibility(View.GONE);
searchMenuItem.collapseActionView();
adapter.notifyDataSetChanged();
}
});
The BottomNavigationView is held by a LinearLayout like so:
<LinearLayout
android:id="#+id/footer"
android:baselineAligned="false"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#drawable/bottom_bar_top_shadow"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="60dp"
app:menu="#menu/bottom_navigation_menu"
app:elevation="80dp"
app:labelVisibilityMode="labeled"
app:itemTextColor="#color/bottom_nav_color"
app:itemIconTint="#color/bottom_nav_color"
android:background="?attr/backgroundColor">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:layout_marginTop="6dp"
android:weightSum="5"
android:elevation="16dp">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center|top">
<TextView
android:id="#+id/missed_calls"
style="#style/unread_count_font"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/unread_message_count_bg"
android:layout_marginStart="20dp"
android:gravity="center"
android:visibility="gone"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center|top">
<TextView
android:id="#+id/missed_chats"
style="#style/unread_count_font"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/unread_message_count_bg"
android:layout_marginStart="20dp"
android:gravity="center"
android:visibility="gone"/>
</RelativeLayout>
</LinearLayout>
</com.google.android.material.bottomnavigation.BottomNavigationView>
</LinearLayout>
On your BottomNavigation use this code snippet:
setVisibility(View.GONE)
View.Gone = No Space lefts for view.

CardView selector?

I'm creating a notes app and I'm using a contextual action bar to delete notes. I have my notes displayed in a card view. I have the action bar working to the point where I can select notes and delete them. But when I select notes I want to use a checkmark as a selector if possible (otherwise any selector) which I have been unsuccessful in achieving.
contextual action bar
mListViewNotes = (ListView) findViewById(R.id.listview_notes);
mListViewNotes.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
mListViewNotes.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
CardView cardView = (CardView) findViewById(R.id.cardView);
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
if (list_items.contains(notes.get(position))) {
count = count-1;
list_items.remove(notes.get(position));
mode.setTitle(count + " Notes Selected");
} else {
count = count+1;
list_items.add(notes.get(position));
mode.setTitle(count + " Notes Selected");
}
if (count == 0) {
mode.setTitle("No Notes Selected");
}
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
app_bar.setVisibility(View.GONE);
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
actionMode = mode;
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
deleteNote();
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
count = 0;
app_bar.setVisibility(View.VISIBLE);
list_items.clear();
}
});
}
item_note.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/light_sand"
android:orientation="vertical"
android:id="#+id/item_note">
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_gravity="center"
android:layout_marginBottom="6dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="6dp"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="6dp">
<ImageView
android:layout_width="1000dp"
android:layout_height="150dp"
android:layout_gravity="start"
android:layout_marginLeft="-80dp"
android:layout_marginTop="-12dp"
android:background="#drawable/texture_tile_old"
android:scaleType="fitStart" />
<ImageView
android:id="#+id/clef_note"
android:layout_width="70dp"
android:layout_height="120dp"
android:layout_gravity="start"
android:layout_marginTop="10dp"
android:background="#drawable/treble_clef" />
<View
android:layout_width="780dp"
android:layout_height="1dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="43dp"
android:background="#color/black" />
<View
android:layout_width="780dp"
android:layout_height="1dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="70dp"
android:background="#color/black" />
<View
android:layout_width="780dp"
android:layout_height="1dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="97dp"
android:background="#color/black" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:scaleType="center">
<TextView
android:id="#+id/list_note_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="65dp"
android:layout_marginTop="8dp"
android:textColor="#color/black"
android:textSize="28sp" />
<TextView
android:id="#+id/list_note_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="46dp"
android:layout_marginLeft="65dp"
android:layout_marginTop="0dp"
android:maxLines="2"
android:textColor="#color/gunmetal"
android:textSize="20sp"
android:lineSpacingExtra="4dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#color/brown"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:text="Last Updated:"
android:textColor="#color/white" />
<TextView
android:id="#+id/list_note_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="4dp"
android:gravity="end"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/white" />
</LinearLayout>
</android.support.v7.widget.CardView>
You can add a CheckBox to your item layout, and/or change the background of the layout itself to display a selected state.
With the first option, you can change the layout to the selected state just by setting the isChecked to true.
checkBox.setChecked(true);
To change the background of the layout, you can simply set the background to a new color resource.
itemLayout.setBackgroundResource(R.color.color_selected);
Or you can use a ColorStateList to do some more advanced state changes (such as a ripple effect to a solid color).
ripple_selector.xml
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/color_highlight">
<item android:drawable="#drawable/color_highlight"/>
</ripple>
item_selector_background.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="#color/color_selected"/>
<item android:drawable="#drawable/ripple_effect"/>
</selector>
To use this, you can set the background attribute in your layout xml to item_selector_background then changed the selected state in your code.
itemLayout.setSelected(true);
MaterialCardView can be a good solution. result
<com.google.android.material.card.MaterialCardView
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:strokeWidth="4dp"
app:rippleColor="#color/colorPrimary"
app:strokeColor="#color/colorAccent"
app:cardCornerRadius="#dimen/margin_app"
app:cardBackgroundColor="#android:color/white">
<androidx.constraintlayout.widget.ConstraintLayout
android:paddingVertical="#dimen/margin_large"
android:paddingHorizontal="#dimen/margin_xlarge"
android:layout_width="match_parent"
android:layout_height="wrap_content">
...
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>

Implementing searchview custom toolbar

I have a toolbar that i created by myself, a custom toolbar and want to place a searchview above so everytime i click the icon it replaces my toolbar.
My Custom toolbar:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/emerald">
<ImageView
android:id="#+id/arrow_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="0"
android:src="#drawable/ic_002_keyboard_left_arrow_button"
android:onClick="previousActivity"
android:tint="#color/white"/>
<com.example.afcosta.inesctec.pt.android.Helpers.NexusBoldTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:layout_gravity="left"
android:layout_marginLeft="25dp"
android:id="#+id/toolbar_title"
android:textSize="#dimen/toolbar_title" />
<ImageView
android:id="#+id/user_ic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="0"
android:src="#drawable/ic_user"
android:onClick="userProfile"
android:layout_marginRight="25dp"
android:tint="#color/white"/>
<ImageView
android:id="#+id/user_ic2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="0"
android:src="#drawable/ic_search_black_24dp"
android:tintMode="#color/white"
android:onClick="userProfile"
android:layout_marginRight="25dp"
android:tint="#color/white"/>
</android.support.v7.widget.Toolbar>
i include it on other activities like this:
<include
layout="#layout/custom_toolbar"
android:id="#+id/my_toolbar"
tools:layout_constraintTop_creator="1"
tools:layout_constraintLeft_creator="1"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="0dp" />
now i want to use the searchview on this custom toolbar, but i can't even add it, any help?
Create a menu with item
<item
android:id="#+id/action_search"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"/>
In your activity
#Override
public boolean onCreateOptionsMenu( Menu menu) {
getMenuInflater().inflate( R.menu.menu_main, menu);
MenuItem search = menu.findItem( R.id.action_search);
final SearchView searchView = (SearchView) search.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
//Do your action
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return true;
}

Categories