Example usage for android.view DragEvent getAction

List of usage examples for android.view DragEvent getAction

Introduction

In this page you can find the example usage for android.view DragEvent getAction.

Prototype

public int getAction() 

Source Link

Document

Inspect the action value of this event.

Usage

From source file:rosmi.acagild.alarmclock.ringing.AlarmRingingFragment.java

@Nullable
@Override/*  w  ww.ja v  a 2 s.co  m*/
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    Logger.init(getActivity());
    Bundle args = getArguments();
    UUID alarmId = UUID.fromString(args.getString(ARGS_ALARM_ID));
    mAlarm = AlarmList.get(getContext()).getAlarm(alarmId);

    View view = inflater.inflate(R.layout.fragment_alarm_ringing, container, false);

    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN) {
        TextView timeField = (TextView) view.findViewById(R.id.alarm_ringing_time);
        timeField.setText(DateTimeUtilities.getUserTimeString(getContext(), mAlarm.getTimeHour(),
                mAlarm.getTimeMinute()));
    }

    TextView dateField = (TextView) view.findViewById(R.id.alarm_ringing_date);
    dateField.setText(DateTimeUtilities.getFullDateStringForNow());

    String name = mAlarm.getTitle();
    TextView titleField = (TextView) view.findViewById(R.id.alarm_ringing_title);
    titleField.setText(name);

    ImageView dismissButton = (ImageView) view.findViewById(R.id.alarm_ringing_dismiss);
    dismissButton.setOnDragListener(new View.OnDragListener() {
        @Override
        public boolean onDrag(View v, DragEvent event) {
            switch (event.getAction()) {
            case DragEvent.ACTION_DROP:
                dismissAlarm();
                break;
            case DragEvent.ACTION_DRAG_ENDED:
                if (mShowClockOnDragEnd) {
                    mAlarmRingingClock.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            mAlarmRingingClock.setVisibility(View.VISIBLE);
                        }
                    }, SHOW_CLOCK_AFTER_UNSUCCESSFUL_DRAG_DELAY);
                }
                break;
            default:
                break;
            }
            return true;
        }
    });

    // Dismiss ringing if someone presses the dismiss button directly
    dismissButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dismissAlarm();
        }
    });

    ImageView snoozeButton = (ImageView) view.findViewById(R.id.alarm_ringing_snooze);
    snoozeButton.setOnDragListener(new View.OnDragListener() {
        @Override
        public boolean onDrag(View v, DragEvent event) {
            switch (event.getAction()) {
            case DragEvent.ACTION_DROP:
                mCallback.onRingingSnooze();
                break;
            default:
                break;
            }
            return true;
        }
    });

    // Snooze ringing if someone presses the snooze button directly
    snoozeButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mCallback.onRingingSnooze();
        }
    });

    // Allow the view to listen to the drag event to update arrow animations accordingly
    view.setOnDragListener(new View.OnDragListener() {
        @Override
        public boolean onDrag(View v, DragEvent event) {
            switch (event.getAction()) {
            case DragEvent.ACTION_DRAG_LOCATION:
                // Update the left/right arrow visibility based on the current drag location.
                onClockDragLocation(event.getX(), event.getY(), v.getWidth() / 2);
                break;
            case DragEvent.ACTION_DROP:
                // The user has dropped the drag, but it is dropped within the view, instead of the target
                // drop zones to dismiss or snooze.
                // Restore to show both left arrow and right arrow animations.
                mDragZone = DragZone.NEAR_MIDDLE_OF_VIEW;
                updateArrowsBasedOnDragZone(mDragZone);
                break;
            default:
                break;
            }
            return true;
        }
    });

    mAlarmRingingClock = (ImageView) view.findViewById(R.id.alarm_ringing_clock);
    mAlarmRingingClock.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                ClipData dragData = ClipData.newPlainText("", "");
                View.DragShadowBuilder shadow = new View.DragShadowBuilder(mAlarmRingingClock);
                mAlarmRingingClock.startDrag(dragData, shadow, null, 0);
                mAlarmRingingClock.setVisibility(View.INVISIBLE);
                return true;
            } else {
                return false;
            }

        }
    });

    initializeClockAnimation(view);

    Loggable.AppAction appAction = new Loggable.AppAction(Loggable.Key.APP_ALARM_RINGING);

    appAction.putJSON(mAlarm.toJSON());
    Logger.track(appAction);

    return view;
}

From source file:com.microsoft.mimickeralarm.ringing.AlarmRingingFragment.java

@Nullable
@Override// ww w  .j  ava 2  s .  c  o m
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    Logger.init(getActivity());
    Bundle args = getArguments();
    UUID alarmId = UUID.fromString(args.getString(ARGS_ALARM_ID));
    mAlarm = AlarmList.get(getContext()).getAlarm(alarmId);

    View view = inflater.inflate(R.layout.fragment_alarm_ringing, container, false);

    if (android.os.Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN) {
        TextView timeField = (TextView) view.findViewById(R.id.alarm_ringing_time);
        timeField.setText(DateTimeUtilities.getUserTimeString(getContext(), mAlarm.getTimeHour(),
                mAlarm.getTimeMinute()));
    }

    TextView dateField = (TextView) view.findViewById(R.id.alarm_ringing_date);
    dateField.setText(DateTimeUtilities.getFullDateStringForNow());

    String name = mAlarm.getTitle();
    TextView titleField = (TextView) view.findViewById(R.id.alarm_ringing_title);
    titleField.setText(name);

    ImageView dismissButton = (ImageView) view.findViewById(R.id.alarm_ringing_dismiss);
    dismissButton.setOnDragListener(new View.OnDragListener() {
        @Override
        public boolean onDrag(View v, DragEvent event) {
            switch (event.getAction()) {
            case DragEvent.ACTION_DROP:
                dismissAlarm();
                break;
            case DragEvent.ACTION_DRAG_ENDED:
                if (mShowClockOnDragEnd) {
                    mAlarmRingingClock.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            mAlarmRingingClock.setVisibility(View.VISIBLE);
                        }
                    }, SHOW_CLOCK_AFTER_UNSUCCESSFUL_DRAG_DELAY);
                }
                break;
            default:
                break;
            }
            return true;
        }
    });

    // Dismiss ringing if someone presses the dismiss button directly
    dismissButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dismissAlarm();
        }
    });

    ImageView snoozeButton = (ImageView) view.findViewById(R.id.alarm_ringing_snooze);
    snoozeButton.setOnDragListener(new View.OnDragListener() {
        @Override
        public boolean onDrag(View v, DragEvent event) {
            switch (event.getAction()) {
            case DragEvent.ACTION_DROP:
                mCallback.onRingingSnooze();
                break;
            default:
                break;
            }
            return true;
        }
    });

    // Snooze ringing if someone presses the snooze button directly
    snoozeButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mCallback.onRingingSnooze();
        }
    });

    // Allow the view to listen to the drag event to update arrow animations accordingly
    view.setOnDragListener(new View.OnDragListener() {
        @Override
        public boolean onDrag(View v, DragEvent event) {
            switch (event.getAction()) {
            case DragEvent.ACTION_DRAG_LOCATION:
                // Update the left/right arrow visibility based on the current drag location.
                onClockDragLocation(event.getX(), event.getY(), v.getWidth() / 2);
                break;
            case DragEvent.ACTION_DROP:
                // The user has dropped the drag, but it is dropped within the view, instead of the target
                // drop zones to dismiss or snooze.
                // Restore to show both left arrow and right arrow animations.
                mDragZone = DragZone.NEAR_MIDDLE_OF_VIEW;
                updateArrowsBasedOnDragZone(mDragZone);
                break;
            default:
                break;
            }
            return true;
        }
    });

    mAlarmRingingClock = (ImageView) view.findViewById(R.id.alarm_ringing_clock);
    mAlarmRingingClock.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                ClipData dragData = ClipData.newPlainText("", "");
                View.DragShadowBuilder shadow = new View.DragShadowBuilder(mAlarmRingingClock);
                mAlarmRingingClock.startDrag(dragData, shadow, null, 0);
                mAlarmRingingClock.setVisibility(View.INVISIBLE);
                return true;
            } else {
                return false;
            }

        }
    });

    initializeClockAnimation(view);

    Loggable.AppAction appAction = new Loggable.AppAction(Loggable.Key.APP_ALARM_RINGING);

    appAction.putJSON(mAlarm.toJSON());
    Logger.track(appAction);

    return view;
}

