it doesn't fire, setOnLongClickListener on AppWidgetHostView

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

it doesn't fire, setOnLongClickListener on AppWidgetHostView

Postby imray » Tue Oct 02, 2012 4:47 pm

i set OnLongClickListner, but it doesn't fire. Can anybody help me?
i just follow http://www.anddev.org/appwidgethost_tutorial-t10329.html, and change some, as it is posted in 2010.

Some part of code:
Code: Select all
//declare under class
private AppWidgetHostView widgets[] = new AppWidgetHostView[16];

widgets[widgCount] = attachWidget(mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo), width, height);
parent.addView(widgets[widgCount]);
       widgets[widgCount].setOnLongClickListener(new OnLongClickListener() {
           public boolean onLongClick(View v) {
              Toast.makeText(MainActivity.this, "hello hello ", Toast.LENGTH_SHORT).show();
               return true;
           }
       });


My code:
Code: Select all
package com.example.test5;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.appwidget.AppWidgetHost;
import android.appwidget.AppWidgetHostView;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProviderInfo;
import android.content.ComponentName;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends Activity {
   private Button b1;
   private AppWidgetManager mAppWidgetManager;
   
   private AppWidgetHost mAppWidgetHost;
   static final int APPWIDGET_HOST_ID = 2037;
   private static final int REQUEST_CREATE_APPWIDGET = 5;
   
   private static final int REQUEST_PICK_APPWIDGET = 9;
   
   
   
   private AppWidgetHostView widgets[] = new AppWidgetHostView[16];
   
   private int widgCount = 0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button1);
