Reputation: 1227
There's some weird behavior here. I've got a bunch of code, pictures, followed by some description of the weird behavior and then my questions.
Files
I have a file random_start_bonus_cards_notification.xml
, which looks like:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/firstCard" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/secondCard" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ifNotCoal"
android:visibility="gone">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="("
android:paddingTop="15dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/if_not_coal"
android:paddingTop="15dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/thirdCard" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=")"
android:paddingTop="15dp" />
</LinearLayout>
And a file random_start_bonus_cards.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"></RelativeLayout>
This is a deliberately blank layout, because of weirdness we'll get into. It turns out this file has to be here, but it doesn't matter what it contains.
Annnnd my styles.xml
:
<resources>
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
</style>
<style name="NotificationTitle">
<item name="android:textColor">?android:attr/textColorPrimaryInverse</item>
<item name="android:textStyle">bold</item>
</style>
<style name="NotificationText">
<item name="android:textColor">?android:attr/textColorPrimaryInverse</item>
</style>
And some Android code:
private void showStartBonusCardsDialog(DrawnCards drawnCards) {
LayoutInflater factory = LayoutInflater.from(CalculatorActivity.this);
final View dialogContent = factory.inflate(R.layout.random_start_bonus_cards_notification, null);
((ImageView) dialogContent.findViewById(R.id.firstCard)).setImageResource(getDrawableIdForStartCard(drawnCards.firstCard()));
((ImageView) dialogContent.findViewById(R.id.secondCard)).setImageResource(getDrawableIdForStartCard(drawnCards.secondCard()));
if(drawnCards.hasThirdCard()) {
dialogContent.findViewById(R.id.ifNotCoal).setVisibility(View.VISIBLE);
((ImageView) dialogContent.findViewById(R.id.thirdCard)).setImageResource(getDrawableIdForStartCard(drawnCards.thirdCard()));
} else {
dialogContent.findViewById(R.id.ifNotCoal).setVisibility(View.GONE);
}
AlertDialog drawnCardsDialog = new AlertDialog.Builder(CalculatorActivity.this).create();
drawnCardsDialog.setView(dialogContent);
drawnCardsDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); }
});
drawnCardsDialog.show();
}
private void showStartBonusCardsNotification(DrawnCards drawnCards) {
Intent openIntent = new Intent(CalculatorActivity.this, CalculatorActivity.class);
openIntent.setAction(refreshStartCardsAction);
PendingIntent openPendingIntent = PendingIntent.getActivity(this, 0, openIntent, 0);
RemoteViews notificationView = new RemoteViews(getPackageName(), R.layout.random_start_bonus_cards_notification);
notificationView.setImageViewResource(R.id.firstCard, getDrawableIdForStartCard(drawnCards.firstCard()));
notificationView.setImageViewResource(R.id.secondCard, getDrawableIdForStartCard(drawnCards.secondCard()));
if(drawnCards.hasThirdCard()) {
notificationView.setViewVisibility(R.id.ifNotCoal, View.VISIBLE);
notificationView.setImageViewResource(R.id.thirdCard, getDrawableIdForStartCard(drawnCards.thirdCard()));
} else {
notificationView.setViewVisibility(R.id.ifNotCoal, View.GONE);
}
NotificationCompat.Builder notification = new NotificationCompat.Builder(this);
notification.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
notification.setSmallIcon(R.drawable.white);
notification.setContentIntent(openPendingIntent);
notification.setContent(notificationView);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int notificationId = 1;
notificationManager.notify(notificationId, notification.build());
}
And here's two screenshots to refer to:
Weirdness
I initially had just one layout file, since the layouts were identical. With this, the text colour of the dialog was black (good) and the text colour of the notification was white and couldn't be read on my HTC M9.
(1) If I put the XML from random_start_bonus_cards_notification.xml
into random_start_bonus_cards.xml
, and replace all usages of random_start_bonus_cards_notification
with random_start_bonus_cards
, the text colours are wrong. If I keep the code as-is, but delete random_start_bonus_cards
, the text colours are wrong. If I use random_start_bonus_cards_notification.xml
and keep random_start_bonus_cards.xml
in the project, even if it's empty, I get black text in both the dialog and notification!
(2) The background on the notification is a pale green. This colour was in the resource file at one point, but has been removed (as shown). After removing the background colour the dialog layout has a white background, but the notification background is still pale green, no matter whether I change it to red or anything.
(3) If I stop calling the dialog code, the same behavior occurs. I've included the dialog code since I'm worried there might be some interaction here, but I don't see how.
Questions
Generally, how does RemoteViews work with custom notification layouts? Is there any kind of system caching that could account for the background colour not changing?
Why is my notification not changing background colour when I change the layout background colour?
Why does my app change behavior when I delete this empty file random_start_bonus_cards.xml
, that isn't even being used?
Why does my notification text colour not work unless I use the file named random_start_bonus_cards_notification.xml
? (If I remove the _notification
, the text changes colour).
Thanks for reading!
Upvotes: 15
Views: 1553
Reputation: 1227
This was maddening, and is still not great.
(1) After confirming that my phone was doing some caching, I started using the emulator and it worked better.
(2) I left the notification with a separate layout files from the dialog, even though they are identical. My reasoning is that apparently the notification background can change between Android releases (there's a lot of complaints).
(3) I didn't post it above, but at one point I had a "v9/styles.xml". I didn't realize this applied to all versions above API9.
The final solution looks a bit like the following code. I copied from this SO answer (5.1.1 notification TextView, the correct way to deal with white text?) and used textAppearance instead of style.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/if_not_coal"
android:textAppearance="@style/NotificationText"
android:paddingTop="15dp" />
textAppearance is great.
<style name="NotificationText" parent="android:TextAppearance.StatusBar.EventContent" />
Gives the text a greyish colour for < API 21.
<style name="NotificationText" parent="@android:style/TextAppearance.Material.Notification.Title"></style>
Gives the text a black colour for API >= 21.
Upvotes: 3
Reputation: 6884
My guess is that this is happenning for one of the following two reasons:
The first of these could be that somewhere else in your code, you are refering to the creation of the notification, and there the changes you have made would not affect the code.
This is more likely to happen. The build has broken down and your built version of the app is not using the latest code. You can solve this by rebuilding the app from your IDE.
If you still can't manage to get hold of why this is happenning, set a breakpoint just before calling the method to display the notification and follow the logic the app is using.
Hope this helps :)
Upvotes: 6