Reputation: 155
I have created a simple application in which I have 3 Imagebuttons in an activity. These buttons are performing some operations. Further I want to create a widget on home screen having the 3 Buttons which is performing same operations like the imageButtons in the activity. I have read many tutorials but cannot find a suitable one.
The widget looks like this:
XML code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:background="@drawable/my_shape"
android:layout_gravity="center"
android:layout_height="wrap_content">
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="52dip"
android:layout_height="50dip"
android:layout_gravity="center_horizontal|center"
android:src="@drawable/ic_launcher" />
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="52dip"
android:layout_height="50dip"
android:layout_gravity="center_horizontal|center"
android:src="@drawable/ic_launcher" />
<ImageButton
android:id="@+id/imageButton3"
android:layout_width="52dip"
android:layout_height="50dip"
android:layout_gravity="center_horizontal|center"
android:src="@drawable/ic_launcher" />
<ImageButton
android:id="@+id/imageButton3"
android:layout_width="52dip"
android:layout_height="50dip"
android:layout_gravity="center_horizontal|center"
android:src="@drawable/ic_launcher" />
</LinearLayout>
Widget_info.xml in res/xml:
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="146dip"
android:minHeight="72dip"
android:updatePeriodMillis="0"
android:initialLayout="@layout/widget_layout"/>
I also have widgets details in manifestfile.
I have not included activity code here. In the activity there are 3 imageButton onclick methods and I want the widgets buttons (3 button) to do exactly these operations.
Please can anyone guide me on this. Thanks in advance
Upvotes: 0
Views: 289
Reputation: 155
In my HomeActivity :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imgbtn1=(ImageButton)findViewById(R.id.imageCallButton1);
imgbtn2=(ImageButton)findViewById(R.id.imageFBButton1);
imgbtn3=(ImageButton)findViewById(R.id.imageSmsButton1);
imgbtn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "U clicked on Call Button", Toast.LENGTH_LONG).show();
}
});
imgbtn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "U clicked on FB Button", Toast.LENGTH_LONG).show();
}
});
imgbtn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "U clicked on SMS Button", Toast.LENGTH_LONG).show();
}
});
}
In my MyWidgetProvider activity:
public class MyWidgetProvider extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
//what should be added here
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
}
Upvotes: 0
Reputation: 15515
You can achieve this with the help of setOnClickPendingIntent
.
public class Widget extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
final int N = appWidgetIds.length;
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
// For first image
Intent intent = new Intent(context, SplashActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.appwidget);
views.setOnClickPendingIntent(R.id.imageButton1, pendingIntent);
//For second Image
Intent intent1 = new Intent(context, HomeActivity.class);
PendingIntent pendingIntent1 = PendingIntent.getActivity(context, 0,
intent1, 0);
views.setOnClickPendingIntent(R.id.imageButton2, pendingIntent1);
// like wise you can add your consecutive classes.
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
}
I hope this will help you.
Upvotes: 1