appwidget-listview icon indicating copy to clipboard operation
appwidget-listview copied to clipboard

Appwidget listview2

Open pitambar opened this issue 11 years ago • 7 comments

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 avatar Dec 09 '13 08:12 pitambar

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

laaptu avatar Dec 09 '13 11:12 laaptu

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:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/appwidget_dark_bg" android:orientation="vertical" >

<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 avatar Dec 09 '13 12:12 pitambar

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 avatar Dec 09 '13 12:12 pitambar

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 avatar Dec 10 '13 08:12 pitambar

i solved update issue check the link http://stackoverflow.com/questions/20539167/appwidget-listview-update-issue-in-android/20565183#20565183

pitambar avatar Dec 13 '13 11:12 pitambar

Congratulations Pitu

On Fri, Dec 13, 2013 at 5:03 PM, pitu [email protected] 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/laaptu/appwidget-listview/pull/2#issuecomment-30502534 .

laaptu avatar Dec 13 '13 11:12 laaptu

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..

jettimadhu avatar Dec 20 '14 19:12 jettimadhu