stuckedunderflow
stuckedunderflow

Reputation: 3767

Adding View to RemoteViews in Android

Straight to the issue.

//This is my main layout
RemoteViews views = null;
views = new RemoteViews(getPackageName(), R.layout.main); 

//Clear parent view
views.removeAllViews(R.id.llMain);

//This is how we add nested view
RemoteViews childView = new RemoteViews(getPackageName(), R.layout.lay_child);
views.addView(R.id.llMain, childView);

The codes run well on Galaxy Nexus. But not on some other devices such Galaxy Tab or Motorola. What is wrong?

It returns error message something like "couldn't find view. Using error view instead." updateAppWidget couldn't find any view, using error view android.widget.RemoteViews$ActionException: can't find view: 0x7f050025

//This is the main layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#00000000"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/llMain"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"    
        android:orientation="vertical"
        android:layout_gravity="center|left"
        android:layout_marginLeft="@dimen/marginLeft">  

    </LinearLayout>             

</FrameLayout>

And this is the child view

lay_child.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/llTimezoned"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="visible"
    android:layout_gravity="center">                    
    <ImageView android:id="@+id/ivTimezone"
        android:layout_width="60dp"
        android:layout_height="60dp"
        />     
</LinearLayout>

Or is it something to do with the OS version?

Upvotes: 0

Views: 9245

Answers (1)

Dantalian
Dantalian

Reputation: 581

This worked for me

RemoteViews update = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
    for (int i = 0; i < 3; i++) {
        RemoteViews textView = new RemoteViews(context.getPackageName(), R.layout.test);
        textView.setTextViewText(R.id.textView1, "TextView number " + String.valueOf(i));
        update.addView(R.id.ll_widget, textView);
    }

    appWidgetManager.updateAppWidget(appWidgetIds, update);

https://stackoverflow.com/a/20040066/1492815

Upvotes: 2

Related Questions