Search code examples
androidtextviewandroid-appwidget

Remote textview not changed by widget configuration activity android


I am making a widget for notepad and I am able to grab the title of notes from the database but now the only problem is I want to change the layout of the widget i.e the text in the layout.I have a listview in my configuration activity and when I click on a item the text in that item should become the text of textView in Widget.Now I am able to get that data for text but I am not able to set it. Heres the code for widget provider:-

public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
    // TODO Auto-generated method stub
    super.onUpdate(context, appWidgetManager, appWidgetIds);
    for (int i = 0; i < appWidgetIds.length; i++) {
        int appWidgetId = appWidgetIds[i];

        Intent intent = new Intent(context, NotesList.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                intent, 0);

        RemoteViews views = new RemoteViews(context.getPackageName(),
                R.layout.widget_layout);
        views.setOnClickPendingIntent(R.id.textViewWidget, pendingIntent);

        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
}

and for configuration widget:-

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    if (intent.getData() == null) {
        intent.setData(Notes.CONTENT_URI);
    }

    Uri notesUri = getIntent().getData();
    Cursor managedCursor = getContentResolver().query(notesUri,
            new String[] { Notes.TAGS, Notes.TITLE }, null, null, null);

    managedCursor.moveToFirst();
    do{
        Log.d("name", managedCursor.getString(1));
        name = managedCursor.getString(1);
        array.add(name);
        managedCursor.moveToNext();
    }while(!managedCursor.isLast());
    name = managedCursor.getString(1);
    array.add(name);

    adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,array);
    setListAdapter(adapter);

    setResult(RESULT_CANCELED);
    Bundle extras = getIntent().getExtras();

    if(extras != null){
        widgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,AppWidgetManager.INVALID_APPWIDGET_ID);
    }

    appWidgetManager = AppWidgetManager.getInstance(this);  
}

protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Uri notesUri = getIntent().getData();
    Cursor managedCursor = getContentResolver().query(notesUri,
            null, null, null, null);

    managedCursor.moveToFirst();
    managedCursor.moveToPosition(position);
    Log.d("Note", managedCursor.getString(9));
    String s = managedCursor.getString(9); 
    //s is the text I want to set & here since I need to change a different layout so I used layout inflator
    LayoutInflater inflate = getLayoutInflater();
    LinearLayout v1 = (LinearLayout) inflate.inflate(R.layout.widget_layout, null);
    text = (TextView)v1.findViewById(R.id.textViewWidget);
    //These are the changed needed to be done
            text.setBackgroundColor(Color.YELLOW);
    text.setText(s);
    //Toast.makeText(this, text.getText()+"", Toast.LENGTH_SHORT).show();   
    views = new RemoteViews(this.getPackageName(),
            R.layout.widget_layout);//This remote view is not updated

    Intent intent1 = new Intent(this, NotesList.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            intent1, 0);
    views.setOnClickPendingIntent(R.id.textViewWidget, pendingIntent);

    appWidgetManager.updateAppWidget(widgetId, views);

    Intent resultValue = new Intent();
    resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
    setResult(RESULT_OK,resultValue);
    finish();
}

The XML file for configuration activity:-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fastScrollEnabled="true" >
</ListView>

</LinearLayout>

and for widget:-

<?xml version="1.0" encoding="utf-8"?>
<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/textViewWidget"
    android:layout_width="96dp"
    android:layout_height="96dp"
    android:background="@android:color/white"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="@android:color/black" />

</LinearLayout>

could someone please have a look at my code and help


Solution

  • First of all I can't find your AppWidgetProvider class.

    Secondly in widget you can't get view using findViewById().In widget you can get view of TextView like below :

    You have to fetch layout of widget in RemoteViews like this that is also in onUpdate() of AppWidgetProvider:

     RemoteViews rvs = new RemoteViews(context.getPackageName(), R.layout.main);
    

    Now to set text in TextView of widget :

    rvs.setTextViewText(R.id.textViewWidget, "we updated! yay!");
    

    And if you have any further query please just check this very simple and effective tutorial on widget.