From source file:com.launcher.silverfish.launcher.appdrawer.TabbedAppDrawerFragment.java

private void setOnDragListener() {

    rootView.setOnDragListener(new View.OnDragListener() {
        @Override//w w w.  j av a 2  s.  com
        public boolean onDrag(View view, DragEvent dragEvent) {

            switch (dragEvent.getAction()) {
            case DragEvent.ACTION_DRAG_STARTED: {
                // Care only about DRAG_APP_MOVE drags.
                ClipDescription cd = dragEvent.getClipDescription();
                if (!cd.getLabel().toString().equals(Constants.DRAG_APP_MOVE))
                    return false;

                // Starting movement, drag offset is now reset to 0
                dragOffsetX = 0;
                dragOffsetY = 0;

                // Show the uninstall indicator
                showUninstallIndicator();
                break;
            }

            case DragEvent.ACTION_DRAG_ENTERED: {
                // Don't do anything
                break;
            }

            case DragEvent.ACTION_DRAG_LOCATION: {
                // getX() and getY() now return relative offsets,
                // so accumulate them to get the total movement
                dragOffsetX += dragEvent.getX();
                dragOffsetY += dragEvent.getY();

                // If drag is on the way out of this page then stop receiving drag events
                int threshold = Constants.SCREEN_CORNER_THRESHOLD;
                // Get display size
                int screen_width = Utils.getScreenDimensions(getActivity()).x;
                if (dragEvent.getX() > screen_width - threshold) {
                    return false;

                } else {

                    // Check if the drag is hovering over a tab button
                    int i = tabHandler.getHoveringTab(dragEvent.getX(), dragEvent.getY());

                    // If so, change to that tab
                    if (i > -1)
                        tabHandler.setTab(i);
                }
                break;
            }

            case DragEvent.ACTION_DROP: {
                String appName = dragEvent.getClipData().getItemAt(0).getText().toString();

                // If app is dropped on the uninstall indicator uninstall the app
                if (Utils.onBottomCenterScreenEdge(getActivity(), dragEvent.getX(), dragEvent.getY())) {
                    launchUninstallIntent(appName);
                } else {
                    // If the user didn't move the application from its original
                    // place (too much), then they might want to show a menu with more options
                    float distSq = (dragOffsetX * dragOffsetX) + (dragOffsetY * dragOffsetY);
                    if (distSq < Constants.NO_DRAG_THRESHOLD_SQ) {
                        showExtraOptionsMenu(appName);
                    } else {
                        // Retrieve tha drop information  and remove it from the original tab
                        int appIndex = Integer
                                .parseInt(dragEvent.getClipData().getItemAt(1).getText().toString());

                        String tabTag = dragEvent.getClipData().getItemAt(2).getText().toString();

                        removeAppFromTab(appIndex, tabTag);

                        // add it to the new tab
                        String app_name = dragEvent.getClipData().getItemAt(0).getText().toString();
                        dropAppInTab(app_name);
                    }
                }
                break;
            }

            case DragEvent.ACTION_DRAG_ENDED: {
                // Just hide the uninstall indicator
                hideUninstallIndicator();
                break;
            }

            }
            return true;
        }

    });
}

From source file:com.launcher.silverfish.HomeScreenFragment.java

private void setOnDragListener() {
    rootView.setOnDragListener(new View.OnDragListener() {
        @Override/* w  w  w . j a v a2  s .co  m*/
        public boolean onDrag(View view, DragEvent dragEvent) {
            switch (dragEvent.getAction()) {
            case DragEvent.ACTION_DRAG_STARTED:
                // Check that it is a shortcut removal gesture
                ClipDescription cd = dragEvent.getClipDescription();
                if (!cd.getLabel().toString().equals(Constants.DRAG_SHORTCUT_REMOVAL)) {
                    return false;
                }
                break;
            case DragEvent.ACTION_DRAG_ENTERED:
                // Don't do anything
                break;
            case DragEvent.ACTION_DRAG_LOCATION:
                //Don't do anything
                break;
            case DragEvent.ACTION_DROP:

                // If outside of bound, remove the app
                if (Utils.onBottomCenterScreenEdge(getActivity(), dragEvent.getX(), dragEvent.getY())) {
                    String appId = dragEvent.getClipData().getItemAt(0).getText().toString();
                    String appIndex = dragEvent.getClipData().getItemAt(1).getText().toString();
                    removeApp(Integer.parseInt(appIndex), Long.parseLong(appId));
                    updateShortcuts();
                }

                break;
            case DragEvent.ACTION_DRAG_ENDED:
                // Hide the remove-indicator
                FrameLayout rem_ind = (FrameLayout) rootView.findViewById(R.id.remove_indicator);
                rem_ind.setVisibility(View.INVISIBLE);
                break;

            }
            return true;
        }
    });
}

