Android searchview remove hint padding - java

I'm working in my app and have a searchview inside toolbar but the hint has a small padding after the cursor.
How can I remove the padding?
Here is my searchview:
and I would like that:
here is my onCreateOptionsMenu:
SearchManager searchManager=(SearchManager)getSystemService(Context.SEARCH_SERVICE);
MenuItem item=menu.findItem(R.id.procura);
Drawable drawable = menu.findItem(R.id.procura).getIcon();
if (drawable != null) {
drawable.mutate();
drawable.setColorFilter(Color.BLACK, PorterDuff.Mode.SRC_ATOP);
}
searchView=(SearchView) MenuItemCompat.getActionView(item);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setQueryHint(getResources().getString(R.string.app_hint));
try {
Field mDrawable = SearchView.class.getDeclaredField("mSearchHintIcon");
mDrawable.setAccessible(true);
Drawable drw = (Drawable) mDrawable.get(searchView);
drw.setBounds(0, 0, 0, 0);
} catch (Exception e) {
e.printStackTrace();
}
My styles:
<style name="SearchViewMy" parent="Widget.AppCompat.Light.SearchView">
<item name="submitBackground">#color/colorPrimary</item>
<item name="queryBackground">#color/colorPrimary</item>
<item name="android:colorFocusedHighlight">#color/colorAccent</item>
</style>
My menu:
<menu 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" tools:context=".MainActivity">
<item
android:id="#+id/procura"
android:title="#string/app_hint"
android:icon="#mipmap/ic_search_black_24dp"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"/>

I found the solution for my problem.
Just add this in the styles:
<item name="searchHintIcon">#null</item>
The styles now is that:
<style name="SearchViewMy" parent="Widget.AppCompat.Light.SearchView">
<item name="submitBackground">#color/colorPrimary</item>
<item name="queryBackground">#color/colorPrimary</item>
<item name="searchHintIcon">#null</item>
<item name="android:colorFocusedHighlight">#color/colorAccent</item>
</style>
And onCreateOptionsMenu now is that:
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
SearchManager searchManager=(SearchManager)getSystemService(Context.SEARCH_SERVICE);
MenuItem item=menu.findItem(R.id.procura);
searchView=(SearchView) MenuItemCompat.getActionView(item);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setQueryHint(getResources().getString(R.string.app_hint));
return true;

Related

SeachView does not use full width

I want to make my SearchView full width something like Whatsapp. This is my result.
Example
And this is my code
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:icon="?searchAppBarButton"
android:title="#string/buscar"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView" />
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.agregar_servicio_menu, menu);
MenuItem myActionMenuItem = menu.findItem(R.id.action_search);
searchView = (SearchView) myActionMenuItem.getActionView();
searchView.clearFocus();
searchView.setMaxWidth(Integer.MAX_VALUE);
searchView.setQueryHint(getString(R.string.buscar_empresa));
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
and style
<style name="SearchViewMy" parent="Widget.AppCompat.Light.SearchView">
<item name="searchHintIcon">#null</item>
<item name="android:maxWidth">1000dp</item>
</style>
this is my wanted result for the widht of the SearchView
Example2
Use this library for creating search view like whatsapp "com.miguelcatalan:materialsearchview:1.4.0"
Link of this library MaterialSearchView

Android action bar options menu item custom selectable background

I'm trying to use the default android action bar with a custom view:
<style name="ActionBar" parent="android:Widget.Material.ActionBar.Solid">
<item name="android:displayOptions">showCustom</item>
<item name="android:customNavigationLayout">#layout/customNavigationLayout</item>
</style>
The options menu contains a single item which should always show with text:
<item
android:id="#+id/action_cancel"
android:showAsAction="always|withText"
android:title="#string/action_cancel" />
Now I'm having the issue with the selectable background, which is still the size of an action icon:
How can I setup the action bar to apply a selectable background which fills the whole box of the item?
You can try setting the android:actionBarItemBackground attribute in styles, like so:
<style name="AppTheme" parent="android:Theme.Material">
...
<item name="android:actionBarItemBackground">?android:selectableItemBackground</item>
</style>
Use one of the following solutions:
Solution 1:
Add <item name="actionButtonStyle">#style/ActionButtonStyle</item> to your base application theme with for exmaple:
<style name="ActionButtonStyle" parent="android:Widget.Material.Button">
<item name="android:textColor">#android:color/black</item>
<item name="android:textSize">16sp</item>
<item name="android:background">#drawable/button_background</item>
</style>
and
button_background
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#color/colorAccent"/>
<item android:state_focused="true"
android:drawable="#color/colorPrimaryDark"/>
<item android:drawable="#color/colorPrimary"/>
</selector>
Solution 2:
Use menuItem.setActionView to apply a custom layout to your menu item as follows:
layout_action_cancel (custom menu item layout):
<Button
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:id="#+id/b_action_cancel"
android:gravity="center"
android:background="#drawable/button_background">
</Button>
Then in your onCreateOptionsMenu apply the custom Layout to the menu
item:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu, menu);
LayoutInflater layoutInflater = getLayoutInflater();
View layout = layoutInflater.inflate(R.layout.layout_action_cancel, null, false);
menu.getItem(0).setActionView(layout);
return super.onCreateOptionsMenu(menu);
}
and in onPrepareOptionsMenu set an OnClick Listener (replacing the
onOptionsItemSelected)
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
View layout = menu.getItem(0).getActionView();
if(layout instanceof Button){
Button b_action_cancel = layout.findViewById(R.id.b_action_cancel);
b_action_cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//your code
}
});
}
return super.onPrepareOptionsMenu(menu);
}
You can try applying drawable withe selected and normal state at menu item property :
<item
android:id="#+id/action_cancel"
android:showAsAction="always|withText"
android:title="#string/action_cancel"
android:icon="#drawable/mybuttonbackground" />

