I'm trying to change the size of my overflow menu button. I've changed the actual icon from the default 3 dots to my own settings icon in my drawables. Here's the code:
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:colorForeground">#color/colorPrimaryDark</item>
<item name="android:actionOverflowButtonStyle">#style/ToolbarOptions</item>
</style>
<!--Toolbar-->
<style name="ToolbarOptions" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:src">#drawable/settings</item>
<item name="android:contentDescription">settings</item>
<item name="android:width">9dp</item>
<item name="android:height">9dp</item>
</style>
</resources>
and then here's the java:
public class Home extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home_menu, menu);
return true;
}
}
So how do I change the size of the settings icon? I've changed the width and height in the style tags below but it stays the same.
Related
I want to make an action bar with a button on the right with a margin_right. like this
And I use the action button to do it. But the button was on the right without margin_right.
android:layout_marginRight doesn't work here.
here is my styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarSize">50dp</item>
<item name="android:actionBarStyle">#style/my_actionbar_style</item>
<item name="android:actionButtonStyle">#style/my_action_button</item>
</style>
<style name="my_actionbar_style" parent="android:Widget.ActionBar">
<item name="android:background">#color/actionbar_backgroud</item>
<item name="android:titleTextStyle">#style/myTitleStyle</item>
<item name="android:displayOptions">showTitle</item>
</style>
<style name="myTitleStyle">
<item name="android:textColor">#ffffff</item>
<item name="android:textSize">20sp</item>
</style>
<style name="my_action_button">
<item name="android:background">#drawable/button_selector</item>
<item name="android:width">70dp</item>
<item name="android:textSize">13sp</item>
<item name="android:layout_marginTop">10dp</item>
<item name="android:layout_marginBottom">10dp</item>
<item name="android:layout_marginRight">10dp</item>
</style>
and this is my menu.xml:
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
tools:ignore="AppCompatResource">
<item
android:id="#+id/action_search"
android:title="submit"
android:showAsAction="always" /></menu>
Another quick and clean way to go about this is specifying your own layout for that menu button. something like this
<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.MainActivity">
<item
android:id="#+id/action_custom_button"
android:title="Custom Button"
android:icon="#drawable/ic_custom_button"
app:actionLayout="#layout/layout_to_custom_button"
app:showAsAction="always" />
</menu>
there should be
layout_to_custom_button.xml
layout file which contains the padding and style you wanted.
and also do this in your activity for the click event
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_product_detail, menu);
final Menu mMenu = menu;
final MenuItem item = menu.findItem(R.id.action_custom_button);
item.getActionView().setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
mMenu.performIdentifierAction(item.getItemId(), 0);
}
});
return true;
}
I found the easiest way to do this was to just create your own action bar in a layout file. You can then use it by having your activities inherit from a common activity that overrides setContentView, where you just stick the view you're given into a parent view containing that and your action bar. You can mimick the overflowing menu behavior by using ActionMenuView (5.0+).
For example, assume you have an action_bar.xml with an ActionMenuView in it named menuView:
class BaseActivity extends Activity {
...
ActionMenuView mMenuView;
...
#Override
protected void setContentView(int resId) {
LayoutInflater inflater = getLayoutInflater();
View actionBar = inflater.inflate(R.layout.action_bar, null);
View contentView = inflater.inflate(resId);
mMenuView = (ActionMenuView)actionBar.findViewById(R.id.menuView);
RelativeLayout parentLayout = new RelativeLayout(this);
LayoutParams actionBarLayout = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
actionBar.setLayoutParams(actionBarLayout);
parentLayout.addView(actionBar);
actionBar.setId(R.id.actionBar);
LayoutParams contentLayout = new LayoutParams(LayoutParams.MATCH_PARENT, 0);
contentLayout.addRule(RelativeLayout.BELOW, actionBar.getId());
contentLayout.addRule(RelativeLayout.ALIGN_WITH_PARENT_BOTTOM);
contentView.setLayoutParams(contentLayout);
parentLayout.addView(contentView);
setContentView(parentLayout);
}
}
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 created an app and It has multiple themes and it changes dynamically. I mean in run-time user can choose different themes. so that UI components color changes in runtime. I got stuck in a strange problem. Problem is status bar color is not changing according to theme. statusBar always stays in initial theme color.
values-v21/style.xml is as below
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryDark">#color/primary_dark</item>
<item name="colorAccent">#color/accent</item>
<item name="android:textColorPrimary">#color/primary_text</item>
<item name="android:icon">#color/icons</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
</style>
values-v21/themes.xml is as below
===================================
<resources>
<style name="AppThemeRed" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#F44336</item>
<item name="colorPrimaryDark">#D32F2F</item>
<item name="colorAccent">#FFCDD2</item>
<item name="android:textColorPrimary">#FFFFFF</item>
<item name="android:icon">#color/icons</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
</style>
<style name="AppThemeAmber" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#FFC107</item>
<item name="colorPrimaryDark">#FFA000</item>
<item name="colorAccent">#FFEB3B</item>
<item name="android:textColorPrimary">#FFFFFF</item>
<item name="android:icon">#color/icons</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
</style>
<style name="AppThemeBlue" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#3F51B5</item>
<item name="colorPrimaryDark">#303F9F</item>
<item name="colorAccent">#448AFF</item>
<item name="android:textColorPrimary">#FFFFFF</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
</style>
<style name="AppThemePurple" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#9C27B0</item>
<item name="colorPrimaryDark">#7B1FA2</item>
<item name="colorAccent">#7C4DFF</item>
<item name="android:textColorPrimary">#FFFFFF</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
</style>
<style name="AppThemeYellow" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#FFEB3B</item>
<item name="colorPrimaryDark">#FBC02D</item>
<item name="colorAccent">#CDDC39</item>
<item name="android:textColorPrimary">#FFFFFF</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
</style>
</resources>
when user selects a theme from theme selection dialog i calling a function and setting theme for that activity, also recreating activity.
public static void onActivityCreateSetTheme(Activity activity)
{
switch (sTheme)
{
case 0:
activity.setTheme(R.style.AppThemeRed);
break;
case 1:
activity.setTheme(R.style.AppThemeAmber);
break;
default:
case 2:
activity.setTheme(R.style.AppTheme);
break;
case 3:
activity.setTheme(R.style.AppThemeBlue);
break;
case 4:
activity.setTheme(R.style.AppThemePurple);
break;
case 5:
activity.setTheme(R.style.AppThemeYellow);
break;
}
}
I am using NavigationView in drawer and drawer is coming below status bar no problem with that. But status bar is not picking up current theme's primaryDark-color. It always takes Default theme(startup theme) color only.
Setting the theme via setTheme(int resId) in the onCreate() method won't have any effect on the StatusBar since it is not part of the Activity itself afaik.
So in order to achieve the desired effect you need to do it programatically. Here's a snippet which resolves the R.attr.colorPrimaryDark and sets it for the StatusBar if the SDK-Level is above 21:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
setContentView(R.layout.activity_management);
// Further code
}
private void init(){
// Set the Theme
setTheme(R.style.MyAppTheme);
if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
getWindow().setStatusBarColor(getAttributeColor(R.attr.colorPrimaryDark));
}
}
// Resolve the given attribute of the current theme
private int getAttributeColor(int resId) {
TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(resId, typedValue, true);
int color = 0x000000;
if (typedValue.type >= TypedValue.TYPE_FIRST_COLOR_INT && typedValue.type <= TypedValue.TYPE_LAST_COLOR_INT) {
// resId is a color
color = typedValue.data;
} else {
// resId is not a color
}
return color;
}
Edit
This approach obviously overwrites the color which is set via android:statusBarColor. So if you're using a NavigationDrawer and you want that the Drawer goes "under" the StatusBar you have to set the StatusBar color to transparent manually as soon as the Drawer is opened:
Example
actionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar,
R.string.open_drawer_accessibility_desc,
R.string.close_drawer_accessibility_desc) {
#SuppressLint("NewApi")
#Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setStatusBarColor(
getResources().getColor(android.R.color.transparent));
}
}
/** Called when a drawer has settled in a completely open state. */
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
/** Called when a drawer has settled in a completely closed state. */
#Override
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
}
};
}
MainActivity.java
I've implemented MultiChoiceModeListener in this class and below is the code:
on listView:
listView.setMultiChoiceModeListener(MainActivity.this);
listView.setChoiceMode(listView.CHOICE_MODE_MULTIPLE_MODAL);
#Override
public boolean onActionItemClicked(ActionMode arg0, MenuItem arg1) {
switch (arg1.getItemId()) {
case R.id.save:
// Close CAB
arg0.finish();
return true;
case R.id.saveto:
// Close CAB
arg0.finish();
return true;
default:
return false;
}
}
#Override
public boolean onCreateActionMode(ActionMode arg0, Menu arg1) {
arg0.getMenuInflater().inflate(R.menu.save_menu, arg1);
return true;
}
#Override
public void onDestroyActionMode(ActionMode arg0) {
listadaptor.removeSelection();
}
#Override
public boolean onPrepareActionMode(ActionMode arg0, Menu arg1) {
return false;
}
#Override
public void onItemCheckedStateChanged(ActionMode arg0, int arg1, long arg2,
boolean arg3) {
final int checkedCount = listView.getCheckedItemCount();
arg0.setTitle(checkedCount + " "+getResources().getString(R.string.selected));
listadaptor.toggleSelection(arg1);
}
style.xml
<style name="AppTheme.Base" parent="Theme.AppCompat.Light">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/White</item>
<item name="android:windowNoTitle">true</item>
<item name="windowActionModeOverlay">true</item>
<item name="windowActionBar">false</item>
<item name="actionModeStyle">#style/LStyled.ActionMode</item>
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppTheme.Base">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<style name="LStyled.ActionMode" parent="#style/Widget.AppCompat.ActionMode">
<item name="background">#color/colorPrimary</item>
</style>
<style name="ActionBarThemeOverlay" parent="Theme.AppCompat.Light">
<item name="android:textColorPrimary">#fff</item>
<item name="colorControlNormal">#fff</item>
<item name="colorControlHighlight">#3fff</item>
</style>
<style name="HeaderBar">
<item name="android:background">#009688</item>
<item name="android:textStyle">bold</item>
</style>
<style name="ActionBarPopupThemeOverlay" parent="ThemeOverlay.AppCompat.Light">
<item name="android:textColor">#000</item>
</style>
below is my screenshots:
you can see both screenshots, in second screenshot, actionmode background is white and text color is also white.. i want to change it to first screenshots color which is in top.
You can change the ActionMode background through attribute actionModeStyle:
<style name="AppTheme.Base" parent="Theme.AppCompat.Light">
....
....
<item name="actionModeStyle">#style/LStyled.ActionMode</item>
</style>
<style name="LStyled.ActionMode" parent="#style/Widget.AppCompat.ActionMode">
<item name="background">#color/color_action_mode_bg</item>
</style>
You will of course need to define a color named color_action_mode_bg:
<color name="color_action_mode_bg">#009688</color>
There are other things you can change as well. Example:
<item name="titleTextStyle">...</item>
<item name="subtitleTextStyle">...</item>
<item name="height">...</item>
To change text color of SAVE and SAVETO, add the following to AppTheme.Base:
<item name="actionMenuTextColor">#color/color_action_mode_text</item>
use actionModeBackground in your AppTheme.Base style.
<item name="actionModeBackground">#color/colorPrimary </item> (or)
<item name="android:actionModeBackground">#color/colorPrimary </item>
To change the Title Color of contextual action bar this was the only method that worked for me:
Write this in your activity's theme
<item name="actionModeStyle">#style/ContextualActionModeTheme</item>
Define the styles
<style name="ContextualActionModeTheme" parent="#style/Widget.AppCompat.ActionMode">
<item name="titleTextStyle">#style/titleColor</item>
</style>
<style name="titleColor" parent="TextAppearance.AppCompat.Widget.ActionMode.Title">
<item name="android:textColor">#color/your_color_here</item>
</style>
The title color of your contextual action bar would be changed.
I am new to android, while customizing action bar i noticed that first it shows old(default) action bar first then it shows customized action bar. Also I want to set text color for action bar title.
My code snippet is
public class MainActivity extends ActionBarActivity {
MenuItem searchItem;
SearchView searchView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar ab = getSupportActionBar();
ab.setTitle("Modification"); ab.setIcon(R.drawable.ap);
ab.setBackgroundDrawable(new
ColorDrawable(Color.parseColor("#00AA00")));
ab.setSubtitle("Modification");
setContentView(R.layout.activity_main);
I think there must be some silly mistake.
Please help.
Try this one..
create a theme.xml as follows
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActivityTheme" parent="#android:style/Theme.Holo">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<!-- other activity and action bar styles here -->
</style>
<!-- style for the action bar backgrounds -->
<style name="MyActionBar" parent="#android:style/Widget.Holo.ActionBar">
<item name="android:background">#5477B1</item>
</style>
Then in your Androidmanifest.xml
mention this in application tag
<application
..
android:theme="#style/CustomActionBarTheme"
Good luck
try like this,
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar mActionBar = getActionBar();
mActionBar.setDisplayShowHomeEnabled(false);
mActionBar.setDisplayShowTitleEnabled(false);
LayoutInflater mInflater = LayoutInflater.from(this);
View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);
TextView mTitleTextView = (TextView) mCustomView.findViewById(R.id.title_text);
mTitleTextView.setText("My Own Title");
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);
}
or
Create a style for the action bar and use a custom background:
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActivityTheme" parent="#android:style/Theme.Holo">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<!-- other activity and action bar styles here -->
</style>
<!-- style for the action bar backgrounds -->
<style name="MyActionBar" parent="#android:style/Widget.Holo.ActionBar">
<item name="android:background">#drawable/background</item>
<item name="android:backgroundStacked">#drawable/background</item>
<item name="android:backgroundSplit">#drawable/split_background</item>
</style>
</resources>