From source file:com.svpino.longhorn.fragments.StockListFragment.java

@TargetApi(11)
@Override//  w  ww.  j  a  va  2  s.  co m
public boolean onDrag(View tile2, DragEvent event) {
    View tile1 = (View) event.getLocalState();

    StockTileViewHolder stockTileViewHolder1 = (StockTileViewHolder) tile1.getTag();
    StockTileViewHolder stockTileViewHolder2 = (StockTileViewHolder) tile2.getTag();

    switch (event.getAction()) {
    case DragEvent.ACTION_DRAG_STARTED:
        tile1.setVisibility(View.INVISIBLE);

        if (stockTileViewHolder1.getPosition() != stockTileViewHolder2.getPosition()) {
            return true;
        }

        return false;

    case DragEvent.ACTION_DRAG_ENTERED:
        this.focusedTileDuringDragAndDrop = tile2;
        StockTileProcessor.updateTileColorToDropReceptor(tile2);
        tile2.invalidate();

        return true;

    case DragEvent.ACTION_DRAG_LOCATION:
        return true;

    case DragEvent.ACTION_DRAG_EXITED:
        this.focusedTileDuringDragAndDrop = null;
        StockTileProcessor.updateTileColor(this, tile2, this.selectedTiles);
        tile2.invalidate();

        return true;

    case DragEvent.ACTION_DROP:
        this.focusedTileDuringDragAndDrop = null;
        swapTiles(tile1, tile2);
        return true;

    case DragEvent.ACTION_DRAG_ENDED:
        if (this.focusedTileDuringDragAndDrop != null) {
            swapTiles(tile1, this.focusedTileDuringDragAndDrop);
            this.focusedTileDuringDragAndDrop = null;

            return true;
        }

        StockTileProcessor.updateTileColor(this, tile1, this.selectedTiles);
        StockTileProcessor.updateTileColor(this, tile2, this.selectedTiles);

        tile1.setVisibility(View.VISIBLE);
        tile2.setVisibility(View.VISIBLE);

        tile1.invalidate();
        tile2.invalidate();

        return true;

    default:
        return true;
    }
}

From source file:org.chromium.chrome.browser.compositor.CompositorViewHolder.java

@Override
public boolean dispatchDragEvent(DragEvent e) {
    ContentViewCore contentViewCore = mTabVisible.getContentViewCore();
    if (contentViewCore == null)
        return false;

    if (mLayoutManager != null)
        mLayoutManager.getViewportPixel(mCacheViewport);
    contentViewCore.setCurrentTouchEventOffsets(-mCacheViewport.left, -mCacheViewport.top);
    boolean ret = super.dispatchDragEvent(e);

    int action = e.getAction();
    if (action == DragEvent.ACTION_DRAG_EXITED || action == DragEvent.ACTION_DRAG_ENDED
            || action == DragEvent.ACTION_DROP) {
        contentViewCore.setCurrentTouchEventOffsets(0.f, 0.f);
    }//  w w w.j av  a 2  s  .  co  m
    return ret;
}

From source file:com.tct.mail.browse.ConversationItemView.java

/**
 * Handles the drag event./* w w w .  j  a v  a2  s  . c o m*/
 *
 * @param event the drag event to be handled
 */
@Override
public boolean onDragEvent(DragEvent event) {
    switch (event.getAction()) {
    case DragEvent.ACTION_DRAG_ENDED:
        mActivity.stopDragMode();
        return true;
    }
    return false;
}

From source file:com.android.systemui.qs.QSDragPanel.java

