App Widget Configuration Activity - How can I access my widget Views? - android

I created a widget configuration file which is started when my app widget is placed on the screen. However, I don't know how I can access the button from my widget layout to make changes on it (In this example I want to change the color/tint)
public class ExampleAppWidgetConfig extends AppCompatActivity {
private int appWidgetId;
private RadioGroup radioGroupColor;
private RadioButton radioButtonDefault;
private RadioButton radioButtonGreen;
private RadioButton radioButtonRed;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_example_app_widget_config);
Intent configIntent = getIntent();
Bundle extras = configIntent.getExtras();
if (extras != null) {
appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
}
radioGroupColor = findViewById(R.id.radio_group_color);
radioButtonDefault = findViewById(R.id.radio_button_default);
radioButtonGreen = findViewById(R.id.radio_button_green);
radioButtonRed = findViewById(R.id.radio_button_red);
}
public void confirmConfiguration(View v) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
RemoteViews views = new RemoteViews(this.getPackageName(), R.layout.example_widget);
views.setOnClickPendingIntent(R.id.example_widget_button, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
setResult(RESULT_OK, resultValue);
finish();
}
}

Related

Android widget ListView item clicks not working

So I'm trying to set up a widget with a list but clicks aren't working, so I understand to do this I need a template pending intent as individual items cannot have their own, and then add to the pending intent template using a fill in intent, I've added all this to my app but it still isn't working, I've followed advice here and here but when I click on an item its just highlighted to show it has registered a click and does nothing but I do get this in the
logcat
08-06 17:02:08.001 2163-3666/? W/Binder: Binder call failed.
java.lang.NullPointerException: Attempt to read from field
'com.android.server.appwidget.AppWidgetServiceImpl$ProviderId
com.android.server.appwidget.AppWidgetServiceImpl$Provider.id'
on a null object reference
at
com.android.server.appwidget.AppWidgetServiceImpl$AppWidgetManagerLocal
.getHostedWidgetPackages(AppWidgetServiceImpl.java:4800)
at
com.android.server.accessibility.AccessibilityManagerService$SecurityPolicy
.resolveValidReportedPackageLocked(AccessibilityManagerService.java:4637)
at com.android.server.accessibility.AccessibilityManagerService
.sendAccessibilityEvent(AccessibilityManagerService.java:531)
at android.view.accessibility.IAccessibilityManager$Stub
.onTransact(IAccessibilityManager.java:71)
at android.os.Binder.execTransact(Binder.java:690)
so here is my code, my onUpdate method calls a static update widget method that defines my template pending intent, and my onReceive method
AppWidgetProvider
public class MessageMeAppWidget extends AppWidgetProvider {
private static final String TAG = "MsgMeWdgt";
#Override
public void onReceive(Context context, Intent intent) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
if (intent.getAction().equals(Constants.LAUNCH_ACTIVITY)) {
int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
//String recipientId = intent.getStringExtra(Constants.FS_ID);
//String recipientName = intent.getStringExtra(Constants.FS_NAME);
Intent i = new Intent(context, MessageListActivity.class);
Bundle b = new Bundle();
//b.putString(Constants.FS_ID, recipientId);
//b.putString(Constants.FS_ID, recipientName);
i.putExtras(b);
Bundle bundle = intent.getExtras();
String toastText = "position is " + bundle.getInt(Constants.LAUNCH_ACTIVITY);
Toast.makeText(context,toastText,Toast.LENGTH_SHORT).show();
//context.startActivity(i);
}else if(intent.getAction().equals(Constants.DONT_LAUNCH_ACTIVITY)){
//Toast.makeText(context,"",Toast.LENGTH_SHORT).show();
}
super.onReceive(context, intent);
}
public static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
SharedPreferences sharedPreferences = context.getSharedPreferences(Constants.SHARED_PREFS,Context.MODE_PRIVATE);
boolean userSignedIn = sharedPreferences.getBoolean(Constants.SIGNED_IN, false);
if (userSignedIn){
//move everything in here after i know its working
}
Intent intent = new Intent(context, MessagingService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_list_view);
//populates data
remoteViews.setRemoteAdapter(R.id.widget_list,intent);
//remoteViews.setRemoteAdapter(appWidgetId, R.id.widget_list, intent);
remoteViews.setEmptyView(R.id.widget_list,R.id.empty);
Intent launchIntent = new Intent(context, WidgetAdapter.class);
launchIntent.setAction(Constants.LAUNCH_ACTIVITY);
launchIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent launchPendingIntent = PendingIntent.getBroadcast(context, 0, launchIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setPendingIntentTemplate(R.id.widget_list, launchPendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
Log.d(TAG,"on update");
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
Then my widget service is standard
public class MessagingService extends RemoteViewsService {
private String TAG = "MsgSrvc";
#Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
Log.d(TAG,"item clicked");
return new WidgetAdapter(this.getApplicationContext(), intent);
}
}
and then in the getViewAt method of my widget adapter I fill my fill in intent like this
Bundle extras = new Bundle();
extras.putInt(Constants.LAUNCH_ACTIVITY, position);
Intent fillInIntent = new Intent();
fillInIntent.putExtras(extras);
remoteViews.setOnClickFillInIntent(R.id.holder, fillInIntent);
in my manifest I'm registering my widget service and the receiver
<receiver android:name="com.sealstudios.aimessage.MessageMeAppWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/message_me_app_widget_info" />
</receiver>
<service android:name=".MessagingService"
android:permission="android.permission.BIND_REMOTEVIEWS" />
any help appreciated
i fixed this it wasthe intent was pointing to the wrong class so
Intent launchIntent = new Intent(context, WidgetAdapter.class);
should have been
Intent launchIntent = new Intent(context, MessageMeAppWidget.class);

How to open another view in a widget that stays on the homescreen?

I was using the Guardian news app and noticed that their widget had an options button that opened anther menu. I am not sure how this is done. If anyone has any ideas that would great.
Guardian Widget
Options menu open
This easily way it's open a dialog or a modal pop-up activity.
You can send an Intent as usual:
public class YourWidget extends AppWidgetProvider {
static PendingIntent getPendingSelfIntent(Context context, String action, int appWidgetId) {
KLog.d(TAG, "getPendingSelfIntent appWidgetId = " + appWidgetId);
Intent intent = new Intent(context, YourWidget.class);
Bundle b = new Bundle();
b.putInt("appWidgetId", appWidgetId);
intent.putExtras(b);
intent.setAction(action);
PendingIntent pendInt = PendingIntent.getBroadcast(context, appWidgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
return pendInt;
}
//....
//your method for remote views
public void doUpdateView() {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.collection_widget);
remoteViews.setOnClickPendingIntent(R.id.button_settings, getPendingSelfIntent(context, RUN_CLICKED, appWidgetId));
}
#Override
public void onReceive(final Context context, Intent intent) {
super.onReceive(context, intent);
if (RUN_CLICKED.equals(intent.getAction())) {
Intent actIntent = new Intent(context, YourActivity.class);
actIntent.putExtra("your_extra_key", your_extra_value);
actIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(actIntent);
}
//...
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
}

AppWidgets transform into Google Calendar and Clock widgets after reboot

I have no idea why this is happening. I'm making widgets which show the best currency rate for the currencies that the user chooses. I've noticed after a reboot, they sometimes turn into a clock and a calendar widget. Here's my code. I assume it could be something with my Intents or OnReceive() but I'm not sure what.
public class LightRowWidget extends RowWidget {
List<CurrencyObject> currencyObjects = new ArrayList<>();
public LightRowWidget() {
super(R.layout.widget_row_layout, R.color.white, Constants.FULL_OPACITY, R.color.black);
}
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
if (appWidgetIds != null && appWidgetIds.length > 0) {
for (int appWidgetId : appWidgetIds) {
// Create currencyObjects. They're used to assign the correct value
// to the correct texView
CurrencyObject currObj1 = new CurrencyObject(
R.id.currency1, R.id.currency1_buy, R.id.currency1_sell);
currencyObjects.add(currObj1);
CurrencyObject currObj2 = new CurrencyObject(R.id.currency2, R.id.currency2_buy, R.id.currency2_sell);
currencyObjects.add(currObj2);
// Register an onClickListener to Update the current widget on a click
Intent clickIntent = new Intent(context, LightRowWidget.class);
clickIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
onUpdate(context, appWidgetManager, appWidgetId, currencyObjects, clickIntent);
}
//start an intent that will schedule the alarm for the next update
Intent startIntent = new Intent(context, LightRowWidget.class);
scheduleNextUpdate(context, startIntent, appWidgetIds);
}
and Here is my RowWidget class with the relevant code.
public abstract class RowWidget extends AppWidgetProvider implements WidgetInterface{
public RowWidget(int layout, int background_color, int background_opacity, int text_color){
super();
this.layout = layout;
this.background_color=background_color;
this.background_opacity=background_opacity;
this.text_color=text_color;
}
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int appWidgetId, List<CurrencyObject> currencyObjects, Intent clickIntent) {
theAppWidgetManager = appWidgetManager;
//Get the current name of class
String currentClass = this.getClass().getName();
switch(currentClass){
case("foobar"):
configure_class=Constants.FOUR_ROW_CONFIGURE;
break;
case("foo"):
configure_class=Constants.ROW_APP_WIDGET_LIGHT_CONFIGURE;
break;
case("bar"):
configure_class=Constants.ROW_APP_WIDGET_DARK_CONFIGURE;
break;
case("barfoo"):
configure_class=Constants.TWO_ROW_CONFIGURE;
break;
}
// There may be multiple widgets active, so update all of them
// Get the preferred Currencies
Set<String> preferredCurrencies = AppWidgetConfigure.loadCurrencyPref(context,appWidgetId, configure_class);
// Inflate the layout
RemoteViews view = new RemoteViews(context.getPackageName(), layout);
// if the preferred Currencies have been declared already
if(preferredCurrencies!= null){
// Set the currencies for each object
for(String currency: preferredCurrencies){
if(currencyCount<currencyObjects.size()){
currencyObjects.get(currencyCount).setCurrencyType(currency);
currencyCount+=1;
}
}
}
else{
for(CurrencyObject curObj:currencyObjects){
curObj.setCurrencyType("RUB");
}
}
// Open up the Currency fragment on the click of the widget
Intent configIntent = new Intent(context, MainActivity.class);
configIntent.putExtra("LaunchCurrency", true);
configIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);
view.setOnClickPendingIntent(R.id.row_widget_layout, configPendingIntent);
// Update the widget whenever refresh button clicked
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
view.setOnClickPendingIntent(R.id.refresh, pendingIntent);
makeRequest(context, theDate, view, currencyObjects, appWidgetId);
currencyCount = 0;
}
//Handle receiving the intent
#Override
public void onReceive(Context context, Intent intent){
if (intent.getAction().equals(ACTION_APPWIDGET_UPDATE)) {
AppWidgetManager manager = AppWidgetManager.getInstance(context);
// Get id's
int[] allWidgetIds = intent
.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
onUpdate(context, manager, allWidgetIds);
}
else{
super.onReceive(context, intent);
}
}
EDIT: Widgets transform to a lot of different widgets that are already on my screen.

app widget not starting main activity after click on widget

I want to create an app widget with a confguration activity, the problem is when I add the widget to home the configuration activity is shown but when I click on button config the main activity does not start.... when I click on the Widget nothing happens. There are no errors in my Main activity. because if I start the app, it works without any errors...
I don't know what I m doing wrong :(. Thank you very much for your help...
here is the code:
public class WidgetConfiguration extends Activity{
final Context context = this;
EditText url;
EditText password;
int mAppWidgetId;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setResult(RESULT_CANCELED);
initializeAppWidget();
showConfigDlg();
final Context context = WidgetConfiguration.this;
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
}
private void initializeAppWidget(){
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();
}
}
private void showConfigDlg(){
setContentView(R.layout.widget_config);
((Button) findViewById(R.id.saveBtn)).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Log.d("blablabla", "setting the onClick1");
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
appWidgetManager.updateAppWidget(mAppWidgetId, views);
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();
}
});
}
}
public class WidgetProvider extends AppWidgetProvider
{
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
final int N = appWidgetIds.length;
Log.d("blablabla", "setting the onClick2");
// Perform this loop procedure for each App Widget that belongs to this provider
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
Log.d("blablabla", "setting the onClick3");
// Create an Intent to launch ExampleActivity
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
// Get the layout for the App Widget and attach an on-click listener
// to the button
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
views.setOnClickPendingIntent(R.id.button, pendingIntent);
// Tell the AppWidgetManager to perform an update on the current app widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
}

