2

I am making an app which will have a simple home screen widget( A simple imageview ).

What i want is to call some specific function like abc() or xyz() whenever i click on this imageview widget on my homescreen.

I have gone through so many examples but did not find a single one which could teach me this and 90% of the example were using TextView which has a function setTextViewText so TextView one is simple but how to call some specific function when we click on imageview or how to set onClick for ImageButton.

Please help and i would really appreciate if you provide me some piece of code.

Thanks.

public class Sample extends Activity {

public static final String TAG = "Sample";
public void onCreate(Bundle saveInstanceState)
{
    Log.e(TAG, "I am HERE");
    Toast.makeText(this, "You Just Pressed Me", Toast.LENGTH_LONG).show();
}
}

HEre is my update method -

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
    for(int i = 0; i < appWidgetIds.length; i++)
     {
         int appWidgetId = appWidgetIds[i];
         Intent intent = new Intent(context, Sample.class);
         //intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
         //intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
         PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
         RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.hswidget);
         views.setOnClickPendingIntent(R.id.widget, pendingIntent);

         appWidgetManager.updateAppWidget(appWidgetId, views);
     }
}
Varundroid
  • 9,135
  • 14
  • 63
  • 93

2 Answers2

3

Varundroid,

I don't think you can have a specific function launch on Widget click, but you can launch an Activity using a PendingIntent. That's how mine is currently setup, and all the Widget consist of is an ImageView. Here is my implementation...

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView 
        android:layout_width="64dp" 
        android:layout_height="64dp"
        android:src="@drawable/icon" 
        android:id="@+id/widget_image" 
        android:scaleType="centerCrop"
        android:layout_centerInParent="true"/>

</RelativeLayout>

Code:

public class WidgetProvider extends AppWidgetProvider {

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        final int N = appWidgetIds.length;
        for (int i=0; i<N; i++) {
            int appWidgetId = appWidgetIds[i];

            /* Create the PendingIntent for a QuickNote */
            Intent intent = new Intent(context, ItemEdit.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_provider);
            views.setOnClickPendingIntent(R.id.widget_image, pendingIntent);

            // Tell the AppWidgetManager to perform an update on the current App Widget
            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }
}

Don't forget AndroidManifest.xml!

    <receiver android:name=".WidgetProvider">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data android:name="android.appwidget.provider"
           android:resource="@xml/appwidget_provider" />
    </receiver>
Will Tate
  • 33,439
  • 9
  • 77
  • 71
  • @willytate i tried your solution. I created a new activity and put one Log message and Toast in it so i can see how it works and i have added this code in my question above. But the problem is none of them(Log and Toast) has been displayed :-( please take a look at my code and tell me that what i did wrong. Thanks – Varundroid Apr 21 '11 at 18:42
  • @willytate yeah i did that too. – Varundroid Apr 21 '11 at 19:04
  • @willy whenever i press on my widget its showing me this message in logcat :- 04-22 00:36:25.423: INFO/ActivityManager(59): Starting activity: Intent { flg=0x10000000 cmp=com.varundroid.util.completetaskmanager/.Sample bnds=[252,421][348,517] }. – Varundroid Apr 21 '11 at 19:07
  • I'm not seeing anything obviously wrong with your code :[ the Sample `Activity` is also in the Manifest? – Will Tate Apr 21 '11 at 19:09
  • based on the `logcat` message, it looks like its attempting to start the `Activity` – Will Tate Apr 21 '11 at 19:11
  • @willy finally it starts working both Log and Toast has been displayed. Actually we both did not notice that i did not make a call to `super.onCreate(savedInstance);`. And 1 more thing after showing me the toast it showing me a black screen. what is this black screen for? – Varundroid Apr 21 '11 at 19:17
  • ohk i got it i did not set any layout for my sample activity so how to get rid of this black screen? should i call finish()? – Varundroid Apr 21 '11 at 19:19
  • 1
    call `finish()` or you could try to make the `Activity` transparent http://stackoverflow.com/questions/2176922/how-to-create-transparent-activity-in-android – Will Tate Apr 21 '11 at 19:25
  • Thanks a lot willytate for all your help. I really appreciate it. :) – Varundroid Apr 21 '11 at 19:28
  • @willytate sorry for interrupting you again but i am facing a strange problem by making my activity transparent. whenever i click on my widget it performs the tasks but its also showing me black screen for few milliseconds. i tried hard to figure it out but did not find any leak. Any idea why its happening? thanks. – Varundroid Apr 23 '11 at 11:06
  • hmm, not sure. i've never tried making an `Activity` transparent. Perhaps a `Thread`, `AsyncTask` or `Service` might better suite your application? That way it would run in the background without needing to display anything at all. – Will Tate Apr 23 '11 at 13:12
-5

i don't understand what's your problem, but your problem mention above has very simple answer like instantiating imageview by

ImageView imgView=findViewById(R.id.imgView);

and then set onClickListner to

imgView.setOnClickListener(new view.OnClickListener(){

public void onClick(View view){
//call your functions
}
}
);
Sunil Pandey
  • 7,042
  • 7
  • 35
  • 48