@Override
public boolean onDrag(View v, DragEvent event) {
    final DragTileRecord targetTile = (DragTileRecord) getRecord(v);
    boolean originatingTileEvent = mDraggingRecord != null && v == mDraggingRecord.tileView;

    final int dragRecordIndex = mRecords.indexOf(mDraggingRecord);
    boolean dragRecordAttached = dragRecordIndex != -1;
    switch (event.getAction()) {
    case DragEvent.ACTION_DRAG_STARTED:
        if (DEBUG_DRAG) {
            Log.v(TAG, "ACTION_DRAG_STARTED on view: " + v);
        }//from  w  ww . ja  va  2 s .c om

        if (originatingTileEvent) {
            if (DEBUG_DRAG) {
                Log.v(TAG, "ACTION_DRAG_STARTED on target view.");
            }
            mRestored = false;
            mQsPanelTop.setDropIcon(R.drawable.ic_qs_tile_delete_disable, R.color.qs_tile_trash_normal_tint);
        }

        break;

    case DragEvent.ACTION_DRAG_ENTERED:
        if (DEBUG_DRAG) {
            if (targetTile != null) {
                Log.v(TAG, "ACTION_DRAG_ENTERED on view with tile: " + targetTile);
            } else {
                Log.v(TAG, "ACTION_DRAG_ENTERED on view: " + v);
            }
        }
        mLocationHits = 0;
        mMovedByLocation = false;

        if (v == mQsPanelTop) {
            int icon, color;
            if (mDraggingRecord.tile instanceof EditTile) {
                // use a different warning, user can't erase this one
                icon = R.drawable.ic_qs_tile_delete_disable_avd;
                color = R.color.qs_tile_trash_delete_tint_warning;
            } else {
                icon = R.drawable.ic_qs_tile_delete_disable;
                color = R.color.qs_tile_trash_delete_tint;
            }

            mQsPanelTop.setDropIcon(icon, color);
        }

        if (!originatingTileEvent && v != getDropTarget() && targetTile != null) {
            if (DEBUG_DRAG) {
                Log.e(TAG, "entered tile " + targetTile);
            }
            if (mCurrentlyAnimating.isEmpty() && !mViewPager.isFakeDragging() && !dragRecordAttached) {
                mMovedByLocation = true;
                shiftTiles(targetTile, true);
            } else {
                if (DEBUG_DRAG) {
                    Log.w(TAG, "ignoring action enter for animating tiles and fake drags");
                }
            }
        }

        break;
    case DragEvent.ACTION_DRAG_ENDED:
        if (DEBUG_DRAG) {
            Log.v(TAG, "ACTION_DRAG_ENDED on view: " + v + "(tile: " + targetTile + "), result: "
                    + event.getResult());
        }
        if (originatingTileEvent && !event.getResult()) {
            // view pager probably ate the event
            restoreDraggingTilePosition(v, null);
        }

        break;

    case DragEvent.ACTION_DROP:
        if (DEBUG_DRAG) {
            Log.v(TAG, "ACTION_DROP, event loc: " + event.getX() + ", " + event.getY() + " + with tile: "
                    + targetTile + " and view: " + v);
        }
        mLastTouchLocationX = event.getX();
        mLastTouchLocationY = event.getY();

        if (isDropTargetEvent(event, v)) {
            if (DEBUG_DRAG) {
                Log.d(TAG, "dropping on delete target!!");
            }
            if (mDraggingRecord.tile instanceof EditTile) {
                final QSTileView editTileView = mDraggingRecord.tileView;

                mQsPanelTop.toast(R.string.quick_settings_cannot_delete_edit_tile);
                restoreDraggingTilePosition(v, new Runnable() {
                    @Override
                    public void run() {
                        // move edit tile to the back
                        final TileRecord editTile = getRecord(editTileView);
                        if (mRecords.remove(editTile)) {
                            // we depend on mHost.setTiles() placing it on the end
                            persistRecords();
                        }
                    }
                });
                break;
            } else if (mDraggingRecord.tile instanceof CustomQSTile) {
                ((CustomQSTile) mDraggingRecord.tile).setUserRemoved(true);
                final String spec = mHost.getSpec(mDraggingRecord.tile);
                restoreDraggingTilePosition(v, new Runnable() {
                    @Override
                    public void run() {
                        // it might get added back later by the app, but that's ok,
                        // we just want to reset its position after it has been removed.
                        mHost.remove(spec);
                    }
                });
            } else {
                mRestored = true;
                removeDraggingRecord();
            }
        } else {
            restoreDraggingTilePosition(v, null);
        }
        break;

    case DragEvent.ACTION_DRAG_EXITED:
        if (DEBUG_DRAG) {
            if (targetTile != null) {
                Log.v(TAG, "ACTION_DRAG_EXITED on view with tile: " + targetTile);
            } else {
                Log.v(TAG, "ACTION_DRAG_EXITED on view: " + v);
            }
        }

        if (v == mQsPanelTop) {
            mQsPanelTop.setDropIcon(R.drawable.ic_qs_tile_delete_disable, R.color.qs_tile_trash_normal_tint);
        }

        if (originatingTileEvent && mCurrentlyAnimating.isEmpty() && !mViewPager.isFakeDragging()
                && dragRecordAttached && mLastLeftShift == -1) {

            if (DEBUG_DRAG) {
                Log.v(TAG, "target: " + targetTile + ", hit mLastRightShift: " + mLastRightShift
                        + ", mLastLeftShift: " + mLastLeftShift + ", dragRecordIndex: " + dragRecordIndex);
            }

            // move tiles back
            shiftTiles(mDraggingRecord, false);
            break;
        }
        // fall through so exit events can trigger a left shift
    case DragEvent.ACTION_DRAG_LOCATION:
        mLastTouchLocationX = event.getX();
        mLastTouchLocationY = event.getY();

        // do nothing if we're animating tiles
        if (mCurrentlyAnimating.isEmpty() && !mViewPager.isFakeDragging()) {
            if (v == mViewPager) {
                // do we need to change pages?
                int x = (int) event.getX();
                int width = mViewPager.getWidth();
                int scrollPadding = (int) (width * QSViewPager.SCROLL_PERCENT);
                if (x < scrollPadding) {
                    if (mViewPager.canScrollHorizontally(-1)) {
                        mViewPager.animatePagerTransition(false);
                        return true;
                    }
                } else if (x > width - scrollPadding) {
                    if (mViewPager.canScrollHorizontally(1)) {
                        mViewPager.animatePagerTransition(true);
                        return true;
                    }
                }
            }
            if (DEBUG_DRAG) {
                Log.v(TAG, "location hit:// target: " + targetTile + ", hit mLastRightShift: " + mLastRightShift
                        + ", mLastLeftShift: " + mLastLeftShift + ", dragRecordIndex: " + dragRecordIndex
                        + ", originatingTileEvent: " + originatingTileEvent + ", mLocationHits: "
                        + mLocationHits + ", mMovedByLocation: " + mMovedByLocation);
            }

            if (v != getDropTarget() && targetTile != null && !dragRecordAttached) {
                // dragging around on another tile
                if (mLocationHits++ == 30) {
                    if (DEBUG_DRAG) {
                        Log.w(TAG, "shifting right due to location hits.");
                    }
                    // add dragging tile to current page
                    shiftTiles(targetTile, true);
                    mMovedByLocation = true;
                } else {
                    mLocationHits++;
                }
            } else if (mLastRightShift != -1 // right has shifted recently
                    && mLastLeftShift == -1 // -1 means its attached
                    && dragRecordIndex == mLastRightShift && !originatingTileEvent
                    && !mMovedByLocation /* helps avoid continuous shifting */) {
                // check if the location is on another tile/view
                // that is not the last drag index, shift back left to revert back and
                // potentially get ready for shifting right
                if (DEBUG_DRAG) {
                    Log.w(TAG, "conditions met to reverse!!!! shifting left. <<<<<<<");
                }
                shiftTiles((DragTileRecord) mRecords.get(mLastRightShift), false);
                mMovedByLocation = true;
            }

        } else {
            if (DEBUG_DRAG) {
                Log.i(TAG, "ignoring location event because things are animating, size: "
                        + mCurrentlyAnimating.size());
            }
        }
        break;

    default:
        Log.w(TAG, "unhandled event");
        return false;
    }
    return true;
}

From source file:foam.starwisp.StarwispBuilder.java

