I built a NavigationView using Java code and then I set the text style to keep the text of my menu items in a specific color and font using the method setItemTextAppearance(int resId). But this method is not working. My text is not changing.
Note : All the other methods are working great, like setItemTextColor(), setItemBackground() etc.
Here is my code:
mNavigationView = new NavigationView(context);
mNavigationView.setId(R.id.nav_view);
mNavigationView.setFitsSystemWindows(true);
// This method works great!
mNavigationView.setBackgroundColor(ContextCompat.getColor(context, R.color.colorPrimary));
// This too.
mNavigationView.setItemTextColor(ColorStateList.valueOf(ContextCompat.getColor(context, android.R.color.white)));
// This method is not working.
mNavigationView.setItemTextAppearance(R.style.MenuTextStyle);
Here is my style code:
<style name="MenuTextStyle">
<item name="android:textSize">16sp</item>
<item name="android:fontFamily">sans-serif-medium</item>
<item name="android:textStyle">normal</item>
<item name="android:textColor">#ffffff</item>
<item name="android:lineSpacingExtra">0sp</item>
</style>
I suppose making your style a descendant of the default style that is applied to NavigationView's items would work out:
<style name="MenuTextStyle" parent="TextAppearance.AppCompat.Medium">
<item name="android:textSize">22sp</item>
<item name="android:textStyle">italic</item>
<item name="android:textColor">#ff0000</item>
</style>
In Java code:
navigationView.setItemTextColor(null);
navigationView.setItemTextAppearance(R.style.MenuTextStyle);
Result
Related
I'm trying to apply a custom style to the material theme action bar. Nothing fancy, just a simple change of title text colour, however I can't get the colour to change at all. I've set up a theme an app theme as follows in the themes xml:
<resources>
<style name="AppTheme" parent="Base.AppTheme">
<item name="android:navigationBarColor">?android:attr/colorPrimaryDark</item>
<item name="actionBarStyle">#style/AppBarLayout</item>
</style>
<style name="Base.AppTheme" parent="Theme.MaterialComponents.DayNight">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryVariant">#color/colorPrimaryDark</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorSecondary">#color/colorSecondary</item>
<item name="colorSecondaryVariant">#color/colorSecondaryDark</item>
<item name="colorOnPrimary">#color/colorTextLight</item>
<item name="colorOnSecondary">#color/colorTextDark</item>
<item name="colorError">#color/colorError</item>
</style>
</resources>
The action bar style which is referenced is defined in the styles xml as:
<style name="AppBarLayout" parent="Widget.AppCompat.ActionBar">
<item name="background">?attr/colorPrimaryDark</item>
<item name="titleTextStyle">#style/AppBarText</item>
</style>
The style which titleTextStyle references is also defined in styles xml as:
<style name="AppBarText" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="android:textColor">#color/colorError</item>
</style>
I've tried other attributes in the latter style, such as:
<item name="android:titleTextStyle">#color/colorError</item>
<item name="titleTextStyle">#color/colorError</item>
<item name="colorOnPrimary">#color/colorError</item>
<item name="android:textColorPrimary">#color/colorError</item>
I've also tried applying the AppBarText style with other attributes to titleTextStyle, such as android:textAppearance. Alas - no luck.
How can I get this to change? Perhaps I'm misunderstanding how Material components work?
I am not sure if this will work but try this code once:
int labelColor = getResources().getColor(R.color.label_color);
String сolorString = String.format("%X", labelColor).substring(2); // !!strip alpha value!!
Html.fromHtml(String.format("<font color=\"#%s\">text</font>", сolorString), TextView.BufferType.SPANNABLE);
getSupportActionBar().setTitle(colorString);
I have inherited ListPopupMenu I used for show popup for items in ListView widget.
I use this code for creation and displaying my popup:
CADropDownPopupList popupMenu = new CADropDownPopupList(getActivity());
popupMenu.setAnchorView(view);
...
popupMenu.show();
I have styled listPopupMenu attribute in styles using this code:
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
...
<item name="android:dialogTheme">#style/NoTitleAlertDialogTheme</item>
<item name="listPopupWindowStyle">#style/PopupMenu</item>
<item name="android:listPopupWindowStyle">#style/PopupMenu</item>
...
</style>
<style name="PopupMenu" parent="Widget.AppCompat.ListPopupWindow">
<item name="android:layout_marginLeft">16dp</item>
<item name="android:layout_marginRight">16dp</item>
<item name="android:paddingLeft">16dp</item>
<item name="android:paddingRight">16dp</item>
<item name="android:textSize">40sp</item>
<item name="android:popupBackground">#drawable/popup_background</item>
</style>
But no one style item maps to my popup. What did I do wrong?
Change your theme
Use PipupMenu insted of ListPopupWindow
<style name="PopupMenu.Example" parent="#style/Widget.AppCompat.Light.PopupMenu">
<item name="android:popupBackground">#drawable/menu_dropdown_panel_example</item>
</style>
Because ListPopupWindow style not available check issue :
http://code.google.com/p/android/issues/detail?id=58023
I too struggled to get styles working, but found that you can manually set properties directly on the ListPopupWindow after creating it.
val popup = ListPopupWindow(context, null, R.attr.listPopupWindowStyle).apply {
setBackgroundDrawable(ContextCompat(context, R.drawable.bg_popup_menu))
// ... etc ...
}
<style name="GCTheme" parent="Theme.Sherlock.Light">
<item name="android:textSize">18sp</item>
<item name="android:textColor">#FFFFFF</item>
<item name="actionDropDownStyle">#style/MyDropDown</item>
<item name="popupMenuStyle">#style/PopupMenu.Example</item>
</style>
<style name="MyDropDown" parent="Widget.Sherlock.Spinner.DropDown.ActionBar">
<item name="android:popupBackground">#7D7D7D</item>
</style>
<style name="PopupMenu.Example" parent="#style/Widget.Sherlock.Light.ListPopupWindow">
<item name="android:popupBackground">#7D7D7D</item>
</style>
The following code styles my action bar such that the title of the action bar is white and the action bar is bluish. The problem that I am faced with now is that, since I am using the Theme.Sherlock.Light, the settings on options menu will not display properly.
Setting in the option menu extends PreferenceActivity and consists of the following code,
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.perferences);
}
The main problem here is that, the textColor for the action bar is set to be white and also the background that appears on this page is white, making the text impossible to read ... Does anyone have an idea so as to solve this problem.
I did try using this code
View v = new View(this);
v.setBackgroundColor(Color.GRAY);
in the settings activity, but it did not work.
So instead of using <item name="android:textColor">#FFFFFF</item> to set your title's color to white you should use this :
<style name="Theme.MyApplications" parent="#style/Theme.Sherlock.Light.DarkActionBar">
<item name="actionBarStyle">#style/ActionBar.Solid</item>
</style>
<style name="ActionBar.Title" parent="TextAppearance.Sherlock.Widget.ActionBar.Title">
<item name="android:textColor">#android:color/white</item>
</style>
<style name="ActionBar.SubTitle" parent="TextAppearance.Sherlock.Widget.ActionBar.Subtitle">
<item name="android:textColor">#android:color/white</item>
</style>
<style name="ActionBar.Solid" parent="#style/Widget.Sherlock.Light.ActionBar.Solid">
<item name="titleTextStyle">#style/ActionBar.Title</item>
<item name="subtitleTextStyle">#style/ActionBar.SubTitle</item>
</style>
This should do the trick for you.
I've trying to work around this on the actionbar but from What I can see the title is basicly a TextView but I need to place an image instead of plain text. I try to set it up into the Style themes.xml as this:
<item name="android:actionBarStyle">#style/MyActionBar</item>
And then I defined this style
<style name="MyActionBar" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#drawable/action_background</item>
<item name="android:titleTextStyle">#style/ActionbarTitleStyle</item>
</style>
I thought that I could defined the background for the title but even If I do is not what I want, try the getActionBar().setCustomView(layout); but happend that my actionbar is null before the setContentView is being called and I need it in a Base class.
PD. I only need to care about HONEYCOMB and Higher, so if you please not suggest Sherlock or Jonhanilson implementations.
if you want an image, you can specify the app logo along with the icon (it will be used instead of icon):
<application
android:icon="#drawable/ic_launcher"
android:logo="#drawable/logo"
android:label="#string/app_name"
android:name=".AppName"
>
Now you have an arbitrary image instead of your lanucher icon. What you need to do next is to get rid of the text. In your styles do something like:
<style name="Widget.Styled.ActionBar" parent="Widget.Sherlock.ActionBar">
<item name="titleTextStyle">#style/NoTitleText</item>
<item name="android:titleTextStyle">#style/NoTitleText</item>
<item name="subtitleTextStyle">#style/NoTitleText</item>
<item name="android:subtitleTextStyle">#style/NoTitleText</item>
</style>
<style name="NoTitleText">
<item name="android:textSize">0sp</item>
<item name="android:textColor">#00000000</item>
</style>
You should use both item name="titleTextStyle" and item name="android:titleTextStyle" if you use ActionBarSherlock if I remember well.
Override setContentView like this:
#Override
public void setContentView(int resLayoutId) {
super.setContentView(resLayoutId);
// TODO Set your actionbar changes here
}
OR, you can do this:
<style name="MyActionBar" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#drawable/action_background</item>
<item name="android:titleTextStyle">#style/ActionbarTitleStyle</item>
<item name="android:displayOptions">showCustom</item>
<item name="android:customNavigationLayout">#layout/custom_nav_layout</item>
</style>
I have a style that includes textColor, textSize, textStyle and typeface. When applied directly to an EditText widget, the color of the text is as specified (as well as the other attributes), but when applied as a theme to the activity or the entire application, the size is fine but the color is not applied. What I am missing?
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="fap" parent="android:Theme.Holo">
<item name="android:textSize">24sp</item>
<item name="android:textColor">#FF0000</item>
<item name="android:textStyle">normal</item>
<item name="android:typeface">normal</item>
</style>
</resources>
This is quite simple : you are not overriding android default style, your just creating a new one which extends android:Widget.EditText. Thus, the style is not applied.
To correct this, into your theme definition, just add :
<item name="android:editTextStyle">#style/fap</item>
Now, each time Android instanciate an EditText, when it load default style values, it will find your fap style.
Edit:
searching through android's source code is very usefull. Check https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/values/attrs.xml
https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/values/themes.xml
https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/values/styles.xml
https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/widget/EditText.java
for example.
EditText widget just can't get these parameters from an activity theme. It gets its default style from the android:editTextStyle parameter of the activity theme. So you have to create your own style:
<style name="MyEditText" parent="android:Widget.EditText">
<item name="android:textSize">24sp</item>
<item name="android:textColor">#FF0000</item>
<item name="android:textStyle">normal</item>
<item name="android:typeface">normal</item>
</style>
And then set it as EditText style in the activity theme:
<style name="fap" parent="android:Theme.Holo">
<item name="android:editTextStyle">#style/MyEditText</item>
</style>
Hope this will work because I haven't tried this code.