b1.setOnLongClickListener(new OnLongClickListener() {
      
    public boolean onLongClick(View v) {
    doWidgetPick();
   return false;
    }

});
        mAppWidgetManager = AppWidgetManager.getInstance(this);
       
       
       
        mAppWidgetHost = new AppWidgetHost(this, APPWIDGET_HOST_ID);
     
        mAppWidgetHost.startListening();
    }




   @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        menu.add(0, 1, 0, "Add Widget");
        return true;
    }
   public boolean onOptionsItemSelected(MenuItem item) {
      
       switch (item.getItemId()) {
   
       case 1:
   
           doWidgetPick();
   
           return true;
   
       }
   
       return false;
   
   }
   protected void doWidgetPick() {
      
        int appWidgetId = this.mAppWidgetHost.allocateAppWidgetId();



    Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);

    pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);

   

    /* custom extra that has to be there or else NPE will happen due to android bug   */

  //this is pulled from the Launcher source, I just changed a few things as it's just a dummy entry at this point

    ArrayList<AppWidgetProviderInfo> customInfo =

            new ArrayList<AppWidgetProviderInfo>();

    AppWidgetProviderInfo info = new AppWidgetProviderInfo();

    info.provider = new ComponentName(getPackageName(), "XXX.YYY");

    info.label = "i love android";

    info.icon = R.drawable.ic_launcher;

    customInfo.add(info);

    pickIntent.putParcelableArrayListExtra(

            AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);

    ArrayList<Bundle> customExtras = new ArrayList<Bundle>();

    Bundle b = new Bundle();

    b.putString("custom_widget", "search_widget");

    customExtras.add(b);

    pickIntent.putParcelableArrayListExtra(

            AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);

    /* that's a lot of lines that are there for no function at all */

       

   

    // start the pick activity

    startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);

    //because we've defined ourselves as a singleTask activity, it will allow this intent to be part of the task



}
   @Override
   
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   
       Log.v("result",requestCode + ", " + resultCode + ", " + data);
   
     //when the user completes the pick process, both these cases come back, according to log.
   
     //looks like this is happening because Pick does a check to see if a config needs to be launched first
   
     //if not it just sends the create intent
   
      
   
      
   
       if (resultCode == RESULT_OK) {
   
           switch (requestCode) {
   
                   case REQUEST_PICK_APPWIDGET:
   
                           addAppWidget(data);
   
                           break;
   
                   case REQUEST_CREATE_APPWIDGET:
   
                   completeAddAppWidget(data);
   
                   break;
   
           }
   
       }
   
       else if ((requestCode == REQUEST_PICK_APPWIDGET ||
   
               requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
   
               data != null) {
   
           // Clean up the appWidgetId if we canceled
   
           int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
   
           if (appWidgetId != -1) {
   
               mAppWidgetHost.deleteAppWidgetId(appWidgetId);
   
           }
   
       }
   
   }
   
   
   
   
   
   private void completeAddAppWidget(Intent data){
   
           //actually creates the view for the widget
   
   
   
       Bundle extras = data.getExtras();
   
       int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
   
   
   
       AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
   
      
   
   
   
       /* Launcher would calculate the grid spans needed to fit this widget
   
        * It would also do a check operation to abort if the cell user picked wasn't acceptable
   
        * given the size of the widget they chose
   
        */
   
      
   
       //What we'll do is log the info about the widget for help in letting user reposition it
   
      
   
       int width = appWidgetInfo.minWidth;
   
       int height = appWidgetInfo.minHeight;
   
      
   
   /*
   
   next we need to make a record of where we are adding this widget
   
   what the launcher is doing is spawning a helper object where it saves details about the widget
   
   it saves the number of cells wide and tall the widget is
   
   it adds the spawned object to the array list for widgetinfos
   
   the array list is a member of LauncherInfo helper object
   
   the model seems to retain the references to everything that's been placed on the Launcher
   
   */
   
       //we can get a reference to our main view here, and then add a relative layout to it.
   
       //I can probably directly reference the relative layout I want and then add widgets filling in from the top
   
       //just need to figure out how to determine if the widget being selected is too long to fit on existing row
   
       //to decide whether to place it on right of last widget or on the bottom
   
       RelativeLayout parent= (RelativeLayout) findViewById(R.id.relativelayout);
   
      
   
       //Log.v("getting parent ref","the ID of the parent is " + parent.getId());
   
   
   
       //AppWidgetHostView newWidget = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
   
   
   
       //we need to store this widget in an array. the views can be recreated but we need to have a persistent ref
   
       //FIXME currently we aren't persistent, need to learn how to make activity save persistent state
   
   
   
       widgets[widgCount] = attachWidget(mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo), width, height);
   
      
   
      
   
       parent.addView(widgets[widgCount]);
       widgets[widgCount].setOnLongClickListener(new OnLongClickListener() {
           public boolean onLongClick(View v) {
              Toast.makeText(MainActivity.this, "hello hello ", Toast.LENGTH_SHORT).show();
               return true;
           }
       });
      
   
      
   
       Log.v("widget was added","the ID of the widget view is " + widgets[widgCount].getId());
   
      
   
       widgCount++;
   
      
   
      
   
      
   
      
   
      
   
           //launcher is doing something to pass this view to their the workspace or the celllayout
   
          
   
           //so every single widget that gets created is one instance of the AppWidgetHostView.
   
           //the viewgroup we would have to maintain holds all the appwidgethostviews/
   
   }
   private AppWidgetHostView attachWidget(AppWidgetHostView widget, int w, int h){
      
       
      
       RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams
   
       (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
   
       if (widgCount == 0) params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
   
       //first widget goes at the top of the relative view widget area
   
       else params.addRule(RelativeLayout.RIGHT_OF, widgets[widgCount-1].getId());
   
        
   
       widget.setLayoutParams(params);
   
      
   
       widget.setId(100+widgCount);
   
       return widget;
   
       }
   void addAppWidget(Intent data) {
      
       // TODO: catch bad widget exception when sent
   
       int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
   
   
   
       String customWidget = data.getStringExtra("custom_widget");
   
       if ("search_widget".equals(customWidget)) {//user picked the extra
   
           // We don't need this any more, since this isn't a real app widget.
   
           mAppWidgetHost.deleteAppWidgetId(appWidgetId);
   
           //scold user for disobedience
   
       } else {
   
           AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
   
   
   
           if (appWidget.configure != null) {
   
               // Launch over to configure widget, if needed
   
               Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
   
               intent.setComponent(appWidget.configure);
   
               intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
   
   
   
               startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
   
           } else {
   
               // Otherwise just add it
   
               onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
   
           }
   
       }
   
   }
   private void onAppWidgetReset() {
      
       mAppWidgetHost.startListening();
   
   }
   @Override
   
   public void onDestroy() {
   
       //mDestroyed = true;
   
   
   
       super.onDestroy();
   
   
   
       try {
   
           mAppWidgetHost.stopListening();
   
       } catch (NullPointerException ex) {
   
           Log.w("lockscreen destroy", "problem while stopping AppWidgetHost during Lockscreen destruction", ex);
   
       }
   
   }
}

Code: Select all
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/relativelayout">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="Button" />

</RelativeLayout>
imray
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Oct 02, 2012 4:30 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 3 guests