Start my application by clicking on widget

I'm trying How do I run an Activity from a button on a widget in Android? in order to launch my application on click on an image button. When i un-comment the code it switches the images but when i try to use this code it does nothing. I'd like to kick off my application instead.
public class MyWidgetIntentReceiver extends BroadcastReceiver {
private static int clickCount = 0;
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("pl.looksok.intent.action.CHANGE_PICTURE")){
updateWidgetPictureAndButtonListener(context);
}
}
private void updateWidgetPictureAndButtonListener(Context context) {
Log.i("PROJECTCARUSO", "updateWidgetPictureAndButtonListener");
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
// remoteViews.setImageViewResource(R.id.widget_button, getImageToSet());
//
// //REMEMBER TO ALWAYS REFRESH YOUR BUTTON CLICK LISTENERS!!!
// remoteViews.setOnClickPendingIntent(R.id.widget_button, MyWidgetProvider.buildButtonPendingIntent(context));
//
// MyWidgetProvider.pushWidgetUpdate(context.getApplicationContext(), remoteViews);
Intent intentClick = new Intent(context, FragmentChange.class);
PendingIntent pendingIntent = PendingIntent.getActivity (context, 0,
intentClick, 0);
remoteViews.setOnClickPendingIntent (R.id.widget_button, pendingIntent);
}
private int getImageToSet() {
clickCount++;
return clickCount % 2 == 0 ? R.drawable.app_icon : R.drawable.energy;
}
}
Here's what i finally got to work:
public class WidgetProvider extends AppWidgetProvider {
public static String SWITCH_WIDGET_UPDATE = "MainActivity.Switch";
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Log.i("PROJECTCARUSO","onUpdate");
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
remoteViews.setOnClickPendingIntent(R.id.widget_button, buildButtonPendingIntent(context));
Intent intentClick = new Intent(context, FragmentChange.class);
PendingIntent pendingIntent = PendingIntent.getActivity (context, 0, intentClick, 0);
remoteViews.setOnClickPendingIntent (R.id.widget_button, pendingIntent);
pushWidgetUpdate(context, remoteViews);
}
public static PendingIntent buildButtonPendingIntent(Context context) {
Intent intent = new Intent();
intent.setAction("CHANGE_PICTURE");
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
public static void pushWidgetUpdate(Context context, RemoteViews remoteViews) {
ComponentName myWidget = new ComponentName(context, WidgetProvider.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(myWidget, remoteViews);
}
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
super.onReceive(context, intent);
}
}

Categories

Resources