Skip to content
t28hub edited this page Mar 24, 2014 · 1 revision

ウィジェットに関するのTipsをまとめます。

  1. ウィジェットの生成

ウィジェットの生成

ウィジェットを生成するにはShadowAppWidgetManagerを用います。
createWidget()メソッドでウィジェットを作成し、その識別子をもとにViewを取得します。

サンプルコード:

// ShadowAppWidgetManagerの生成
Context context = Robolectric.application
		.getApplicationContext();
AppWidgetManager manager = 
		AppWidgetManager.getInstance(context);
ShadowAppWidgetManager shadowManager = 
		Robolectric.shadowOf(manager);
		
// Widgetの生成
int widgetId = shadowManager.createWidget(
		MyWidgetProvider.class, 
		R.layout.activity_main);
		
// Viewの取得
View widgetView = shadowManager.getViewFor(widgetId);

...