KhizarIqbalEngr 0 Newbie Poster

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. When I move the widget on the home screen toast message is shown but when I click on the button nothing shown. But I want to show a toast message (or turn on Bluetooth as per requirements) whenerver I clicked on the widget.
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 for widger 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 appwidger-provider.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>  



@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    Log.d(TAG, "onUpdate()");

    final int N = appWidgetIds.length;
    for (int i = 0; i < N; i++) {
        Intent intent = new Intent(context, BluetoothToggle.class);
        intent.setAction(toggleBluetoothAction);
        PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);

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

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

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()");

    final int N = appWidgetIds.length;
    for (int i = 0; i < N; i++) {
        Intent intent = new Intent(context, BluetoothToggle.class);
        intent.setAction(toggleBluetoothAction);
        PendingIntent pendingIntent = PendingIntent.getService(context, 0, 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);
    }
@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);

    Log.d(TAG, "onReceive");
    Log.d(TAG, intent.getAction());

    Toast.makeText(context, "Button Clicked.....!!!", Toast.LENGTH_SHORT).show();
}

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