1

I've seen other answers such as this one and this one that explain the correct way to add a new View to a RemoteViews object. In the first link, it is specified that there should be a nested LinearLayout inside an outer LinearLayout such that the inner layout is referenced in the addView method, e.g. addView(innerLayoutID, view).

Why reference the inner layout instead of the outer one? Does addView not work with the outer layout or is this just personal opinion?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
yuyu5
  • 365
  • 4
  • 11

1 Answers1

2

It turns out the answer in the first link was generally correct but I couldn't run it exactly as it was posted, so I needed more details in order to get it running in my project. I posted my code below for future reference.

Note that a very important that wasn't specified in the other threads is that rv.addView() required the same layout as specified in the rv object; that is to say, I couldn't have a LinearLayout nested within another LinearLayout and then reference the inner LinearLayout. I had to reference the outer one since that is what was referenced in the parent rv instance.

My onUpdate() is specific to Samsung Look's Edge Panel API, but the general format is the same:

@Override
public void onUpdate(Context context, SlookCocktailManager manager, int[] cocktailIds) {
    //Parent RemoteViews object
    RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.main_view);
    for (int i = 0; i < someObjectArray.length; i++) {
        //Create new remote view using the xml file that represents a list entry
        RemoteViews listEntryLayout = new RemoteViews(context.getPackageName(), R.layout.list_entry);
        //Set the text of the TextView that is inside the above specified listEntryLayout RemoteViews
        listEntryLayout.setTextViewText(R.id.stock_text, someObjectArray[i].getName());
        //Add the new remote view to the parent/containing Layout object
        rv.addView(R.id.main_layout, listEntryLayout);
    }
    //standard update
    if (cocktailIds != null) {
        for (int id : cocktailIds) {
            manager.updateCocktail(id, rv);
        }
    }
}

main_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:importantForAccessibility="2"
    android:orientation="vertical" >
</LinearLayout>

list_entry.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/stock_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/stock_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:background="@android:color/holo_green_light"
        android:text="TextView"
        android:textSize="20dp"/>
</LinearLayout>
yuyu5
  • 365
  • 4
  • 11