• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps click listener on android widget Button not working

RedPrince007

Lurker
Nov 15, 2015
2
1
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:
Code:
<?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:
Code:
<RelativeLayoutxmlns: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:

Code:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-providerxmlns: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:
[code]

publicclassBluetoothToggleextendsAppWidgetProvider{

privatestaticfinalString TAG ="BluetoothToggle";

privatefinalString toggleBluetoothAction ="ToggleBluetooth";




@Override

publicvoid onUpdate(Context context,AppWidgetManager appWidgetManager,int[] appWidgetIds){

Log.d(TAG,"onUpdate()");

// There may be multiple widgets active, so update all of them

finalint N = appWidgetIds.length;

for(int i =0; i < N; i++){

Intent intent =newIntent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

PendingIntent pendingIntent =PendingIntent.getActivity(context, appWidgetIds[i], intent,0);




RemoteViews remoteViews =newRemoteViews(context.getPackageName(), R.layout.bluetooth_toggle);

remoteViews.setOnClickPendingIntent(R.id.btnToggleBluetooth, pendingIntent);




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

}

}




@Override

publicvoid onEnabled(Context context){

Log.d(TAG,"onEnabled()");

// Enter relevant functionality for when the first widget is created

}




@Override

publicvoid onDisabled(Context context){

Log.d(TAG,"onDisabled()");

// Enter relevant functionality for when the last widget is disabled

}




staticvoid updateAppWidget(Context context,AppWidgetManager appWidgetManager,int appWidgetId){

Log.d(TAG,"updateAppWidget()");

RemoteViews views =newRemoteViews(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
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones