This is my first time asking a question on SO.
I want to pass my Calculation object to the widget to be displayed on the home screen. I have the widget to update in my MainActivity.java when I click the calculate button:
I'm using Parceler.
Here is my widget after clicking the Calculate button
And the MainActivity
My issue is that the widget is not displaying the calculation info that was supposedly passed to it from the Provider class. it seems to parcelize correctly in my logs that display the calculation, just that I may be setting the widget view incorrectly to receive the data and display it.
public void saveCalcInfo(View v){
[...]
setWidget(calculation);
}
private void setWidget(Calculation calculationToPassToWidget) {
BullAppWidgetProvider.manualUpdateWidgets(getApplicationContext(), calculationToPassToWidget);
}
All I really need from the widget is to receive the calculation info, and display it.
A nice to have would be upon clicking the button "View", launch the MainActivity and autofill the EditTexts with the displayed information about the calculation
Here's my BullAppWidgetProvider.java
public class BullAppWidgetProvider extends AppWidgetProvider {
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Calculation calculation) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_provider);
if (calculation != null) {
views.setTextViewText(R.id.symbol, calculation.getSymbol());
views.setTextViewText(R.id.shares, String.valueOf(calculation.getShares()));
views.setTextViewText(R.id.buy, String.valueOf(calculation.getBuy()));
views.setTextViewText(R.id.sell, String.valueOf(calculation.getSell()));
views.setTextViewText(R.id.comm, String.valueOf(calculation.getComm()));
}
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, appWidgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.viewCalcBT, pendingIntent);
views.setTextViewText(R.id.widget_app_title, context.getString(R.string.widget_prompt));
appWidgetManager.updateAppWidget(appWidgetId, views);
}
static void manualUpdateWidgets(Context context, Calculation calculation) {
ComponentName componentName = new ComponentName(context, BullAppWidgetProvider.class);
Timber.w("Calculation received in manual Update: %s", calculation.toString());
AppWidgetManager manager = AppWidgetManager.getInstance(context);
int[] appWidgetIds = manager.getAppWidgetIds(componentName);
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, manager, appWidgetId, calculation);
Toast.makeText(context, "Widget has updated!", Toast.LENGTH_LONG).show();
}
}
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId, null);
}
}
}
If needed, here's the
widget_layout.xml
file for reference
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:background="#color/primary">
<TextView
android:id="#+id/symbol"
style="#style/AdapterStyle"
android:text="#string/ipl" />
<TextView
android:id="#+id/shares"
style="#style/AdapterStyle"
android:layout_alignParentEnd="true"
android:text="#string/_42" />
<TextView
android:id="#+id/buy"
style="#style/AdapterStyle"
android:layout_below="#id/symbol"
android:text="#string/buy" />
<TextView
android:id="#+id/sell"
style="#style/AdapterStyle"
android:layout_below="#id/buy"
android:text="#string/sell" />
<TextView
android:id="#+id/comm"
style="#style/AdapterStyle"
android:layout_below="#id/shares"
android:layout_alignParentEnd="true"
android:text="#string/comm" />
<Button
android:id="#+id/viewCalcBT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/sell"
android:layout_alignParentEnd="true"
android:text="#string/view" />
</RelativeLayout>
as well as the
widget_provider.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="8dp">
<TextView
android:id="#+id/widget_app_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/app_name" />
<
android:id="#+id/ll_parent_for_widget"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Thank you for your time!
Related
I have a AppWidgetProvider like this, how can I set selection for this stackview on click on "Next event" ?
And please don't care about these lines below, my english too bad to describe more"
It looks like your post is mostly code; please add some more details.
It looks like your post is mostly code; please add some more details.
It looks like your post is mostly code; please add some more details."
enter image description here
void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.event_app_widget);
Intent serviceIntent = new Intent(context,ItemWidgetService.class);
serviceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,appWidgetId);
serviceIntent.setData(Uri.parse(serviceIntent.toUri(Intent.URI_INTENT_SCHEME)));
views.setRemoteAdapter(R.id.stackorlistview,serviceIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
AppWidgetManager appWidgetManagera = AppWidgetManager.getInstance(context);
appWidgetManagera.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.stackorlistview);
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/widget_back"
android:padding="#dimen/widget_margin"
android:orientation="vertical">
<TextView
android:id="#+id/titleview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="0dp"
android:background="#drawable/title_widget"
android:gravity="center"
android:padding="10dp"
android:text="Next event"
android:textColor="#FFF"
app:layout_constraintTop_toTopOf="parent" />
<StackView
android:paddingBottom="15dp"
android:paddingLeft="15dp"
android:background="#drawable/item_widget_background"
android:layout_marginTop="10dp"
android:loopViews="true"
android:id="#+id/stackorlistview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
I've created a widget, and on click an Activity has to be launched, I followed the answer to this question [Clickable widgets in android].
However, on click I am unable to get any response, here is the java code to my widget:
Widget.java
public class Widget extends AppWidgetProvider
{
private static String YOUR_AWESOME_ACTION = "YourAwesomeAction";
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
Intent intent = new Intent(context, HomeTeamSelector.class);
intent.setAction(YOUR_AWESOME_ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
views.setOnClickPendingIntent(R.id.widgetLayout, pendingIntent);
}
#Override
public void onReceive(Context context, Intent intent)
{
super.onReceive(context, intent);
if (intent.getAction().equals(YOUR_AWESOME_ACTION))
{
Intent x = new Intent(context,HomeTeamSelector.class);
context.startActivity(x);
}
}
}
This is the code to the xml file:
widget.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/widgetLayout"
android:layout_height="match_parent"
android:transitionGroup="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name_extra"
android:id="#+id/textView"
android:layout_centerInParent="true"
android:textColor="#color/colorPrimary"
android:textSize="30sp" />
</RelativeLayout>
The widget is rendered, however I am unable to get any response on click, what is possibly causing this, and how to fix?
try to add AppWidgetProvider receiver in AndroidMenifest.xml like
<receiver
android:name=".Widget"
android:label="#string/widget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/widget" />
</receiver>
I am setting a repeated alarm in the Android, which fires the activity.
This activity is having two buttons: "stop" and "snooze". When I click on "snooze" alarm is postponed to 10 minutes.
But the alarm is in slider window, i.e it is in onresume state.
I want to stop the alarm completely on both the buttons.
public class RingAlarm extends Activity {
MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ring_alarm);
Intent in;
in = getIntent();
String s=in.getStringExtra("habbit_id");
int col= in.getIntExtra("habbit_Color",0);
ScrollView sv = (ScrollView)findViewById(R.id.sv_ra);
if(col<0)
{
sv.setBackgroundResource(R.color.red);
}
if(col>0) {
sv.setBackgroundResource(R.color.green);
}
if(col==0)
{
sv.setBackgroundResource(R.color.yellow);
}
System.out.println("alarm string" + s);
TextView tv = (TextView)findViewById(R.id.habbit_ra);
tv.setText(s);
// mp = MediaPlayer.create(getApplicationContext(), R.raw.song1);
//mp.start();
}
public void onthecancelringclicked(View view){
//mp.stop();
int id=Process.myPid();
finish();
Process.killProcess(id);
//System.exit(0);
}
public void onthesnoozeringclicked(View view){
//mp.stop();
long l=System.currentTimeMillis();
Intent i = this.getIntent();
Random r = new Random();
int ri =r.nextInt(1000000)+64;
PendingIntent pi = PendingIntent.getActivity(this,ri,i,0 );
AlarmManager am = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,l+600000,pi);
finish();
Process.killProcess(Process.myPid());
}
}
And my XML file is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:background="#color/background"
>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="#string/app_name"
android:gravity="left|center"
android:padding="7dp"
android:background="#color/user_profile"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/sv_ra">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:id="#+id/habbit_ra"
android:text="habbit_name"/>
<AnalogClock
android:text="time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:layout_gravity="center"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="bsy your self"
android:layout_marginTop="20dp"
android:gravity="center"
android:id="#+id/habbit_message_ra"/>
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="0"
android:background="#color/user_profile">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="stop"
android:background="#color/user_profile"
android:onClick="onthecancelringclicked"/>
<Button
android:background="#color/user_profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="snooze 10 min"
android:onClick="onthesnoozeringclicked"/>
</LinearLayout>
As you can see I have implemented Process.killProcess(pid) method. Still my alarm is getting saved into the navigation drawer. I want to remove the activity from everywhere.
i am using same intent for 32 alarms so i can not put the id of the pending intent to the intent as putExtra.
Hopefully this will give you an idea of how to cancel/stop the alarm:
Intent intent = new Intent(context, MyClass.class);
PendingIntent recurring = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if(active) { //If alarm is active
alarms.cancel(recurring);
recurring.cancel();
} else {
// Do something..
}
I copied it from one of my projects and it works for me.
add the following lines in the entry of the activity that is fired in the menifest file.
android:excludeFromRecents="true"
this will not let the activity go into recents and calling finish will lead to end of the activity.
I've developed a news android app, this app uses a MediaPlayer class to stream newscast tracks from SoundCloud and everything works fine. Now let's say the user pressed the play button from inside the app and the music starts playing. Now the user pressed the hardware home button and the app went to the background or the user pressed the sleep button to turn off the screen. I have managed to let the music playing in the background but i want the user to be able to control the MediaPlayer from outside the app (pause, stop, next, prev actions).
I searched a lot and didn't find anything, How can i do that?
I didn't put my code because i think it's irrelevant Please tell me if you want to see the code.
Thanks in advance.
I've done something similar by adding a custom notification with images/buttons to control the audio play and channel from the Android notification bar.
I think this is what you are after.
Notification code:
public Notification buildNotification()
{
Intent intent = new Intent(mContext, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
setContentView();
Notification.Builder notibuilder = new Notification.Builder(mContext);
notibuilder.setContentTitle(" ");
notibuilder.setContentText(" ");
notibuilder.setSmallIcon(R.drawable.ic_launcher);
notibuilder.setOngoing(true);
notibuilder.setAutoCancel(false);
notibuilder.setContentIntent(pIntent);
notibuilder.setContent(mContentView);
notibuilder.setTicker(null);
mNotification = notibuilder.build();
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
return mNotification;
}
public void setContentView()
{
mContentView = new RemoteViews(mContext.getPackageName(), R.layout.custom_notification);
mContentView.setTextViewText(R.id.notificaiton_app_title, "");
String currentChannel = " ";
if ( mCurrentChannel != -1)
currentChannel = " " + (mCurrentChannel + 1);
mContentView.setTextViewText(R.id.notificaiton_app_channel, currentChannel);
Intent previousIntent = new Intent(mContext, NotificationButtonIntentService.class);
previousIntent.setAction(NotificationButtonIntentService.Action_Previous);
mContentView.setOnClickPendingIntent(R.id.notification_layout_previous, PendingIntent.getService(mContext, 0, previousIntent, PendingIntent.FLAG_CANCEL_CURRENT));
mContentView.setOnClickPendingIntent(R.id.notification_image_previous, PendingIntent.getService(mContext, 0, previousIntent, PendingIntent.FLAG_CANCEL_CURRENT));
Intent playIntent = new Intent(mContext, NotificationButtonIntentService.class);
if ( mCurrentChannel != -1)
{
playIntent.setAction(NotificationButtonIntentService.Action_Pause);
mContentView.setInt(R.id.notification_image_play_pause, "setBackgroundResource", R.drawable.ic_media_pause);
}
else
{
playIntent.setAction(NotificationButtonIntentService.Action_Play);
mContentView.setInt(R.id.notification_image_play_pause, "setBackgroundResource", R.drawable.ic_media_play);
}
mContentView.setOnClickPendingIntent(R.id.notification_layout_play_pause, PendingIntent.getService(mContext, 0, playIntent, PendingIntent.FLAG_CANCEL_CURRENT));
mContentView.setOnClickPendingIntent(R.id.notification_image_play_pause, PendingIntent.getService(mContext, 0, playIntent, PendingIntent.FLAG_CANCEL_CURRENT));
Intent nextIntent = new Intent(mContext, NotificationButtonIntentService.class);
nextIntent.setAction(NotificationButtonIntentService.Action_Next);
mContentView.setOnClickPendingIntent(R.id.notification_layout_next, PendingIntent.getService(mContext, 0, nextIntent, PendingIntent.FLAG_CANCEL_CURRENT));
mContentView.setOnClickPendingIntent(R.id.notification_image_next, PendingIntent.getService(mContext, 0, nextIntent, PendingIntent.FLAG_CANCEL_CURRENT));
Intent closeIntent = new Intent(mContext, NotificationButtonIntentService.class);
closeIntent.setAction(NotificationButtonIntentService.Action_Close);
mContentView.setOnClickPendingIntent(R.id.notification_layout_close, PendingIntent.getService(mContext, 0, closeIntent, PendingIntent.FLAG_CANCEL_CURRENT));
mContentView.setOnClickPendingIntent(R.id.notification_image_close, PendingIntent.getService(mContext, 0, closeIntent, PendingIntent.FLAG_CANCEL_CURRENT));
}
Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true" >
<ImageView
android:id="#+id/notification_app_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="12dp"
android:src="#drawable/ic_stat_zipstreamer" />
<TextView
android:id="#+id/notificaiton_app_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1" >
<TextView
android:id="#+id/notificaiton_app_channel"
style="#style/notification.channel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:ems="2" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/notification_layout_previous"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1" >
<ImageButton
android:id="#+id/notification_image_previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#drawable/ic_media_previous" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/notification_layout_play_pause"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1" >
<ImageButton
android:id="#+id/notification_image_play_pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#drawable/ic_media_play" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/notification_layout_next"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1" >
<ImageButton
android:id="#+id/notification_image_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#drawable/ic_media_next" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/notification_layout_close"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1" >
<ImageButton
android:id="#+id/notification_image_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#drawable/ic_media_close" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
Intent Service:
public class NotificationButtonIntentService extends IntentService
{
public static final String TAG = "NotificationButtonIntentService";
public static final String Action_Play = "play";
public static final String Action_Pause = "pause";
public static final String Action_Previous = "previous";
public static final String Action_Next = "next";
public static final String Action_Close = "close";
public NotificationButtonIntentService()
{
super("");
}
public NotificationButtonIntentService(String name)
{
super(name);
}
#Override
protected void onHandleIntent(Intent intent)
{
String action = intent.getAction();
String realAction = intent.getAction();
if (realAction != null)
{
if (realAction.equals(Action_Play))
{
EventBus.getDefault().post(new PlayControlEvent(PlayControlEvent.PlayAction.PLAY));
}
else if (realAction.equals(Action_Pause))
{
EventBus.getDefault().post(new PlayControlEvent(PlayControlEvent.PlayAction.PAUSE));
}
else if (realAction.equals(Action_Close))
{
EventBus.getDefault().post(new ShutdownEvent());
}
else if ( realAction.equals(Action_Next))
{
EventBus.getDefault().post(new PlayControlEvent(PlayControlEvent.PlayAction.NEXT_CHANNEL));
}
else if ( realAction.equals(Action_Previous))
{
EventBus.getDefault().post(new PlayControlEvent(PlayControlEvent.PlayAction.PREVIOUS_CHANNEL));
}
}
}
}
You need yo access your MediaPlayer object. make it static and create your static pause,start,etc methods. something like this:
public class myMediaPlayer{
static MediaPlayer mediaPlayer = null;
public static void start(final Context context, Uri media){
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(context, media);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MEDIA);
mediaPlayer.prepare();
mediaPlayer.start();
}
public static void stop() {
if(mediaPlayer!=null)
mediaPlayer.release();
}
This is only my second time using widgets, and first time using buttons on it... or anything other than just TextViews in general.
So basically all I have is a widget... When I click it it doesn't do anything and it never updates. Why? Confused... :P
So, here is my WidgetAct onUpdate code:
#Override
public void onUpdate(Context c, AppWidgetManager awm, int[] appWidgetIds){
Log.i("IN","onUpdate");
RemoteViews rv = new RemoteViews(c.getPackageName(),R.layout.widget);
Intent twitter_intent = new Intent(c,TwitterUpdate.class);
twitter_intent.setAction(ACTION_WIDGET_TWITTER);
Intent fb_intent = new Intent(c,FBUpdate.class);
fb_intent.setAction(ACTION_WIDGET_FB);
//Intent curr = new Intent(c,WidgetAct.class);
//curr.setAction(ACTION_WIDGET_WA);
PendingIntent twitter_pi = PendingIntent.getActivity(c, 0, twitter_intent, 0);
PendingIntent fb_pi = PendingIntent.getActivity(c,0,fb_intent,0);
rv.setOnClickPendingIntent(R.id.tweet, twitter_pi);
rv.setOnClickPendingIntent(R.id.fb, fb_pi);
awm.updateAppWidget(appWidgetIds, rv);
}
Now here is my manifest (or the important part):
<receiver android:name="WidgetAct">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="com.laytproducts.IN.WidgetAct.ACTION_WIDGET_FB" />
<action android:name="com.laytproducts.IN.WidgetAct.ACTION_WIDGET_TWITTER" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="#xml/winfo"/>
</receiver>
Here is my xml for the widget provider:
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth = "220dp"
android:minHeight = "72dp"
android:updatePeriodMillis="0"
android:initialLayout="#layout/widget">
Here is my layout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<FrameLayout android:background="#drawable/inwidget" android:id="#+id/frameLayout1" android:layout_width="wrap_content" android:layout_height="wrap_content">
<RelativeLayout android:id="#+id/relativeLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent">
<ImageButton android:scaleType="fitXY" android:src="#drawable/twitterlogo" android:layout_marginTop="17dp" android:id="#+id/tweet" android:layout_marginLeft="20dp" android:layout_width="40dp" android:layout_height="40dp"></ImageButton>
<ImageButton android:scaleType="fitXY" android:src="#drawable/facebooklogo" android:layout_marginLeft="5dp" android:id="#+id/fb" android:layout_toRightOf="#+id/tweet" android:layout_alignTop="#+id/tweet" android:layout_alignBottom="#+id/tweet" android:layout_width="40dp" android:layout_height="40dp"></ImageButton>
</RelativeLayout>
</FrameLayout>
Kind of a lot of code... but I literally have NO reason to believe that I broke something XD... but that is because my widget knowledge is minimal.
Hopefully its a small mistake I over looked.
Thanks all,
Brandon
If you want your widget to update, you should have to set the time interval in "updatePeriodMillis" field in the XML file .But remember ,the minimum time interval for updating using this method is 30 min.
According to the explanation in the SDK document, the onUpdate() method is designed to respond to the ACTION_APPWIDGET_UPDATE broadcast, which will be sent in conditions below:
Sent when it is time to update your AppWidget.
This may be sent in response to a new instance for this AppWidget provider having been instantiated, the requested update interval having lapsed, or the system booting.
In your code, you set the action com.laytproducts.IN.WidgetAct.ACTION_WIDGET_FB, so the onUpdate() won't be called. I haven't used getActivity in widget, but I have used getBroadcast and it works well.
PendingIntent fb_pi = PendingIntent.getBroadcast(c,0,fb_intent,0);
I think you should handle your click event in the onReceive() method. Please note that the AppWidgetProvider extends from BroadcastReceiver, so only onReceive() will be called(in the onReceive() in the source code of AppWidgetProvider, onUpdate() is called inside. That's why we have onUpdate()). You should do something like:
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
String action = intent.getAction();
if (action.equals("com.laytproducts.IN.WidgetAct.ACTION_WIDGET_FB")) {
//handle your click event here
}
}
This is what I did in my widget. Hope it helps.
You can try this..
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
Intent one = new Intent(context, TwitterUpdate.class);
Intent two = new Intent(context, FBUpdate.class);
PendingIntent p1 = PendingIntent.getActivity(context, 0, one, 0);
PendingIntent p2 = PendingIntent.getActivity(context, 0, two, 0);
remoteViews.setOnClickPendingIntent(R.id.twitter_pi, p1);
remoteViews.setOnClickPendingIntent(R.id.fb_pi, p2);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}