Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set text of Android RemoteViews inside layout when include it multiple times

I refer to this question: How do I access the views inside the layout when I reuse it multiple times?

I have the following two layouts:

<!-- layout_to_include.xml -->
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

and

<!-- widget.xml -->
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include
        android:id="@+id/included_layout_1"
        layout="@layout/layout_to_include"/>

    <include
        android:id="@+id/included_layout_2"
        layout="@layout/layout_to_include"/>

</LinearLayout>

Normally, you can access the TextViews programmatically inside the included layout like this:

LinearLayout l1 = (LinearLayout) findViewById(R.id.included_layout_1);
((TextView) l1.findViewById(R.id.text_view)).setText("test1");

LinearLayout l2 = (LinearLayout) findViewById(R.id.included_layout_2);
((TextView) l2.findViewById(R.id.text_view)).setText("test2");

But in my case, I have an Android AppWidget which can only accessed via RemoteViews:

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
views.setTextViewText(R.id.text_view, "test1");

This will only change the text of the first TextView.

I have not find any solution, so my question is whether is it possible to set the text of the TextView within multiple included layouts.

like image 543
lewen Avatar asked Oct 21 '25 14:10

lewen


1 Answers

Can be done.

Remove your includes; you need to add programmatically (no other way AFAIK), so add id (e.g. LinearLayout) to your widget's linearlayout, and:

RemoteViews one = new RemoteViews(getPackageName(), R.layout.layout_to_include);
one.setTextViewText(R.id.text_view, "tv-ONE"); // <---
remoteViews.addView(R.id.LinearLayout, one);

RemoteViews two = new RemoteViews(getPackageName(), R.layout.layout_to_include);
two.setTextViewText(R.id.text_view, "tv-TWO"); // <---
remoteViews.addView(R.id.LinearLayout, two);
like image 105
hundreAd Avatar answered Oct 24 '25 02:10

hundreAd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!