Skip to content

Conversation

@pitambar
Copy link

@pitambar pitambar commented Dec 9, 2013

hi i am using this list view,
is it possible to add 1 button on widget, on click of it ,it should open my app, i tried the below code for it, when i use this code ,list view displays empty view, & it forc close some time, how to do that ,please

public class WidgetProvider extends AppWidgetProvider {

/*
 * this method is called every 30 mins as specified on widgetinfo.xml this
 * method is also called on every phone reboot
 */
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
    final int N = appWidgetIds.length;
    /*
     * int[] appWidgetIds holds ids of multiple instance of your widget
     * meaning you are placing more than one widgets on your homescreen
     */
    // initializing widget layout

     RemoteViews viewClick = new
      RemoteViews(context.getPackageName(),R.layout.widget_layout);

      Intent informationIntent = new Intent(context,info.class);
      PendingIntent infoPendingIntent = PendingIntent.getActivity(context,0, informationIntent, 0);
      viewClick.setOnClickPendingIntent(R.id.btnSeeMore,infoPendingIntent);


    for (int i = 0; i < N; ++i) {
        RemoteViews remoteViews = updateWidgetListView(context,
                appWidgetIds[i]);
        appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
        // register for button event

    }

    super.onUpdate(context, appWidgetManager, appWidgetIds);
}

xml contain a button btnseemore

@pitambar pitambar closed this Dec 9, 2013
@pitambar pitambar reopened this Dec 9, 2013
@laaptu
Copy link
Owner

laaptu commented Dec 9, 2013

For adding button on widget,add a view (like ImageView) or any other views supported by app widget in the layout. Then on manifest.xml file,on receiver of appwidget provider

 <receiver android:name="your.appwidget.receivername" >
            <intent-filter>
                <action android:name="com.refresh.widget"/>
                <!-- Here you can put your own name  like com.your.own.name -->
            </intent-filter> 
</receiver>

So this name is essential as when it is received,you have to call your intent for launching the activity.Let's say you have added an ImageView on your layout for widget

  <ImageView
            android:id="@+id/refreshImageView"
            android:layout_width="32dip"
            android:layout_height="32dip"
            android:clickable="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:scaleType="fitXY"
            android:src="@drawable/your_drawable" />

Then on your AppWidgetProvider code

public class WidgetProvider extends AppWidgetProvider{
  public static String MANIFEST_DEFINED_STRING="com.refresh.widget";
  private RemoteViews updateWidgetListView(Context context, int appWidgetId) {

              // here you are just setting click listener to your image view        
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                R.layout.widget_layout);
        final Intent refreshIntent = new Intent(context, WidgetProvider.class);
        refreshIntent
                .putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        refreshIntent.setAction(WidgetProvider. MANIFEST_DEFINED_STRING);
        final PendingIntent refreshPendingIntent = PendingIntent.getBroadcast(
                context, appWidgetId, refreshIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.refreshImageView,
                refreshPendingIntent);
        return remoteViews;
    }

     @Override
    public void onReceive(Context context, Intent intent) {
    //here you will receive that onclicklistener

       if(MANIFEST_DEFINED_STRING.equals(intent.getAction()){
                                   Intent settingIntent = new Intent(ctx, YourActivity.class);
                    settingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(settingIntent);
    }
}

}

This is all you have to do to get to your activity by click of ImageView defined on the App Widget

@pitambar
Copy link
Author

pitambar commented Dec 9, 2013

log cat :
12-09 17:30:22.115: E/MtpService(28880): In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
12-09 17:30:22.115: E/MtpService(28880): battPlugged Type : 2
12-09 17:30:22.425: I/InputReader(472): Touch event's action is 0x0 (deviceType=0) [pCnt=1, s=0.38060 ]
12-09 17:30:22.425: I/InputDispatcher(472): Delivering touch to current input target: action: 0x0
12-09 17:30:22.425: I/InputDispatcher(472): Delivering touch to current input target: action: 0x0
12-09 17:30:22.435: I/InputDispatcher(472): Delivering touch to current input target: action: 0x0
12-09 17:30:22.435: I/InputDispatcher(472): Delivering touch to current input target: action: 0x0
12-09 17:30:22.475: E/ThermalDaemon(1076): CPU[3] offline
12-09 17:30:22.475: E/ThermalDaemon(1076): CPU[2] offline
12-09 17:30:22.475: I/TwDVFSBroadcastReceiver(770): onLauncherIntent
12-09 17:30:22.565: I/InputReader(472): Touch event's action is 0x1 (deviceType=0) [pCnt=1, s=]
12-09 17:30:22.565: I/InputDispatcher(472): Delivering touch to current input target: action: 0x1
12-09 17:30:22.565: I/InputDispatcher(472): Delivering touch to current input target: action: 0x1
12-09 17:30:22.565: I/InputDispatcher(472): Delivering touch to current input target: action: 0x1
12-09 17:30:22.565: I/InputDispatcher(472): Delivering touch to current input target: action: 0x1