public void Build(final StarwispActivity ctx, final String ctxname, JSONArray arr, ViewGroup parent) {

    if (StarwispLinearLayout.m_DisplayMetrics == null) {
        StarwispLinearLayout.m_DisplayMetrics = ctx.getResources().getDisplayMetrics();
    }/*from  ww w .j av a 2  s  . c  o  m*/

    try {
        String type = arr.getString(0);

        //Log.i("starwisp","building started "+type);

        if (type.equals("build-fragment")) {
            String name = arr.getString(1);
            int ID = arr.getInt(2);
            Fragment fragment = ActivityManager.GetFragment(name);
            LinearLayout inner = new LinearLayout(ctx);
            inner.setLayoutParams(BuildLayoutParams(arr.getJSONArray(3)));
            inner.setId(ID);
            FragmentTransaction fragmentTransaction = ctx.getSupportFragmentManager().beginTransaction();
            fragmentTransaction.add(ID, fragment);
            fragmentTransaction.commit();
            parent.addView(inner);
            return;
        }

        if (type.equals("map")) {
            int ID = arr.getInt(1);
            LinearLayout inner = new LinearLayout(ctx);
            inner.setLayoutParams(BuildLayoutParams(arr.getJSONArray(2)));
            inner.setId(ID);
            Fragment mapfrag = SupportMapFragment.newInstance();
            FragmentTransaction fragmentTransaction = ctx.getSupportFragmentManager().beginTransaction();
            fragmentTransaction.add(ID, mapfrag);
            fragmentTransaction.commit();
            parent.addView(inner);
            return;
        }

        if (type.equals("drawmap")) {
            final LinearLayout inner = new LinearLayout(ctx);
            inner.setLayoutParams(BuildLayoutParams(arr.getJSONArray(2)));
            DrawableMap dm = new DrawableMap();
            dm.init(arr.getInt(1), inner, (StarwispActivity) ctx, this, arr.getString(3));
            parent.addView(inner);
            m_DMaps.put(arr.getInt(1), dm);
            return;
        }

        if (type.equals("linear-layout")) {
            StarwispLinearLayout.Build(this, ctx, ctxname, arr, parent);
            return;
        }

        if (type.equals("relative-layout")) {
            StarwispRelativeLayout.Build(this, ctx, ctxname, arr, parent);
            return;
        }

        if (type.equals("draggable")) {
            final LinearLayout v = new LinearLayout(ctx);
            final int id = arr.getInt(1);
            final String behaviour_type = arr.getString(5);
            v.setPadding(20, 20, 20, 10);
            v.setId(id);
            v.setOrientation(StarwispLinearLayout.BuildOrientation(arr.getString(2)));
            v.setLayoutParams(BuildLayoutParams(arr.getJSONArray(3)));
            v.setClickable(true);
            v.setFocusable(true);

            JSONArray col = arr.getJSONArray(4);
            v.setBackgroundResource(R.drawable.draggable);

            GradientDrawable drawable = (GradientDrawable) v.getBackground();
            final int colour = Color.argb(col.getInt(3), col.getInt(0), col.getInt(1), col.getInt(2));
            drawable.setColor(colour);

            /*LayerDrawable bgDrawable = (LayerDrawable)v.getBackground();
            GradientDrawable bgShape = (GradientDrawable)bgDrawable.findDrawableByLayerId(R.id.draggableshape);
            bgShape.setColor(colour);*/
            /*v.getBackground().setColorFilter(colour, PorterDuff.Mode.MULTIPLY);*/

            parent.addView(v);
            JSONArray children = arr.getJSONArray(6);
            for (int i = 0; i < children.length(); i++) {
                Build(ctx, ctxname, new JSONArray(children.getString(i)), v);
            }

            // Sets a long click listener for the ImageView using an anonymous listener object that
            // implements the OnLongClickListener interface
            if (!behaviour_type.equals("drop-only") && !behaviour_type.equals("drop-only-consume")) {
                v.setOnLongClickListener(new View.OnLongClickListener() {
                    public boolean onLongClick(View vv) {
                        if (id != 99) {
                            ClipData dragData = new ClipData(
                                    new ClipDescription("" + id,
                                            new String[] { ClipDescription.MIMETYPE_TEXT_PLAIN }),
                                    new ClipData.Item("" + id));

                            View.DragShadowBuilder myShadow = new MyDragShadowBuilder(v);
                            Log.i("starwisp", "start drag id " + vv.getId() + " " + v);
                            v.startDrag(dragData, myShadow, v, 0);
                            v.setVisibility(View.GONE);
                            return true;
                        }
                        return false;
                    }
                });
            }

            if (!behaviour_type.equals("drag-only")) {
                // ye gads - needed as drag/drop doesn't deal with nested targets
                final StarwispBuilder that = this;

                v.setOnDragListener(new View.OnDragListener() {
                    public boolean onDrag(View vv, DragEvent event) {

                        //Log.i("starwisp","on drag event happened");

                        final int action = event.getAction();
                        switch (action) {
                        case DragEvent.ACTION_DRAG_STARTED:
                            //Log.i("starwisp","Drag started"+v );
                            if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
                                // returns true to indicate that the View can accept the dragged data.
                                return true;
                            } else {
                                // Returns false. During the current drag and drop operation, this View will
                                // not receive events again until ACTION_DRAG_ENDED is sent.
                                return false;
                            }
                        case DragEvent.ACTION_DRAG_ENTERED: {
                            if (that.m_LastDragHighlighted != null) {
                                that.m_LastDragHighlighted.getBackground().setColorFilter(null);
                            }
                            v.getBackground().setColorFilter(0x77777777, PorterDuff.Mode.MULTIPLY);
                            that.m_LastDragHighlighted = v;
                            //Log.i("starwisp","Drag entered"+v );
                            return true;
                        }
                        case DragEvent.ACTION_DRAG_LOCATION: {
                            //View dragee = (View)event.getLocalState();
                            //dragee.setVisibility(View.VISIBLE);
                            //Log.i("starwisp","Drag location"+v );
                            return true;
                        }
                        case DragEvent.ACTION_DRAG_EXITED: {
                            //Log.i("starwisp","Drag exited "+v );
                            v.getBackground().setColorFilter(null);
                            return true;
                        }
                        case DragEvent.ACTION_DROP: {
                            v.getBackground().setColorFilter(null);
                            //Log.i("starwisp","Drag dropped "+v );
                            View otherw = (View) event.getLocalState();
                            //Log.i("starwisp","removing from parent "+((View)otherw.getParent()).getId());

                            // check we are not adding to ourself
                            if (id != otherw.getId()) {
                                ((ViewManager) otherw.getParent()).removeView(otherw);
                                //Log.i("starwisp","adding to " + id);

                                if (!behaviour_type.equals("drop-only-consume")) {
                                    v.addView(otherw);
                                }
                            }
                            otherw.setVisibility(View.VISIBLE);
                            return true;
                        }
                        case DragEvent.ACTION_DRAG_ENDED: {
                            //Log.i("starwisp","Drag ended "+v );
                            v.getBackground().setColorFilter(null);

                            View dragee = (View) event.getLocalState();
                            dragee.setVisibility(View.VISIBLE);

                            if (event.getResult()) {
                                //Log.i("starwisp","sucess " );
                            } else {
                                //Log.i("starwisp","fail " );
                            }
                            ;
                            return true;
                        }
                        // An unknown action type was received.
                        default:
                            //Log.e("starwisp","Unknown action type received by OnDragListener.");
                            break;
                        }
                        ;
                        return true;
                    }
                });
                return;
            }
        }

        if (type.equals("frame-layout")) {
            FrameLayout v = new FrameLayout(ctx);
            v.setId(arr.getInt(1));
            v.setLayoutParams(BuildLayoutParams(arr.getJSONArray(2)));
            parent.addView(v);
            JSONArray children = arr.getJSONArray(3);
            for (int i = 0; i < children.length(); i++) {
                Build(ctx, ctxname, new JSONArray(children.getString(i)), v);
            }
            return;
        }

        /*
        if (type.equals("grid-layout")) {
        GridLayout v = new GridLayout(ctx);
        v.setId(arr.getInt(1));
        v.setRowCount(arr.getInt(2));
        //v.setColumnCount(arr.getInt(2));
        v.setOrientation(BuildOrientation(arr.getString(3)));
        v.setLayoutParams(BuildLayoutParams(arr.getJSONArray(4)));
                
        parent.addView(v);
        JSONArray children = arr.getJSONArray(5);
        for (int i=0; i<children.length(); i++) {
            Build(ctx,ctxname,new JSONArray(children.getString(i)), v);
        }
                
        return;
        }
        */

        if (type.equals("scroll-view")) {
            HorizontalScrollView v = new HorizontalScrollView(ctx);
            v.setId(arr.getInt(1));
            v.setLayoutParams(BuildLayoutParams(arr.getJSONArray(2)));
            parent.addView(v);
            JSONArray children = arr.getJSONArray(3);
            for (int i = 0; i < children.length(); i++) {
                Build(ctx, ctxname, new JSONArray(children.getString(i)), v);
            }
            return;
        }

        if (type.equals("scroll-view-vert")) {
            ScrollView v = new ScrollView(ctx);
            v.setId(arr.getInt(1));
            v.setLayoutParams(BuildLayoutParams(arr.getJSONArray(2)));
            parent.addView(v);
            JSONArray children = arr.getJSONArray(3);
            for (int i = 0; i < children.length(); i++) {
                Build(ctx, ctxname, new JSONArray(children.getString(i)), v);
            }
            return;
        }

        if (type.equals("view-pager")) {
            ViewPager v = new ViewPager(ctx);
            v.setId(arr.getInt(1));
            v.setLayoutParams(BuildLayoutParams(arr.getJSONArray(2)));
            v.setOffscreenPageLimit(3);
            final JSONArray items = arr.getJSONArray(3);

            v.setAdapter(new FragmentPagerAdapter(ctx.getSupportFragmentManager()) {

                @Override
                public int getCount() {
                    return items.length();
                }

                @Override
                public Fragment getItem(int position) {
                    try {
                        String fragname = items.getString(position);
                        return ActivityManager.GetFragment(fragname);
                    } catch (JSONException e) {
                        Log.e("starwisp", "Error parsing fragment data " + e.toString());
                    }
                    return null;
                }
            });
            parent.addView(v);
            return;
        }

        if (type.equals("space")) {
            // Space v = new Space(ctx); (class not found runtime error??)
            TextView v = new TextView(ctx);
            v.setLayoutParams(BuildLayoutParams(arr.getJSONArray(2)));
            parent.addView(v);
        }

        if (type.equals("image-view")) {
            ImageView v = new ImageView(ctx);
            v.setId(arr.getInt(1));
            v.setLayoutParams(BuildLayoutParams(arr.getJSONArray(3)));
            v.setAdjustViewBounds(true);

            String image = arr.getString(2);

            if (image.startsWith("/")) {
                Bitmap b = BitmapCache.Load(image);
                if (b != null) {
                    v.setImageBitmap(b);
                }
            } else {
                int id = ctx.getResources().getIdentifier(image, "drawable", ctx.getPackageName());
                v.setImageResource(id);
            }

            parent.addView(v);
        }

        if (type.equals("image-button")) {
            ImageButton v = new ImageButton(ctx);
            v.setId(arr.getInt(1));
            v.setLayoutParams(BuildLayoutParams(arr.getJSONArray(3)));

            String image = arr.getString(2);

            if (image.startsWith("/")) {
                v.setImageBitmap(BitmapCache.Load(image));
            } else {
                int id = ctx.getResources().getIdentifier(image, "drawable", ctx.getPackageName());
                v.setImageResource(id);
            }

            final String fn = arr.getString(4);
            v.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Callback(ctx, ctxname, v.getId());
                }
            });

            v.setAdjustViewBounds(true);
            v.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

            parent.addView(v);
        }

        if (type.equals("text-view")) {
            TextView v = new TextView(ctx);
            v.setId(arr.getInt(1));
            v.setText(Html.fromHtml(arr.getString(2)), BufferType.SPANNABLE);
            v.setTextSize(arr.getInt(3));
            v.setLayoutParams(BuildLayoutParams(arr.getJSONArray(4)));
            v.setLinkTextColor(0xff00aa00);

            // uncomment all this to get hyperlinks to work in text...
            // should make this an option of course

            //v.setClickable(true); // make links
            //v.setMovementMethod(LinkMovementMethod.getInstance());
            //v.setEnabled(true);   // go to browser
            /*v.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View vv, MotionEvent event) {
                return false;
            }
            };*/

            if (arr.length() > 5) {
                if (arr.getString(5).equals("left")) {
                    v.setGravity(Gravity.LEFT);
                } else {
                    if (arr.getString(5).equals("fill")) {
                        v.setGravity(Gravity.FILL);
                    } else {
                        v.setGravity(Gravity.CENTER);
                    }
                }
            } else {
                v.setGravity(Gravity.CENTER);
            }
            v.setTypeface(((StarwispActivity) ctx).m_Typeface);
            parent.addView(v);
        }

        if (type.equals("debug-text-view")) {
            TextView v = (TextView) ctx.getLayoutInflater().inflate(R.layout.debug_text, null);
            //                v.setBackgroundResource(R.color.black);
            v.setId(arr.getInt(1));
            //                v.setText(Html.fromHtml(arr.getString(2)));
            //                v.setTextColor(R.color.white);
            //                v.setTextSize(arr.getInt(3));
            //                v.setMovementMethod(LinkMovementMethod.getInstance());
            //                v.setMaxLines(10);
            //                v.setVerticalScrollBarEnabled(true);
            //                v.setLayoutParams(BuildLayoutParams(arr.getJSONArray(4)));
            //v.setMovementMethod(new ScrollingMovementMethod());

            /*
            if (arr.length()>5) {
            if (arr.getString(5).equals("left")) {
                v.setGravity(Gravity.LEFT);
            } else {
                if (arr.getString(5).equals("fill")) {
                    v.setGravity(Gravity.FILL);
                } else {
                    v.setGravity(Gravity.CENTER);
                }
            }
            } else {
            v.setGravity(Gravity.LEFT);
            }
            v.setTypeface(((StarwispActivity)ctx).m_Typeface);*/
            parent.addView(v);
        }

        if (type.equals("web-view")) {
            WebView v = new WebView(ctx);
            v.setId(arr.getInt(1));
            v.setVerticalScrollBarEnabled(false);
            v.loadData(arr.getString(2), "text/html", "utf-8");
            v.setLayoutParams(BuildLayoutParams(arr.getJSONArray(3)));
            parent.addView(v);
        }

        if (type.equals("edit-text")) {
            final EditText v = new EditText(ctx);
            v.setId(arr.getInt(1));
            v.setText(arr.getString(2));
            v.setTextSize(arr.getInt(3));
            v.setGravity(Gravity.LEFT | Gravity.TOP);

            String inputtype = arr.getString(4);
            if (inputtype.equals("text")) {
                //v.setInputType(InputType.TYPE_CLASS_TEXT);
            } else if (inputtype.equals("numeric")) {
                v.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL
                        | InputType.TYPE_NUMBER_FLAG_SIGNED);
            } else if (inputtype.equals("email")) {
                v.setInputType(InputType.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS);
            }

            v.setLayoutParams(BuildLayoutParams(arr.getJSONArray(5)));
            v.setTypeface(((StarwispActivity) ctx).m_Typeface);
            final String fn = arr.getString(5);
            //v.setSingleLine(true);

            v.addTextChangedListener(new TextWatcher() {
                public void afterTextChanged(Editable s) {
                    CallbackArgs(ctx, ctxname, v.getId(), "\"" + s.toString() + "\"");
                }

                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                public void onTextChanged(CharSequence s, int start, int before, int count) {
                }
            });

            parent.addView(v);
        }

        if (type.equals("button")) {
            Button v = new Button(ctx);
            v.setId(arr.getInt(1));
            v.setText(arr.getString(2));
            v.setTextSize(arr.getInt(3));
            v.setLayoutParams(BuildLayoutParams(arr.getJSONArray(4)));
            v.setTypeface(((StarwispActivity) ctx).m_Typeface);
            final String fn = arr.getString(5);
            v.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Callback(ctx, ctxname, v.getId());
                }
            });

            parent.addView(v);
        }

        if (type.equals("colour-button")) {
            Button v = new Button(ctx);
            v.setId(arr.getInt(1));
            v.setText(arr.getString(2));
            v.setTextSize(arr.getInt(3));
            v.setLayoutParams(BuildLayoutParams(arr.getJSONArray(4)));
            v.setTypeface(((StarwispActivity) ctx).m_Typeface);
            JSONArray col = arr.getJSONArray(6);
            v.getBackground().setColorFilter(
                    Color.argb(col.getInt(3), col.getInt(0), col.getInt(1), col.getInt(2)),
                    PorterDuff.Mode.MULTIPLY);
            final String fn = arr.getString(5);
            v.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Callback(ctx, ctxname, v.getId());
                }
            });
            parent.addView(v);
        }

        if (type.equals("toggle-button")) {
            ToggleButton v = new ToggleButton(ctx);
            if (arr.getString(5).equals("fancy")) {
                v = (ToggleButton) ctx.getLayoutInflater().inflate(R.layout.toggle_button_fancy, null);
            }

            if (arr.getString(5).equals("yes")) {
                v = (ToggleButton) ctx.getLayoutInflater().inflate(R.layout.toggle_button_yes, null);
            }

            if (arr.getString(5).equals("maybe")) {
                v = (ToggleButton) ctx.getLayoutInflater().inflate(R.layout.toggle_button_maybe, null);
            }

            if (arr.getString(5).equals("no")) {
                v = (ToggleButton) ctx.getLayoutInflater().inflate(R.layout.toggle_button_no, null);
            }

            v.setId(arr.getInt(1));
            v.setText(arr.getString(2));
            v.setTextSize(arr.getInt(3));
            v.setLayoutParams(BuildLayoutParams(arr.getJSONArray(4)));
            v.setTypeface(((StarwispActivity) ctx).m_Typeface);
            final String fn = arr.getString(6);
            v.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    String arg = "#f";
                    if (((ToggleButton) v).isChecked())
                        arg = "#t";
                    CallbackArgs(ctx, ctxname, v.getId(), arg);
                }
            });
            parent.addView(v);
        }

        if (type.equals("seek-bar")) {
            SeekBar v = new SeekBar(ctx);
            v.setId(arr.getInt(1));
            v.setMax(arr.getInt(2));
            v.setProgress(arr.getInt(2) / 2);
            v.setLayoutParams(BuildLayoutParams(arr.getJSONArray(3)));
            final String fn = arr.getString(4);

            v.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                public void onProgressChanged(SeekBar v, int a, boolean s) {
                    CallbackArgs(ctx, ctxname, v.getId(), Integer.toString(a));
                }

                public void onStartTrackingTouch(SeekBar v) {
                }

                public void onStopTrackingTouch(SeekBar v) {
                }
            });
            parent.addView(v);
        }

        if (type.equals("spinner")) {
            Spinner v = new Spinner(ctx);
            final int wid = arr.getInt(1);
            v.setId(wid);
            final JSONArray items = arr.getJSONArray(2);
            v.setLayoutParams(BuildLayoutParams(arr.getJSONArray(3)));
            v.setMinimumWidth(100); // stops tiny buttons
            ArrayList<String> spinnerArray = new ArrayList<String>();

            for (int i = 0; i < items.length(); i++) {
                spinnerArray.add(items.getString(i));
            }

            ArrayAdapter spinnerArrayAdapter = new ArrayAdapter<String>(ctx, R.layout.spinner_item,
                    spinnerArray) {
                public View getView(int position, View convertView, ViewGroup parent) {
                    View v = super.getView(position, convertView, parent);
                    ((TextView) v).setTypeface(((StarwispActivity) ctx).m_Typeface);
                    return v;
                }
            };

            spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_layout);

            v.setAdapter(spinnerArrayAdapter);
            v.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                public void onItemSelected(AdapterView<?> a, View v, int pos, long id) {
                    CallbackArgs(ctx, ctxname, wid, "" + pos);
                }

                public void onNothingSelected(AdapterView<?> v) {
                }
            });

            parent.addView(v);
        }

        if (type.equals("nomadic")) {
            final int wid = arr.getInt(1);
            NomadicSurfaceView v = new NomadicSurfaceView(ctx, wid);
            v.setId(wid);
            v.setLayoutParams(BuildLayoutParams(arr.getJSONArray(2)));
            Log.e("starwisp", "built the thing");
            parent.addView(v);
            Log.e("starwisp", "addit to the view");
        }

        if (type.equals("canvas")) {
            StarwispCanvas v = new StarwispCanvas(ctx);
            final int wid = arr.getInt(1);
            v.setId(wid);
            v.setLayoutParams(BuildLayoutParams(arr.getJSONArray(2)));
            v.SetDrawList(arr.getJSONArray(3));
            parent.addView(v);
        }

        if (type.equals("camera-preview")) {
            PictureTaker pt = new PictureTaker();
            CameraPreview v = new CameraPreview(ctx, pt);
            final int wid = arr.getInt(1);
            v.setId(wid);
            v.setLayoutParams(BuildLayoutParams(arr.getJSONArray(2)));
            parent.addView(v);

            Log.i("starwisp", "in camera-preview...");

            List<List<String>> info = v.mPictureTaker.GetInfo();
            // can't find a way to do this via a callback yet
            String arg = "'(";
            for (List<String> e : info) {
                arg += "(" + e.get(0) + " " + e.get(1) + ")";
                //Log.i("starwisp","converting prop "+arg);
            }
            arg += ")";
            m_Scheme.eval("(set! camera-properties " + arg + ")");
        }

        if (type.equals("button-grid")) {
            LinearLayout horiz = new LinearLayout(ctx);
            final int id = arr.getInt(1);
            final String buttontype = arr.getString(2);
            horiz.setId(id);
            horiz.setOrientation(LinearLayout.HORIZONTAL);
            parent.addView(horiz);
            int height = arr.getInt(3);
            int textsize = arr.getInt(4);
            LayoutParams lp = BuildLayoutParams(arr.getJSONArray(5));
            JSONArray buttons = arr.getJSONArray(6);
            int count = buttons.length();
            int vertcount = 0;
            LinearLayout vert = null;

            for (int i = 0; i < count; i++) {
                JSONArray button = buttons.getJSONArray(i);

                if (vertcount == 0) {
                    vert = new LinearLayout(ctx);
                    vert.setId(0);
                    vert.setOrientation(LinearLayout.VERTICAL);
                    horiz.addView(vert);
                }
                vertcount = (vertcount + 1) % height;

                if (buttontype.equals("button")) {
                    Button b = new Button(ctx);
                    b.setId(button.getInt(0));
                    b.setText(button.getString(1));
                    b.setTextSize(textsize);
                    b.setLayoutParams(lp);
                    b.setTypeface(((StarwispActivity) ctx).m_Typeface);
                    final String fn = arr.getString(6);
                    b.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            CallbackArgs(ctx, ctxname, id, "" + v.getId() + " #t");
                        }
                    });
                    vert.addView(b);
                } else if (buttontype.equals("toggle")) {
                    ToggleButton b = new ToggleButton(ctx);
                    b.setId(button.getInt(0));
                    b.setText(button.getString(1));
                    b.setTextSize(textsize);
                    b.setLayoutParams(lp);
                    b.setTypeface(((StarwispActivity) ctx).m_Typeface);
                    final String fn = arr.getString(6);
                    b.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            String arg = "#f";
                            if (((ToggleButton) v).isChecked())
                                arg = "#t";
                            CallbackArgs(ctx, ctxname, id, "" + v.getId() + " " + arg);
                        }
                    });
                    vert.addView(b);
                }
            }
        }

    } catch (JSONException e) {
        Log.e("starwisp", "Error parsing [" + arr.toString() + "] " + e.toString());
    }

    //Log.i("starwisp","building ended");

}

