I have a android.View.ContextMenu, which pops up after clicking an item in a recyclerview. It looks like this:
My problem is, that I don't know how to change the color of the header (here: 12345678) and the divider.
I tried to create a PopupMenuStyle, but I'm only able to change the items, not the header. I use Theme.AppCompat.Light.DarkActionBar as a parent for my theming
EDIT 1:
With this I can set the headertextcolor to primary text color, but the divider still stays blue, regardless of which items I add.
<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
...
</style>
EDIT 2:
Context Menu-creation
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.my_menu, menu);
menu.setHeaderTitle("12345678");
}
Styling
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar" parent="#style/AppTheme">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:alertDialogTheme">#style/AppCompatAlertDialogStyle</item>
</style>
<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<!-- I tried a lot here, but nothing works -->
</style>
Changing Divider Color:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.dialog)
.setIcon(R.drawable.ic)
.setMessage(R.string.dialog_msg);
Dialog d = builder.show();
int dividerId = d.getContext().getResources().getIdentifier("android:id/titleDivider", null, null);
View divider = d.findViewById(dividerId);
divider.setBackgroundColor(getResources().getColor(R.color.my_color));
Changing Text Title Color:
public static void brandAlertDialog(AlertDialog dialog) {
try {
Resources resources = dialog.getContext().getResources();
int color = resources.getColor(...); // your color here
int alertTitleId = resources.getIdentifier("alertTitle", "id", "android");
TextView alertTitle = (TextView) dialog.getWindow().getDecorView().findViewById(alertTitleId);
alertTitle.setTextColor(color); // change title text color
int titleDividerId = resources.getIdentifier("titleDivider", "id", "android");
View titleDivider = dialog.getWindow().getDecorView().findViewById(titleDividerId);
titleDivider.setBackgroundColor(color); // change divider color
} catch (Exception ex) {
ex.printStackTrace();
}
}
Related
I was trying to change Picker default settings to android , So I was changing android default settings (since it can't be doned with css).
My problem is: Large strings wasnt showed by picker. So I make this changes at my styles.xml:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:windowBackground">#drawable/screen</item>
<item name="android:spinnerItemStyle">#style/SpinnerItem</item>
<item name="android:spinnerDropDownItemStyle">#style/SpinnerDropDownItem</item>
</style>
<style name="SpinnerItem" parent="Theme.AppCompat.Light.NoActionBar">>
</style>
<style name="SpinnerDropDownItem" parent="Theme.AppCompat.Light.NoActionBar">>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:inputType">textMultiLine</item>
<item name="android:maxLines">3</item>
</style>
With this done, picker now wrap lines. But it isnt selectable anymore. I've tried to use android:clickable true, but hasnt worked.
What can I do to fix it?
Override getDropDownView() method in ArrayAdapter and put setSingleLine(false) in post method of view. So when view completely created it wraps the text to appropriate lines.
#Override
public View getDropDownView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = new TextView(_context);
}
TextView item = (TextView) convertView;
item.setText("asddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd");
final TextView finalItem = item;
item.post(new Runnable() {
#Override
public void run() {
finalItem.setSingleLine(false);
}
});
return item;
}
this is my style.xml
Now when i click the option menu. the text color appears to be black. i need to change it to white
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/redDark</item>
<item name="colorPrimaryDark">#color/redLite</item>
<item name="colorAccent">#color/redDark</item>
<item name="actionMenuTextColor">#color/redDark</item>
</style>
Try this code
<item name="android:actionMenuTextColor">#color/your_color</item>
Check if this works
<item name="android:itemTextAppearance">#style/myMenuTextApearance</item>
and add this text style in the styles
<style name="myMenuTextApearance" parent="#android:style/TextAppearance.Widget.IconMenu.Item">
<item name="android:textColor">#android:color/primary_text_dark</item>
</style>
You can change menu items text color programmatically.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main_activity, menu);
for(int i = 0; i < menu.size(); i++) {
MenuItem item = menu.getItem(i);
SpannableString spanString = new SpannableString(menu.getItem(i).getTitle().toString());
spanString.setSpan(new ForegroundColorSpan(Color.GREEN), 0, spanString.length(), 0); //fix the color to white
item.setTitle(spanString);
}
return super.onCreateOptionsMenu(menu);
}
Hope it helps:)
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>
How to center vertical the "Login" title?
This is the style:
<style name="ABGAlertDialog" parent="#android:style/Theme.DeviceDefault.Dialog">
<item name="android:textColor">#android:color/black</item>
<item name="android:textColorHint">#android:color/darker_gray</item>
<item name="android:background">#color/corporativo_red</item>
<item name="android:windowTitleStyle">#style/MyAlertDialogTitle</item>
</style>
<style name="MyAlertDialogTitle">
<item name="android:background">#color/corporativo_red</item>
<item name="android:colorBackground">#color/corporativo_red</item>
<item name="android:textColor">#android:color/white</item>
<item name="android:textStyle">bold</item>
<item name="android:maxLines">1</item>
<item name="android:textSize">20sp</item>
<item name="android:bottomOffset">5sp</item>
</style>
I've tried gravity, textAlignment and some others items with no result.
You can't centre a normal alert dialog's title. You will need to create a custom dialog preferably with a dialog fragment or dialog activity depending on the functionality you need.
Set your text style in the android:textAppearance
<style name="MyAlertDialogTitle">
<item name="android:background">#color/corporativo_red</item>
<item name="android:colorBackground">#color/corporativo_red</item>
<item name="android:bottomOffset">5sp</item>
<item name="android:textAppearance">#style/YourTextStyle</item>
</style>
Change both text and background color in title bar (Android)?
Textview: using the dialog title style: #android:style/TextAppearance.DialogWindowTitle
If Alert dialog is normal without any of your custom layout or theme than you ca get object by passing id with android namespace
AlertDialog.Builder alertDialogBuild = new AlertDialog.Builder(getActivity());
alertDialogBuild.setMessage("message text align center");
alertDialogBuild.setTitle("Title text Align Center");
alertDialogBuild.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialog = alertDialogBuild.show();
TextView title = (TextView) dialog.findViewById(getActivity().getResources().getIdentifier("alertTitle", "id", "android"));
title.setGravity(Gravity.CENTER);
TextView message = (TextView) dialog.findViewById(getActivity().getResources().getIdentifier("message", "id", "android"));
message.setGravity(Gravity.CENTER);
static void centerTitleHorizontally(final TextView title)
{
ViewTreeObserver vto = title.getViewTreeObserver();
vto.addOnGlobalLayoutListener (new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
try {
title.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} catch (NoSuchMethodError x) {
title.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
Rect bounds = new Rect();
title.getPaint().getTextBounds(title.getText().toString(), 0, title.getText().length(), bounds);
title.setPadding((title.getWidth() - bounds.width()) / 2, 0, 0, 0);
}
});
}
I'm using Drop-down Navigation with my Action bar, and I can't get a sensible color for the corresponding text when using a dark action bar. The action bar itself is a dark grey, and the text color is black, so it's very hard to read.
I followed the basic instructions on the Action Bar developer's guide so my code is simply
SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.action_list, android.R.layout.simple_spinner_dropdown_item);
ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {
#Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
// TODO
return false;
}
};
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
actionBar.setListNavigationCallbacks(mSpinnerAdapter, navigationListener);
and my styles.xml was simply
<resources>
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
</style>
</resources>
I think that should have given me something sensible, but as the following image shows, it comes out as black text on a dark grey background so it's no good.
I've tried playing around with the styles.xml file to try and solve the problem. My best attempt was the following
<resources>
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:actionDropDownStyle">#style/myActionDropDownStyle</item>
<item name="android:actionBarStyle">#style/myActionBar</item>
</style>
<style name="myActionDropDownStyle" parent="android:style/Widget.Holo.Light.Spinner">
<item name="android:background">#color/LightCyan</item>
</style>
<style name="myActionBar" parent="#android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:titleTextStyle">#style/myActionBar.titleTextStyle</item>
</style>
<style name="myActionBar.titleTextStyle" parent="#android:style/TextAppearance.Holo.Widget.ActionBar.Title.Inverse">
<item name="android:textColor">#color/Orange</item>
</style>
</resources>
which was (1) able to change the text color of the app title to Orange, (2) able to change the background color of the selected navigation item in the action bar to a light blue, (3) able to change the background color of the dropdown so that at least the black text is shown against a light background. However nothing I do changes the text color itself, either for the selected item shown in the action bar, or for the items shown in the dropdown. My best attempt also looks horrible :-(!
All I want is a dark action bar with a sensible text color for the selected item shown in the action bar, which should be the same text color used when an item is being selected, and with the background color for the dropdown when an item is being selected being the same color as the action bar. But I can't work out how to do it, can anyone help?
FYI, I'm using Eclipse with an adt-bundle that I downloaded very recently (adt-bundle-windows-x86_64-20130219). When I search for solutions, I find a lot about the Sherlock action bar, however the styles for Sherlock weren't included in that adt-bundle. In any case, surely what I want should be possible with the Holo.Light.DarkActionBar that I'm using?
Please forgive my necromancy but this really bugged me today and the solution is actually quite quick and requires no custom adapters or styling.
If you change the context in the SpinnerAdapter's constructor to use getActionBar().getThemedContext() rather than this it will show light text on a dark background that will match the Action Bar's style.
SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(getActionBar().getThemedContext(), R.array.action_list, android.R.layout.simple_spinner_dropdown_item);
I've worked out that I can alter the colors for the drop-down Navigation in code, by creating my own subclass of ArrayAdapter as follows:
public class myArrayAdaptor<T> extends ArrayAdapter<T> {
public myArrayAdaptor(Context context, int textViewResourceId, T[] objects) {
super(context, textViewResourceId, objects);
}
public static myArrayAdaptor<CharSequence> createFromResource(Context context, int textArrayResId, int textViewResId) {
CharSequence[] strings = context.getResources().getTextArray(textArrayResId);
return new myArrayAdaptor<CharSequence>(context, textViewResId, strings);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
return SetColourWhite(super.getView(position, convertView, parent));
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return SetColourWhite(super.getView(position, convertView, parent));
}
private View SetColourWhite(View v) {
if (v instanceof TextView) ((TextView)v).setTextColor(Color.rgb(0xff, 0xff, 0xff)); // white
return v;
}
}
Then with the simple styles.xml
<resources>
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
</style>
</resources>
I can substitute the class myArrayAdaptor
SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.action_list, android.R.layout.simple_spinner_dropdown_item)
and the text color will always be white. But isn't there a simpler way of doing this using settings in the styles.xml file?
Java code
SpinnerAdapter adapter = ArrayAdapter.createFromResource(this,
R.array.actions,R.layout.dropdown_textcolor);
// OnNavigationListener
OnNavigationListener callback = new OnNavigationListener() {
String[] items = getResources().getStringArray(R.array.actions);
public boolean onNavigationItemSelected(int position, long id) {
// Do stuff when navigation item is selected
Log.d("NavigationItemSelected", items[position]); // Debug
return true;
}
};
ActionBar actions = getSupportActionBar();
actions.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
actions.setDisplayShowTitleEnabled(false);
actions.setListNavigationCallbacks(adapter, callback);
Xml dropdown_textcolor.xml
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:textColor="#android:color/white"
android:layout_height="wrap_content"
android:padding="10dp"
android:ellipsize="marquee"
android:textAlignment="inherit"/>
Add this in your styles.xml that will change the color of your hidden action menu text:
<style name="AppTheme.AppCompat.Light" parent="#android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:itemTextAppearance">#style/MenuTextAppearance</item>
</style>
<style name="MenuTextAppearance">
<item name="android:textColor">#android:color/black</item>
</style>
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<!-- Customize your theme here. -->
<item name="android:textColor">#ffffff</item>
<item name="android:itemBackground">#991005</item>
<item name="android:actionBarSize">40dp</item>
<item name="android:divider">#ff0000</item>
<item name="android:dividerHeight">1dp</item>
<item name="android:textSize">14sp</item>
</style>
</resources>
If you are using AppCompat support library .. this might help you..
<style name="MyActionBar3" parent="#style/Widget.AppCompat.Light.ActionBar">
<item name="android:background">#color/ActionBarBackground</item>
</style>
<style name="MyActionBarDropDownStyle" parent="">
<item name="android:background">#00FF00</item>
<item name="android:divider">#ff0000</item>
<item name="android:dividerHeight">1dp</item>
<item name="android:textColor">#FFFFFF</item>
<item name="android:textSize">10sp</item>
</style>
<style name="MyTextStyle" parent="#style/Widget.AppCompat.Light.Base.ListPopupWindow">
<item name="android:popupBackground">#FF0000</item>
</style>