-1

I want to create my widget on the home page after I hit a button in my app. The class of my widget is called Widget_p and my main layout is :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:mm="http://millennialmedia.com/android/schema"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/to"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:onClick="someMethod"
        android:background="@drawable/more" />

</RelativeLayout>


 @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

        final int n = appWidgetIds.length;
        view = new RemoteViews(context.getPackageName(), R.layout.widgetlayout);

        for (int i = 0; i < n; i++) {  
               int appWidgetId = appWidgetIds[i];  

               Intent intent = new Intent(context, FlashWidget.class);
               intent.setAction(ACTION_WIDGET_RECEIVER);

               PendingIntent pend = PendingIntent.getBroadcast(context, 0, intent, 0);

               view.setOnClickPendingIntent(R.id.Button, pend);


               appWidgetManager.updateAppWidget(appWidgetId, view);
        }


    }

Can anybody provide some code for that use or give me a reference URL?

We're All Mad Here
  • 1,544
  • 3
  • 18
  • 46

1 Answers1

0

Follow these guidelines and documentation and code for your understanding at : http://developer.android.com/guide/topics/appwidgets/index.html . It has enough details in detail step-by-step to dig in

Ramakishna Balla
  • 1,020
  • 1
  • 8
  • 12
  • Although, i doesn't get installed, – We're All Mad Here Jan 02 '14 at 15:01
  • never mind i'll start a new question here:http://stackoverflow.com/questions/20885792/why-doesnt-my-widget-gets-installed – We're All Mad Here Jan 02 '14 at 15:06
  • Do you mind if i ask you one more thing?,is there a way to create a widget/shortcut i don't know, on the home screen when i press a button from my app directly not in widgets, and to have the same functions as a widget.? – We're All Mad Here Jan 02 '14 at 15:15
  • Please read the link I shared with you and start coding as per your need. If you get stuck anywhere in your code / logic, post back with your logcat errors...SO experts are here to help you for sure but SO requires you to put some efforts as well -- Cheers – Ramakishna Balla Jan 04 '14 at 03:16