2015-01-20 3 views
0

Я пытаюсь обновить цвет ImageView в своем AppWidget в моем приложении для Android.Обновление Android AppWidget ImageView src с цветом

У меня есть шестнадцатеричный десятичный знак для моего цвета, допустим, #ffffff.

Я отправляю широковещательную передачу от активности, запуская виджет onReceive. Цвет сохраняется в SharedPreferences. Это как AppWidget onReceive() выглядит следующим образом:

@Override 
public void onReceive(Context context, Intent intent) { 

    RemoteViews widgetView = new RemoteViews(context.getPackageName(), 
      R.layout.appwidget_homescreen); 
    AppWidgetManager appWidgetManager = AppWidgetManager 
      .getInstance(context); 

    // If the WidgetProvider was told to update the colors of the widget 
    if (intent.hasExtra("updateTheme")) { 
     SharedPreferences sharedPref = PreferenceManager 
       .getDefaultSharedPreferences(context); 
     String txtColorString = sharedPref.getString(
       PreferencesActivity.KEY_PREF_TXT_COLOR, ""); 
     int txtColor = Integer.parseInt(txtColorString); 

     //I'm setting the color of the two text views here, working as intended. 
     widgetView.setTextColor(R.id.tvAppWidgetArtist,txtColor 
       ); 
     widgetView.setTextColor(R.id.tvAppWidgetTitle, 
       txtColor); 

     //I want to set the color of the ImageView here, this is not working. 
     widgetView.setTextColor(R.id.ivAppWidgetHS, txtColor); 
     widgetView.setTextColor(R.id.ivAppWidgetVS, txtColor); 

     appWidgetManager.updateAppWidget(new ComponentName(context, 
       SongAppWidgetProvider.class), widgetView); 
    } 
    super.onReceive(context, intent); 
} 

Проблема заключается в том, что цвет не обновляется. Все, что он говорит, это «Виджет проблемной загрузки». Любые идеи, почему?

+0

и проблема? – Yazan

+0

@Yazan Проблема в том, что она не работает, я получаю «Проблемный загрузочный виджет» на рабочем столе – Marcus

ответ

2

Если вам нужно просто сплошное цветное изображение, вы можете просто создать его.

Bitmap b = Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888); // size doesn't matter 
    Canvas c = new Canvas(b);   
    c.drawColor(color); 
    widgetView.setImageViewBitmap(R.id.imageView, b); 
+0

Спасибо, что сработало – Marcus

0

сплошной цвет для widgetView

Bitmap bitmap = Bitmap.createBitmap(1,1, Bitmap.Config.ARGB_8888); 
bitmap.eraseColor(Color.DKGRAY); 
widgetView.setImageViewBitmap(R.id.imageView, bitmap); 
Смежные вопросы