Gabriele Pia Gabriele Pia - 1 year ago 62
Android Question

How to get text of a TextView of a Widget from an Activity?

I have a widget and an activity, when I start the activity, I have to read text from the TextView of the widget and show it in the TextWidget of the activity.

My relevant code:

String frase="";
TextView text_frase = (TextView) findViewById(;
if (text_frase != null){
frase = (String) text_frase.getText();
Log.v(LOG_CLASS_NAME, "frase: "+frase);

debugging it - I have
as null (I think I can't reference to a view object from another view.) Anyway, how could I do this?

Answer Source

No, you can't. I suggest one of this ways:

  1. Adding your text to a Bundle and fetching it in your activity.

in your widget:

Intent intent = new Intent(context, YourActivity.class);
Bundle b= new Bundle();
b.putString("Text", yourtext);
PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetId, intent, PendingIntent.FLAG_CANCEL_CURRENT);

in your activity:

Intent intent=getIntent();
Bundle b= intent.getExtras();
String text= b.getString("Text", "");
  1. Using SharedPreferences for saving your text and loading it in the activity.

In your situation, i recommend method 1.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download