1

I'm trying to change the color of a linearview when a certain state happens in my live card. I added the colors I want in my colors.xml and I'm able to set them in the live card layout itself.

but is there a way to set the background from code? I saw that there is no .setbackground or anything like this.

So is there a way to do this or would I need to go with images?

TheUnknown
  • 50
  • 5

1 Answers1

1

You can try to give the layout id in the xml and then:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (mLiveCard == null) {
        mLiveCard = new LiveCard(this, LIVE_CARD_TAG);

        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.live_card);
        remoteViews.setInt(R.id.my_layout, "setBackgroundResource", android.R.color.holo_red_dark);            
        mLiveCard.setViews(remoteViews);

        // Display the options menu when the live card is tapped.
        Intent menuIntent = new Intent(this, LiveCardMenuActivity.class);
        mLiveCard.setAction(PendingIntent.getActivity(this, 0, menuIntent, 0));
        mLiveCard.publish(PublishMode.REVEAL);
    } else {
        mLiveCard.navigate();
    }
    return START_STICKY;
}

xml:

<LinearLayout
android:id="@+id/my_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/card_margin"
tools:context=".LiveCardService">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />
</LinearLayout>

Full project on GitHub.

pt2121
  • 11,720
  • 8
  • 52
  • 69
  • I tried `setBackgroundResource` myself but with that I got a sad cloud icon. With your code the view renders fine but it doesn't show any color at all. – TheUnknown Mar 10 '15 at 12:16
  • Have you tried setBackground or setBackgroundColor? Sorry dont have my Glass with me now – pt2121 Mar 10 '15 at 12:19
  • at this moment I've tried `setBackgroundResource` and `setBackground` which both gave me the sad cloud as result. `setBackgroundColor` didn't give me the sad cloud but the color wouldn't show. – TheUnknown Mar 10 '15 at 12:43
  • 1
    I just tried using `setBackgroundResource` again cause I figured maybe I made a typo or whatever. and no doubt it was working now. must have made a typo. however thanks for your response you where definitely in the right direction! – TheUnknown Mar 10 '15 at 12:52
  • @TheUnknown cool. I just had a chance to test it. setBackgroundResource works fine for me too. Thanks! – pt2121 Mar 10 '15 at 13:53