In my project I am using search view of android.
It is working properly.
But I am having one problem i.e related to back image while searchview expands.
This is my action bar with icon and other search and drawable icon.
Now when I click on search Icon
Here Icon is changes. It's by default taking launcher image. but I want it to be same as its showing in image 1.
So can any one help me with this issue.
edit1
here is my action bar style xml.
<style name="Theme.Example" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="actionBarItemBackground">#drawable/selectable_background_example</item>
<item name="actionBarTabStyle">#style/ActionBarTabStyle.Example</item>
<item name="actionBarStyle">#style/ActionBar.Solid.Example</item>
<item name="actionModeBackground">#drawable/cab_background_top_example</item>
<item name="actionModeSplitBackground">#drawable/cab_background_bottom_example</item>
<item name="actionBarTabTextStyle">#style/ActionBarTabText.Example</item>
<!-- Light.DarkActionBar specific -->
<item name="android:textColorHighlight">#99c0c0c0</item>
</style>
edit2
substyle for actionbar
<style name="ActionBar.Solid.Example" parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:icon">#drawable/ic_logo</item>
<item name="background">#drawable/ab_solid_example</item>
<item name="backgroundStacked">#drawable/ab_stacked_solid_example</item>
<item name="backgroundSplit">#drawable/ab_bottom_solid_example</item>
</style>
edit 3
This is my onCreateOptionsMenu method.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
try {
search = (MenuItem) menu.findItem(R.id.search);
MenuItemCompat.setOnActionExpandListener(search,
new OnActionExpandListener() {
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Do something when collapsed
layoutList.setVisibility(View.INVISIBLE);
return true; // Return true to collapse action view
}
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
// Do something when expanded
layoutList.setVisibility(View.VISIBLE);
return true; // Return true to expand action view
}
});
SearchView searchView = (SearchView) MenuItemCompat
.getActionView(search);
setSearchTextColour(searchView);
searchView.setQueryHint(getString(R.string.search_hint));
searchView.setOnQueryTextListener(this);
searchView.setOnCloseListener(new OnCloseListener() {
#Override
public boolean onClose() {
// TODO Auto-generated method stub
Debugger.debugE("on close");
layoutList.setVisibility(View.INVISIBLE);
return false;
}
});
} catch (Exception e) {
// TODO: handle exception
}
return super.onCreateOptionsMenu(menu);
}
and to show actionbar
ActionBar actionBar = getSupportActionBar();
SpannableString s = new SpannableString(getString(R.string.app_name));
s.setSpan(new TypeFaceSpan(getContext(),
getString(R.string.font_gothum)), 0, s.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
actionBar.setTitle(s);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setBackgroundDrawable(new ColorDrawable(Color
.parseColor("#bb0404")));
There is nothing else that I had used.
note: In all other navigation from the main screen icon which is showing in first image is coming.
Issue is only with searchview.
Thanks in advance.
Bskania
Try changing your app theme on your styles.xml
You can do something like this:
<style name="AppTheme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/ActionBar</item>
</style>
<style name="ActionBar" parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:icon">#drawable/myicon</item>
</style>
create a custom action bar :
LayoutInflater inflator = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.custom_search_actionbar, null);
actionBar.setCustomView(v);
_searchView = (SearchView)v.findViewById(R.id.searchView);
....
....
....
and define ids of views and use .
Related
I don't understand why is so painful to change a color for an android app.
I tried several ways to change the background color of the popup menu in my action bar with no success.
I'm using a AppTheme.NoActionBar to style, obviously, the action bar.
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="navigationIcon">#drawable/ic_return_dark</item>
<item name="overlapAnchor">false</item>
<item name="popupMenuStyle">#style/PopupMenu</item>
</style>
<style name="PopupMenu" parent="Widget.AppCompat.PopupMenu">
<item name="android:popupBackground">#color/color4</item>
</style>
Following the examples that I found, there was this explanation that you need to insert in your custom style (in my case AppTheme.NoActionBar) a custom popupMenuStyle in order to custom the popupBackground, which changes the background color of popup menu.
It doesn't work.
What can I do to change the background color of popup menu?
To change the color of the Options Menu in Android, changing themes and styles won't help. You have to initialise LayoutInflater Factory Class.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu, menu);
getLayoutInflater().setFactory(new Factory() {
#Override
public View onCreateView(String name, Context context,
AttributeSet attrs) {
if (name .equalsIgnoreCase(“com.android.internal.view.menu.IconMenuItemView”)) {
try {
LayoutInflater f = getLayoutInflater();
final View view = f.createView(name, null, attrs);
new Handler().post(new Runnable() {
public void run() {
view.setBackgroundResource(R.drawable.your_color);
((TextView) view).setTextColor(Color.your_color);
}
});
return view;
} catch (InflateException e) {
} catch (ClassNotFoundException e) {
}
}
return null;
}
});
return super.onCreateOptionsMenu(menu);
}
If someone is still wondering is there is a better solution to this problem, here's the explanation and answer.
Since, the question clearly mentions using NoActionBar theme for app, setting popupMenu directly to AppTheme will not work.
Instead you need to apply that theme manually to your toolbar in xml
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/PopupMenu" />
and in your styles.xml file
<style name="PopupMenu" parent="Theme.AppCompat.Light">
<item name="android:textColor">#color/white</item>
<item name="android:colorBackground">#color/popup_color</item>
</style>
By default if the SearchView x button (on the right of SearchView) is pressed while the SearchView is empty, the keyboard will hide while shrinking the SearchView to just the magnifying glass icon. This causes the bottom border of the SearchView to hide as well as the hint (placeholder). How can I remove this functionality so the SearchView never shrinks and loses its features when the x button is pressed?
In onCreateOptionsMenu method, set an OnCloseListener to the SearchView and return true in it's onClose() method. This will prevent it's default behavior, so it will do nothing. Check the example below:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.search_options_menu, menu);
MenuItem searchMenuItem = menu.findItem(R.id.itemSearch);
searchView = (SearchView) searchMenuItem.getActionView();
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
#Override
public boolean onClose() {
return true;
}
});
}
STEP 1:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="actionBarWidgetTheme">#style/AppTheme.WidgetTheme</item>
</style>
<style name="AppTheme.WidgetTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="searchViewCloseIcon">#android:color/transparent</item>
</style>
OR
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="searchViewStyle">#style/AppTheme.SearchView</item>
</style>
<style name="AppTheme.SearchView" parent="Widget.AppCompat.SearchView">
<item name="closeIcon">#android:color/transparent</item>
</style>
or
If you want to do it programmatically , try the following
Step2:
searchView.setBackgroundColor(Color.WHITE);
or
((EditText)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text)).setHintTextColor(Color.WHITE);
I have a SearchView in my ActionBar and I want to customize the style of it, but I have not found out how.
I have tried this in my v21\styles.xml
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="searchViewStyle">#style/AppTheme.SearchView</item>
</style>
<style name="AppTheme.SearchView" parent="Widget.AppCompat.Light.SearchView">
<item name="android:queryBackground">#color/white</item>
<item name="android:submitBackground">#color/white</item>
</style>
This is my SeachView in my menu.xml
<item android:id="#+id/action_search"
android:title="#string/action_search"
android:icon="#drawable/ic_action_search"
appcompat:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom"/>
Edit: Setting queryHint and iconifiedByDefault in the xml does not work, but setting it in the activity does. Does anyone know why? This is weird...
As the Android Developers Blog states:
values/themes.xml:
<style name=”Theme.MyTheme” parent=”Theme.AppCompat”>
<item name=”searchViewStyle”>#style/MySearchViewStyle</item>
</style>
<style name=”MySearchViewStyle” parent=”Widget.AppCompat.SearchView”>
<!-- Background for the search query section (e.g. EditText) -->
<item name="queryBackground">...</item>
<!-- Background for the actions section (e.g. voice, submit) -->
<item name="submitBackground">...</item>
<!-- Close button icon -->
<item name="closeIcon">...</item>
<!-- Search button icon -->
<item name="searchIcon">...</item>
<!-- Go/commit button icon -->
<item name="goIcon">...</item>
<!-- Voice search button icon -->
<item name="voiceIcon">...</item>
<!-- Commit icon shown in the query suggestion row -->
<item name="commitIcon">...</item>
<!-- Layout for query suggestion rows -->
<item name="suggestionRowLayout">...</item>
</style>
// Configure the search products widget
SearchManager searchManager = (SearchManager)mActivity.getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView)menu.findItem(R.id.action_search).getActionView();
if(searchView != null)
{
// Assumes current activity is the searchable activity
searchView.setSearchableInfo(searchManager.getSearchableInfo(mActivity.getComponentName()));
// Do not iconify the widget, expend it by default
searchView.setIconifiedByDefault(false);
// Customize the search view
searchView.setQueryHint(getResources().getString(R.string.search_hint));
searchView.setMaxWidth(1000);
searchView.setWeightSum(5.0f);
View searchPlate = searchView.findViewById(android.support.v7.appcompat.R.id.search_plate);
EditText searchSourceText = (EditText)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchPlate.setBackgroundColor(Color.TRANSPARENT);
searchPlate.setMinimumWidth(10000);
searchSourceText.setTextColor(Color.WHITE);
searchSourceText.setWidth(10000);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
{
#Override
public boolean onQueryTextSubmit(String s)
{
Log.v("onQueryTextSubmit", String.format("Query of search: %s", s));
Intent searchIntent = new Intent(mActivity, SearchProducts.class);
searchIntent.putExtra(SearchProducts.QUERY, s);
startActivity(searchIntent);
return false;
}
#Override
public boolean onQueryTextChange(String s) { return false; }
});
searchView.setOnCloseListener(new SearchView.OnCloseListener()
{
#Override
public boolean onClose()
{
Log.v("Main", "searchView::OnCloseListener");
return false;
}
});
}
I implemented a Material Design nav drawer using this example. I customize it according to my theme. Everything is working perfectly. Just the last thing I want to customize it's menu icon. I googled it, but have not find any good solution. I want to change its color if possible , or add a custom drawable. Any solution on idea. Thanks in advance
This is my Application theme which i set in menifest
<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:textColorPrimary">#color/textColorPrimary</item>
<item name="android:windowBackground">#color/windowBackground</item>
</style>
And this is my toolbar style
<
style name="GalaxyZooThemeToolbarDarkOverflow" parent="Theme.AppCompat.NoActionBar">
<item name="android:textColorPrimary">#color/headerColor</item>
<item name="android:iconPreview">#drawable/ic_launcher</item>
<item name="actionMenuTextColor">#color/colorPrimaryDark</item>
<item name="android:textColorSecondary">#color/headerColor</item>
My Main activity code
public class Activity_Home extends ActionBarActivity implements FragmentDrawer.FragmentDrawerListener {
private static String TAG = Activity_Home.class.getSimpleName();
private Toolbar mToolbar;
private FragmentDrawer drawerFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_drawer);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
drawerFragment = (FragmentDrawer)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
drawerFragment.setDrawerListener(this);
// display the first navigation drawer view on app launch
displayView(0);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity__main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
//
// if(id == R.id.action_search){
// Toast.makeText(getApplicationContext(), "Search action is selected!", Toast.LENGTH_SHORT).show();
// return true;
// }
return super.onOptionsItemSelected(item);
}
#Override
public void onDrawerItemSelected(View view, int position) {
displayView(position);
}
private void displayView(int position) {
}
}
Add:
getSupportActionBar().setHomeAsUpIndicator(resId);
or
getSupportActionBar().setHomeAsUpIndicator(drawable);
to your Activity's onCreate
Found a sloution myself. It was pretty easy indeed. All you have to do is set a toolbar style. In your style primary color act as a toolbar title and textScondary color act as a menu icon color.
style name="GalaxyZooThemeToolbarDarkOverflow" parent="Theme.AppCompat.NoActionBar">
<item name="android:textColorPrimary">#color/headerColor</item>
<item name="android:iconPreview">#drawable/ic_launcher</item>
<item name="actionMenuTextColor">#color/colorPrimaryDark</item>
<item name="android:textColorSecondary">#color/headerColor</item>
I have made a custom theme for my app which uses action bar.Now when i run my app the actionbar is visible but button in it is not visible.I dont know what went wrong.
The button is visible when i click on menu button (the 3 hardware button) but not displaying on the action bar
Style.Xml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<style name="TripLoggerTheme" parent="android:Theme.Holo.Light">
<item name="android:actionBarStyle">#style/TripLoggerActionBar</item>
<!--<item name="android:homeAsUpIndicator">#drawable/test</item>-->
</style>
<!--use to style the actionbar-->
<style name="TripLoggerActionBar" parent="android:Widget.ActionBar">
<item name="android:background">#6e784c</item>
<item name="android:showAsAction">ifRoom|always|collapseActionView|never|withText</item>
<item name="android:displayOptions">homeAsUp|showHome|showTitle</item>
</style>
Menu.Xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_settings"
android:title="#string/action_settings"
android:icon="#drawable/images"
app:showAsAction="always" />
</menu>
Code
public class MainActivity extends Activity {
private int[] images = {R.drawable.images, R.drawable.images_2, R.drawable.images_1, R.drawable.images_4, R.drawable.images, R.drawable.images_2};
private GridView gridView;
private LinearLayout linearLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.base_activity);
linearLayout = (LinearLayout) findViewById(R.id.root);
getActionBar();
// gridView = (GridView) findViewById(R.id.grid);
// gridView.setAdapter(new CustomAdapter(MainActivity.this, images));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
linearLayout.setVisibility(View.VISIBLE);
break;
}
return true;
}
As the screenshots shows,when i click on the 3 dots from below test is visible but it should be visible on the top where i have marked a cross
I tried finding out what could be the problem but all in vain.
I believe what you are experiencing is the standard Android behaviour. You may want to read the following text on the official docs on Action Overflow:
The overflow icon only appears on phones that have no menu hardware keys. Phones with menu keys display the action overflow when the user presses the key.
First, from snapshot, it seems that you are testing in emulator. Check if same happens on the device.