I am making an app which will have a simple home screen widget( A simple imageview ).
What i want is to call some specific f开发者_Go百科unction 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,
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 Widge
t 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>
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
}
}
);
精彩评论