From source file:app.umitems.greenclock.widget.sgv.StaggeredGridView.java

@Override
public boolean dispatchDragEvent(DragEvent event) {
    if (!isDragReorderingSupported()) {
        // If the consumer of this StaggeredGridView has not registered a ReorderListener,
        // don't bother handling drag events.
        return super.dispatchDragEvent(event);
    }//  w  w w.  ja  v  a2s. c  o m

    switch (event.getAction()) {
    case DragEvent.ACTION_DRAG_STARTED:
        // Per bug 7071594, we won't be able to catch this event in onDragEvent,
        // so we'll handle the event as it is being dispatched on the way down.
        if (mReorderHelper.hasReorderListener() && mIsDragReorderingEnabled) {
            final View child = getChildAtCoordinate(mTouchDownForDragStartX, mTouchDownForDragStartY);
            if (child != null) {
                // Child can be null if the touch point is not on a child view, but is
                // still within the bounds of this StaggeredGridView (i.e., margins
                // between cells).
                startDragging(child, mTouchDownForDragStartX, mTouchDownForDragStartY);
                // We must return true in order to continue getting future
                // {@link DragEvent}s.
                return true;
            }
        }
        // Be sure to return a value here instead of calling super.dispatchDragEvent()
        // which will unnecessarily dispatch to all the children (since the
        // {@link StaggeredGridView} handles all drag events for our purposes)
        return false;

    case DragEvent.ACTION_DROP:
    case DragEvent.ACTION_DRAG_ENDED:
        if (mDragState == ReorderUtils.DRAG_STATE_DRAGGING) {
            handleDrop((int) event.getX(), (int) event.getY());
        }

        // Return early here to avoid calling super.dispatchDragEvent() which dispatches to
        // children (since this view already can handle all drag events). The super call
        // can also cause a NPE if the view hierarchy changed in the middle of a drag
        // and the {@link DragEvent} gets nulled out. This is a workaround for
        // a framework bug: 8298439.
        // Since the {@link StaggeredGridView} handles all drag events for our purposes,
        // just manually fire the drag event to ourselves.
        return onDragEvent(event);
    }

    // In all other cases, default to the superclass implementation. We need this so that
    // the drag/drop framework will fire off {@link #onDragEvent(DragEvent ev)} calls to us.
    return super.dispatchDragEvent(event);
}