2

I just begin programming with android home widget, so i could set text to a textview in home widget from my widget provider by settextviewtext() but when i want to get text from it, i have no ideal. Can not use findViewById() as normal as in activity and then getText(). Is there any way to get text of textview in home widget from widget provider? Or show me how to get from another activity?

Kiradev
  • 347
  • 2
  • 17
  • 4
    You should get in the habit of marking correct answers in questions you ask; people will be more inclined to give you good answers if you do. – Cat Jun 28 '12 at 02:19
  • thanks, i will do. It's great ideal for me. – Kiradev Jun 28 '12 at 03:16

3 Answers3

7

CommonsWare in this question says:

You can't. App widgets are write-only: you can push data to them, but you cannot read them.

Instead, when you update your app widget with new text, you will need to store that text somewhere, perhaps in a file.

Community
  • 1
  • 1
hasanghaforian
  • 13,858
  • 11
  • 76
  • 167
0

read this for widget tutorial and what you can do with it.

Nguyen Minh Binh
  • 23,891
  • 30
  • 115
  • 165
  • yes, i have already read that tutorial. But there is no ideal about my problem surely. But i have recognized i am little stupid for trying to get text from textview, because it's myself that set text to it :)). thanks every body. – Kiradev Jun 28 '12 at 03:08
0
  1. Where you instantiate App Widget layout and showing, write this:

     SharedPreferences settings = 
     PreferenceManager.getDefaultSharedPreferences(context);
     @SuppressLint("CommitPrefEdits")
     SharedPreferences.Editor  editor = settings.edit();
     String rowid = "500"; 
     editor.putString("appWidgetRowId", rowid);
     editor.apply();
    
  2. Then get that rowid (it means 500), write this in AppWidgetProvider:

    @Override
         public void onReceive(final Context context, Intent intent) {
             super.onReceive(context, intent);
    
     SharedPreferences settings = 
     PreferenceManager.getDefaultSharedPreferences(context);
                 @SuppressLint("CommitPrefEdits")
                 SharedPreferences.Editor  editor = settings.edit();
    
                 final String rowId= settings.getString("appWidgetRowId", null);
                 Toast toast = Toast.makeText(context, "Opening....  "+rowId, Toast.LENGTH_SHORT);
                 toast.setGravity(Gravity.CENTER|Gravity.BOTTOM, 0, 0);
                 toast.show();
     }
    

You can call this second method onWidget click option.

halfer
  • 19,824
  • 17
  • 99
  • 186
Noor Hossain
  • 1,620
  • 1
  • 18
  • 25