manifest






appwidget provider:
public class Provider extends AppWidgetProvider {
public static String MANIFEST_DEFINED_STRING = "com.refresh.widget";

@SuppressWarnings("unused")
private RemoteViews updateWidgetListView(Context context, int appWidgetId) {

    // here you are just setting click listener to your image view
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
            R.layout.widget_layout);
    final Intent refreshIntent = new Intent(context, MainActivity.class);
    refreshIntent
            .putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    refreshIntent.setAction("com.refresh.widget");
    final PendingIntent refreshPendingIntent = PendingIntent.getBroadcast(
            context, appWidgetId, refreshIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    remoteViews.setOnClickPendingIntent(R.id.txtSeeMore,
            refreshPendingIntent);
    return remoteViews;
}

@Override
    public void onReceive(Context context, Intent intent) {
    //here you will receive that onclicklistener

       if(MANIFEST_DEFINED_STRING.equals(intent.getAction()))
               {
                    Intent settingIntent = new Intent();
                    settingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(settingIntent);
    }
}

}

widget_layout:

<TextView
    android:id="@+id/txttitle"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentTop="true"
    android:gravity="center"
    android:text="Title"
    android:textColor="#ffffff"
    android:textSize="20sp" />
<Button
    android:id="@+id/txtSeeMore"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:gravity="center"
    android:text="SeeMore"
    android:textColor="#ffffff"
    android:textSize="15sp"
    android:clickable="true" />

<ListView
    android:id="@+id/listViewWidget"
    android:layout_below="@id/txtQuirkli" 
    android:layout_above="@id/txtSeeMore"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

<TextView
    android:id="@+id/empty_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/txtQuirkli"
    android:gravity="center"
    android:text="@string/empty_string"
    android:textColor="#ffffff"
    android:textSize="20sp"
    android:visibility="gone" />

not working ,
below code worked for me please let me know i am doing it right or not

@pitambar
Copy link
Author

pitambar commented Dec 9, 2013

i have done some thing like ,is it right way of doing this or not please say that, if not than i wll change my code,
i your example on receive i changed that code,
i.e
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (intent.getAction().equals(DATA_FETCHED)) {
Log.i("inside action", "DATA_FETCHED");
int appWidgetId = intent.getIntExtra(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
AppWidgetManager appWidgetManager = AppWidgetManager
.getInstance(context);
RemoteViews remoteViews = updateWidgetListView(context, appWidgetId);

         Intent informationIntent = new Intent(context,MainActivity.class);
          PendingIntent infoPendingIntent = PendingIntent.getActivity(context,0, informationIntent, 0);
          remoteViews.setOnClickPendingIntent(R.id.txtSeeMore,infoPendingIntent);

        appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
    }

this code works fine without any modification anywere in your example, so am going in right or not

@pitambar
Copy link
Author

hi laaptu,
now i am trying different ways to use your code, so that we can create different ways of using same code,
how to get widget like gmail app widget, which have an view more conversations view end of the list
& how to give onclick to view more view

@pitambar
Copy link
Author

@laaptu
Copy link
Owner

laaptu commented Dec 13, 2013

Congratulations Pitu

On Fri, Dec 13, 2013 at 5:03 PM, pitu notifications@github.com wrote:

i solved update issue
check the link

http://stackoverflow.com/questions/20539167/appwidget-listview-update-issue-in-android/20565183#20565183


Reply to this email directly or view it on GitHubhttps://github.com//pull/2#issuecomment-30502534
.

@jettimadhu
Copy link

Hi,
Thanks for posting such a great one. I have small question regarding the app widget.

Does we use Gridview and listview in a single app widget or not..
Please help me on this...

Thanks..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants