0

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

I Bajwa PHD
  • 1,708
  • 1
  • 20
  • 42
hitman_93
  • 116
  • 9
  • please also post your xml file of widget layout. Where is your listview?? – baldguy Feb 15 '13 at 10:43
  • XML codes posted for listview of widgetConfig file as well as widget file. – hitman_93 Feb 15 '13 at 10:49
  • Toast made in onListItemClick does show me the changed value of textView but as soon as the config activity finishes the widget shows the original layout. – hitman_93 Feb 15 '13 at 10:56
  • please notify me whether this helps you or not. – baldguy Feb 15 '13 at 11:13
  • Please have a look at widget provider class also I have added it – hitman_93 Feb 15 '13 at 11:30
  • Just add rvs.setTextViewText(R.id.textViewWidget, "Set text here"); before views.setOnClickPendingIntent(R.id.textViewWidget, pendingIntent); Set your selected text in static variable and pass it as second parameter of setTextViewText.. – baldguy Feb 15 '13 at 11:35
  • OMG this static variable thing really worked I tried it without using static string but it did'nt work why is it so ? ANd last thing what could I do to change background color of this textView ? – hitman_93 Feb 15 '13 at 11:40
  • May be setImageViewResource() help.. But i am not sure about it.You need static variable because the value of that variable not accessible outside that class. By defining static you can access that. – baldguy Feb 15 '13 at 11:47

1 Answers1

1

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.

baldguy
  • 2,090
  • 1
  • 16
  • 25
  • OK thank-you Rahil for sorting out my problem but here comes a new one my Widget Text is not updated when I change my notes in Application itself – hitman_93 Feb 15 '13 at 11:47
  • I have given in widget xml file to update after 1000 ms so technically onupdate methood of widget provider class should be called after this much time ? So now from the widgetId how would I know which list item was clicked in config file so that I should update that in provider class – hitman_93 Feb 15 '13 at 11:56
  • you just follow tutorial I gave in my answer. Hope you can clear all your issues. – baldguy Feb 15 '13 at 11:59
  • ok thanks would do so and could you please vote up my question if you think this would be helpful to others . – hitman_93 Feb 15 '13 at 12:03