1

Instead of Text, it's necessary to read the R.id.text text from TextView = My Text, I tried a lot but failed, how, please help.

This is the line:

public class MyWidgetProvider extends AppWidgetProvider {
// String array to show text at textview on refresh
private static final String[] refreshStrings = {"Text"};

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:text="My Text"
    android:textColor="@android:color/white"
    android:textSize="18sp" />

I want it to be like this:

private static final String[] refreshStrings = R.id.text;

Cule
  • 117
  • 8

1 Answers1

0

I found this How to find view id in appwidgetprovider?, so it is not possible to reference or modify in Appwidget directly. One solution could be to use AppCompatActivity. Here you can see how to update and modify with AppCompatActivity http://android4beginners.com/2013/06/lesson-1-3-how-to-modify-textview-in-java-code-findviewbyid-settext-and-gettext-methods/.

This is my code with AppCompatActivity how to get the text from the TextView by the R.id.<idname>

MainActivity:

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

    public class MainActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            TextView tv = (TextView) findViewById(R.id.text);
        }
    }

activity_main.xml (Don't forget to change the standard Layout, I chose LinearLayout):

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:text="My Text"
    android:textColor="@android:color/white"
    android:textSize="18sp" />
Max
  • 183
  • 1
  • 5
  • 18