0

I have a android.View.ContextMenu, which pops up after clicking an item in a recyclerview. It looks like this:

Screenshot

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>
abc
  • 2,285
  • 5
  • 29
  • 64

1 Answers1

0

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();
    }
}
Akshay Sood
  • 6,366
  • 10
  • 36
  • 59