I have built a widget that contains a button and an editable TextView
(that behaves like an EditText) much like the native Google Search bar
widget. How can I access the text that the user has typed into the
TextView? In an Activity I would just use findViewById(), but of
course I can't do that in the AppWidgetProvider. (I get the error
message "The method findViewById(int) is undefined for the type
Widget.")

So I tried to solve it by using a million casts, like so:

                String str = (String) ((TextView)((Activity)
context).findViewById(R.id.txt_input)).getText();

Unfortunately, with that line present my app crashes. What is the
correct way to get references to those Widget elements? I've read all
about RemoteViews and Widgets and looked for examples where somebody
does this (which seems like it would be common, but I guess not) --
but I haven't turned up a solution. What am I missing?

Thanks!

S

PS - In case it's relevant, the code in question is in onReceive():

public void onReceive(Context context, Intent intent)
    {
        super.onReceive(context, intent);
        String action = intent.getAction();
        if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action))
        {

                String str = (String) ((TextView)((Activity)
context).findViewById(R.id.txt_input)).getText();

                RemoteViews views = new RemoteViews
(context.getPackageName(), R.layout.widget);
                Intent browsingIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.cnn.com";));
                browsingIntent.addCategory(Intent.CATEGORY_BROWSABLE);
                browsingIntent.setComponent(new ComponentName
("com.android.browser", "com.android.browser.BrowserActivity"));
                PendingIntent pendingIntent = PendingIntent.getActivity
(context, 0, browsingIntent, 0);
                views.setOnClickPendingIntent(R.id.btn_go,
pendingIntent);
                AppWidgetManager.getInstance(context).updateAppWidget
(intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS),
views);

        }
    }

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to