Khizar Iqbal

Khizar Iqbal

  • 1.6k
  • 44
  • 8.8k

click listener on android widget Button not working

Nov 14 2015 3:46 PM

I'm working on an android project and created a widget for toggling Bluetooth (i.e., Pressing the widget turn on Bluetooth if turned off and vice versa). But the problem is that click listener of button is not working and when I click on the widget on the home screen it does nothing.
I do a lot of googling and tried almost all solutions but unfortunately nothing worked for me. I am unable to get the idea why my code is not working.

Here is the manifest file for project:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gmail.redprince007.togglebluetooth" >

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name=".BluetoothToggle" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>

<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/bluetooth_toggle_info" />
</receiver>
</application>

</manifest>

And xml file for widget layout is: 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/widget_margin">

<Button
android:id="@+id/btnToggleBluetooth"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bluetooth_on" />

</RelativeLayout>

And AppWidget-proviver.xml is: 

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialKeyguardLayout="@layout/bluetooth_toggle"
android:initialLayout="@layout/bluetooth_toggle"
android:minHeight="40dp"
android:minWidth="40dp"
android:previewImage="@drawable/ic_bluetooth_icon"
android:updatePeriodMillis="0"
android:widgetCategory="home_screen|keyguard">

</appwidget-provider>

And BluetoothOnOff.Java Is: 

package com.gmail.redprince007.togglebluetooth;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;

public class BluetoothToggle extends AppWidgetProvider {
private static final String TAG = "BluetoothToggle";
private final String toggleBluetoothAction = "ToggleBluetooth";

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Log.d(TAG, "onUpdate()");
// There may be multiple widgets active, so update all of them
final int N = appWidgetIds.length;
for (int i = 0; i < N; i++) {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], intent, 0);

RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.bluetooth_toggle);
remoteViews.setOnClickPendingIntent(R.id.btnToggleBluetooth, pendingIntent);

updateAppWidget(context, appWidgetManager, appWidgetIds[i]);
}
}

@Override
public void onEnabled(Context context) {
Log.d(TAG, "onEnabled()");
// Enter relevant functionality for when the first widget is created
}

@Override
public void onDisabled(Context context) {
Log.d(TAG, "onDisabled()");
// Enter relevant functionality for when the last widget is disabled
}

static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
Log.d(TAG, "updateAppWidget()");
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.bluetooth_toggle);

// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}

Thanks in advance for your reply and help. I spend a day to resolve the issue but all my effort went in vain.