Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
<?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:
XML
<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
<?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:
Java
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.
Posted
Comments
Richard MacCutchan 15-Nov-15 3:19am    
I don't see any listener code to respond to the click.
EngrKhizarIqbal 15-Nov-15 3:23am    
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.bluetooth_toggle);
remoteViews.setOnClickPendingIntent(R.id.btnToggleBluetooth, pendingIntent);

Above code is written in onupdate method of BluetoothToggle class.
Richard MacCutchan 15-Nov-15 3:41am    
Not sure how that is supposed to work, but take a look at http://stackoverflow.com/questions/21715168/widget-setonclickpendingintent.

Thanks Richard for your reply. It solved my problem partially. Now 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).
The updated version of ToggleBluetooth.java file is:
Java
@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]);
    }
}

..... other methods same as in questions
Java
@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();
}
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 19-Nov-15 15:44pm    
Good job solving it! :-)
So finally I solve the problem after searching for two days. Here is the updated code:
C#
Intent intent = new Intent(context, BluetoothToggle.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWi‌​dgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
intent.putExtra(toggleBluetoothAction, toggleBluetoothAction);

And here is the onReceive() method which is called every time I clicked on the widget.
public void onReceive(Context context, Intent intent)
{
    Bundle bundle = intent.getExtras();

    if (bundle != null && bundle.getString(toggleBluetoothAction) != null)
    {
        // My Logic
    }
}


Take a look at the intent setAction and putExtra, which are useful in my case to solve the problem. I checked many website but most of them didn't include this piece of code to make widget clickable. All they did is to create an Intent and then immediately create a PendingIndent. I did the same but nothing helped me in any way.

Thanks.
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 19-Nov-15 15:45pm    
Do not re-post under the same question. You can surely add a solution, but update the previous one and write this one in that post.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900