Skip to content

Instantly share code, notes, and snippets.

@yerenutku
Last active November 6, 2017 07:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yerenutku/101c87a7fdcbcab030686013564b3ece to your computer and use it in GitHub Desktop.
Save yerenutku/101c87a7fdcbcab030686013564b3ece to your computer and use it in GitHub Desktop.
public class ConfigurableWidgetConfigureActivity extends Activity {
int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
private EditText etUrl;
private Button btAdd;
private AppWidgetManager widgetManager;
private RemoteViews views;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setResult(RESULT_CANCELED);
// activity stuffs
setContentView(R.layout.activity_widget_configure);
etUrl = (EditText) findViewById(R.id.etUrl);
// These steps are seen in the previous examples
widgetManager = AppWidgetManager.getInstance(this);
views = new RemoteViews(this.getPackageName(), R.layout.configurable_widget);
// Find the widget id from the intent.
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
}
if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
finish();
return;
}
btAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Gets user input
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(etUrl.getText().toString()));
PendingIntent pending = PendingIntent.getActivity(ConfigurableWidgetConfigureActivity.this, 0, intent, 0);
views.setOnClickPendingIntent(R.id.ivWidget, pending);
widgetManager.updateAppWidget(mAppWidgetId, views);
Intent resultValue = new Intent();
// Set the results as expected from a 'configure activity'.
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();
}
});
}
}
@gotoark
Copy link

gotoark commented Nov 6, 2017

add btAdd=(Button)findViewById(R.id.btAdd); at line 15 to avoid Null Pointer Error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment