Apply style to a RemoteView?

3.5k views Asked by At

I use two applications. One exposes a RemoteView via an AIDL interface. The second uses a ListView and custom adapter to present the RemoteView.

With a very simple view with one layout and one TextView, the TextView is white in the Listview.

All applications use the same light style.

Is it possible to apply a style to a RemoteView ? Or, how it's possible to manage the style of a RemoteView instance ?

Thanks

3

There are 3 answers

2
Isaiah On

RemoteView does not support changing themes. And then only way you can do is keep two layout files with same layout and different themes(like different font colors), and before you update appWidget, you can choose any one of the layouts as the RemoteView

0
pprados On

It' possible to apply a style with a RemoteView.

final View view=remoteViews.apply(new ContextWrapper(mContext)
{
  public Context createPackageContext(String packageName, int flags) throws NameNotFoundException
  {
    return new ContextWrapper(getBaseContext().createPackageContext(packageName, flags))
    {
      // Delegate the theme to the context
      @Override
      public Theme getTheme()
      {
        return mContext.getTheme();
      }
    };
  }
}, parent);
0
Spoom On

I solved this issue by creating one layout file for the RemoteView (in my case, I was creating a template for a custom notification) and two styles.xml files, one in values, another in values-v21. That way, I could apply the Material styles for Lollipop and up, and the normal styles for previous Android versions.

If you're trying to match a system style of some kind, you can take a look at the Android core system styles for a reference on how they're put together. I would especially look at attrs.xml since you can use some of those with the automatic theme syntax, e.g.

<style name="mediaNotificationTitle">
    <item name="android:textColor">?android:attr/textColorPrimary</item>
    <item name="android:textAppearance">?android:attr/textAppearanceMedium</item>
</style>

Then inside the layout, just refer to it with style=@style/mediaNotificationTitle.