How can I implement onCreateOptionsMenu on my bottomNavigationView

Hi I wouldike to know when I click on my bottomnavigationView how to display my search toolbar ?
getMenuInflater().inflate(R.menu.toolbar_menu, menu);
return super.onCreateOptionsMenu(menu);
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
return true;
case R.id.navigation_loope:
toolbar_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:title="Search"
android:id="#+id/action_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
Bottom_nav_view.xml
<item
android:id="#+id/navigation_home"
android:icon="#drawable/ic_home"
android:title="#string/title_home" />
<item
android:id="#+id/navigation_loope"
android:icon="#drawable/ic_loope"
android:title="#string/title_loope"
/>
<item
android:id="#+id/navigation_take_photo"
android:icon="#drawable/ic_take_photo"
android:title="#string/title_take_photo" />
<item
android:id="#+id/navigation_public"
android:icon="#drawable/ic_public"
android:title="#string/title_public" />
</menu>
I can't actually use getMenuInflater().inflate(R.menu.toolbar_menu, menu) I don't know how to implement it for doing the same result when i click on my loope (R.id.navigation_loope)
EDIT : Bottom_nav_view it's for my bottom bar and toolbar_menu for my topbar. If I click on the item navigation_loop, I wouldike to show the search bar on the top.

How to get a thinner font in options menu android

I've tried making a option menu in my activity, and I succeed. The problem now, is that the text style is bold, and I don't know how to make it normal. I tried setting textStyle, but it had no effect.
option_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/AppointmentItem"
app:showAsAction="always"
android:icon="#drawable/ic_notifications_white_24dp"
android:title="Appointments"/>
<item android:id="#+id/WeekItem"
app:showAsAction="never"
android:icon="#drawable/ic_today_24dp"
android:title="Week vooruit/achteruit"/>
<item android:id="#+id/LeerlingItem"
app:showAsAction="never"
android:icon="#drawable/ic_person_24dp"
android:title="Leerling veranderen"/>
<item android:id="#+id/InstelllingenItem"
app:showAsAction="never"
android:icon="#drawable/ic_settings_24dp"
android:title="Instellingen"/>
<item android:id="#+id/OverItem"
app:showAsAction="never"
android:icon="#drawable/ic_work_white_24dp"
android:title="Over ons"/>
</menu>
How I create the options menu in my mainactivity.java
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.option_menu, menu); //your file name
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(final MenuItem item) {
switch (item.getItemId()) {
case R.id.AppointmentItem:
return true;
case R.id.WeekItem:
getWeek();
return true;
case R.id.LeerlingItem:
getLeerling();
return true;
case R.id.InstelllingenItem:
getSettings();
return true;
case R.id.OverItem:
getAbout();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
How it looks (first) and how I want it (last):
Fixed it by changing "TextAppearance.AppCompat.Widget.ActionBar.Title" to "TextAppearance.AppCompat.Widget.ActionBar.Subtitle".
<style name="MyActionBar.MenuTextStyle" parent="style/TextAppearance.AppCompat.Widget.ActionBar.Subtitle">
<item name="android:textColor">#color/black</item>
<item name="android:textStyle">normal</item>
<item name="android:textSize">16dp</item>
</style>
Thanks to #Rotwang for advising me to check the styles.xml

Action items/icons not displaying on action bar in Android App

I am trying to have a search action on my action bar. I have set an icon and have showAsAction set to always but it is still always displayed in the overflow menu. How can I make it display on the action bar itself? Thanks!
Activity
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
setTitle("Add");
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
Toast.makeText(getApplicationContext(), "Settings",
Toast.LENGTH_LONG).show();
return true;
}
if (id == R.id.action_search) {
Toast.makeText(getApplicationContext(), "Search",
Toast.LENGTH_LONG).show();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Menu XML file
<menu 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"
tools:context="com.example.testtabs.MainActivity" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"/>
<item
android:id="#+id/action_search"
android:actionViewClass="android.support.v7.widget.SearchView"
android:icon="#drawable/ic_search"
android:showAsAction="always"
android:title="#string/title_search"/>
</menu>
Either remove
android:actionViewClass="android.support.v7.widget.SearchView"
or delete your case statement for action_search in onOptionsItemSelected. Doing either of those will display the icon.
If you want to use SearchView the callback is handled differently than onOptionsItemSelected.
Documentation here: http://developer.android.com/guide/topics/ui/actionbar.html#ActionView
Just change app:showAsAction instead of android:showAsAction
<menu 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"
tools:context="com.example.testtabs.MainActivity" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"`enter code here`
android:title="#string/action_settings"
app:showAsAction="never"/>
<item
android:id="#+id/action_search"
android:actionViewClass="android.support.v7.widget.SearchView"
android:icon="#drawable/ic_search"
app:showAsAction="always"
android:title="#string/title_search"/>
</menu>

Categories

Resources