diff --git a/core/api/current.txt b/core/api/current.txt index b7e98bea9293f..57e256142eb09 100644 --- a/core/api/current.txt +++ b/core/api/current.txt @@ -54884,6 +54884,7 @@ package android.widget { method public int describeContents(); method public int getLayoutId(); method public String getPackage(); + method @IdRes public int getViewId(); method @Deprecated public boolean onLoadClass(Class); method public void reapply(android.content.Context, android.view.View); method public void removeAllViews(@IdRes int); @@ -54944,6 +54945,7 @@ package android.widget { method public void setTextViewText(@IdRes int, CharSequence); method public void setTextViewTextSize(@IdRes int, int, float); method public void setUri(@IdRes int, String, android.net.Uri); + method public void setViewId(@IdRes int); method public void setViewLayoutHeight(@IdRes int, float, int); method public void setViewLayoutHeightDimen(@IdRes int, @DimenRes int); method public void setViewLayoutMargin(@IdRes int, int, float, int); diff --git a/core/java/android/widget/RemoteViews.java b/core/java/android/widget/RemoteViews.java index dfb2263a97a76..2328e589216bd 100644 --- a/core/java/android/widget/RemoteViews.java +++ b/core/java/android/widget/RemoteViews.java @@ -358,6 +358,13 @@ public class RemoteViews implements Parcelable, Filter { @ApplyFlags private int mApplyFlags = 0; + /** + * Id to use to override the ID of the top-level view in this RemoteViews. + * + * Only used if this RemoteViews is defined from a XML layout value. + */ + private int mViewId = View.NO_ID; + /** Class cookies of the Parcel this instance was read from. */ private Map mClassCookies; @@ -4799,6 +4806,9 @@ public class RemoteViews implements Parcelable, Filter { inflater = inflater.cloneInContext(inflationContext); inflater.setFilter(shouldUseStaticFilter() ? INFLATER_FILTER : this); View v = inflater.inflate(rv.getLayoutId(), parent, false); + if (mViewId != View.NO_ID) { + v.setId(mViewId); + } v.setTagInternal(R.id.widget_frame, rv.getLayoutId()); return v; } @@ -5746,4 +5756,25 @@ public class RemoteViews implements Parcelable, Filter { } return true; } + + /** + * Set the ID of the top-level view of the XML layout. + * + * Set to {@link View#NO_ID} to reset and simply keep the id defined in the XML layout. + * + * @throws UnsupportedOperationException if the method is called on a RemoteViews defined in + * term of other RemoteViews (e.g. {@link #RemoteViews(RemoteViews, RemoteViews)}). + */ + public void setViewId(@IdRes int viewId) { + if (hasMultipleLayouts()) { + throw new UnsupportedOperationException( + "The viewId can only be set on RemoteViews defined from a XML layout."); + } + mViewId = viewId; + } + + /** Get the ID of the top-level view of the XML layout, as set by {@link #setViewId}. */ + public @IdRes int getViewId() { + return mViewId; + } }