Example usage for android.view View setTouchDelegate

List of usage examples for android.view View setTouchDelegate

Introduction

In this page you can find the example usage for android.view View setTouchDelegate.

Prototype

public void setTouchDelegate(TouchDelegate delegate) 

Source Link

Document

Sets the TouchDelegate for this View.

Usage

From source file:Main.java

public static void enlargeBtnTouchScope(View parent, Button btn, Rect rect, float width, float height) {
    rect.top = btn.getTop();//from ww w .  j ava2  s  . c  o m
    rect.bottom = btn.getBottom();
    rect.left = btn.getLeft();
    rect.right = btn.getRight();

    rect.top -= height;
    rect.bottom += height;
    rect.left -= width;
    rect.right += width;

    parent.setTouchDelegate(new TouchDelegate(rect, btn));
}

From source file:cl.monsoon.s1next.binding.TextViewBindingAdapter.java

@BindingAdapter("increaseClickingArea")
public static void increaseClickingArea(TextView textView, float size) {
    // fork from http://stackoverflow.com/a/1343796
    View parent = (View) textView.getParent();
    // post in the parent's message queue to make sure the parent
    // lays out its children before we call View#getHitRect()
    parent.post(() -> {// w ww.ja v a  2s . com
        final int halfSize = (int) (size / 2 + 0.5);
        Rect rect = new Rect();
        textView.getHitRect(rect);
        rect.top -= halfSize;
        rect.right += halfSize;
        rect.bottom += halfSize;
        rect.left -= halfSize;
        // use TouchDelegate to increase count's clicking area
        parent.setTouchDelegate(new TouchDelegate(rect, textView));
    });
}

From source file:Main.java

/**
 * @param top/*from  www  . j a  v  a2  s  .c  om*/
 * @param left
 * @param bottom
 * @param right
 * @param delegate
 */
public static void increaseHitRectBy(final int top, final int left, final int bottom, final int right,
        final View delegate) {
    final View parent = (View) delegate.getParent();
    if (parent != null && delegate.getContext() != null) {
        parent.post(new Runnable() {
            // Post in the parent's message queue to make sure the parent
            // lays out its children before we call getHitRect()
            public void run() {
                final float densityDpi = delegate.getContext().getResources().getDisplayMetrics().densityDpi;
                final Rect r = new Rect();
                delegate.getHitRect(r);
                r.top -= transformToDensityPixel(top, densityDpi);
                r.left -= transformToDensityPixel(left, densityDpi);
                r.bottom += transformToDensityPixel(bottom, densityDpi);
                r.right += transformToDensityPixel(right, densityDpi);
                parent.setTouchDelegate(new TouchDelegate(r, delegate));
            }
        });
    }
}

From source file:com.sutromedia.android.core.PhotoActivity.java

private void setupTouchOnPlayButton() {
    View playButton = findViewById(R.image.play_slideshow);
    Rect expandedSize = new Rect();
    playButton.getHitRect(expandedSize);
    expandedSize.inset(30, 30);/*w w  w  . j a va2  s  .c  o  m*/
    TouchDelegate delegate = new TouchDelegate(expandedSize, playButton);
    View parent = (View) playButton.getParent();
    parent.setTouchDelegate(delegate);
}

From source file:com.tingtingapps.securesms.ContactSelectionActivity.java

private void expandTapArea(final View container, final View child, final int padding) {
    container.post(new Runnable() {
        @Override//  ww w  .jav  a2 s  .  co m
        public void run() {
            Rect rect = new Rect();
            child.getHitRect(rect);

            rect.top -= padding;
            rect.left -= padding;
            rect.right += padding;
            rect.bottom += padding;

            container.setTouchDelegate(new TouchDelegate(rect, child));
        }
    });
}

From source file:com.xandy.calendar.selectcalendars.SelectCalendarsSyncAdapter.java

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    if (position >= mRowCount) {
        return null;
    }/*from   w  w w  .  jav a  2s .c om*/
    String name = mData[position].displayName;
    boolean selected = mData[position].synced;
    int color = Utils.getDisplayColorFromColor(mData[position].color);
    View view;
    if (convertView == null) {
        view = mInflater.inflate(LAYOUT, parent, false);
        final View delegate = view.findViewById(R.id.color);
        final View delegateParent = (View) delegate.getParent();
        delegateParent.post(new Runnable() {

            @Override
            public void run() {
                final Rect r = new Rect();
                delegate.getHitRect(r);
                r.top -= mColorViewTouchAreaIncrease;
                r.bottom += mColorViewTouchAreaIncrease;
                r.left -= mColorViewTouchAreaIncrease;
                r.right += mColorViewTouchAreaIncrease;
                delegateParent.setTouchDelegate(new TouchDelegate(r, delegate));
            }
        });
    } else {
        view = convertView;
    }

    view.setTag(mData[position]);

    CheckBox cb = (CheckBox) view.findViewById(R.id.sync);
    cb.setChecked(selected);

    if (selected) {
        setText(view, R.id.status, mSyncedString);
    } else {
        setText(view, R.id.status, mNotSyncedString);
    }

    View colorView = view.findViewById(R.id.color);
    colorView.setEnabled(hasMoreColors(position));
    colorView.setBackgroundColor(color);
    colorView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // Purely for sanity check--view should be disabled if account has no more colors
            if (!hasMoreColors(position)) {
                return;
            }

            if (mColorPickerDialog == null) {
                mColorPickerDialog = CalendarColorPickerDialog.newInstance(mData[position].id, mIsTablet);
            } else {
                mColorPickerDialog.setCalendarId(mData[position].id);
            }
            mFragmentManager.executePendingTransactions();
            if (!mColorPickerDialog.isAdded()) {
                mColorPickerDialog.show(mFragmentManager, COLOR_PICKER_DIALOG_TAG);
            }
        }
    });

    setText(view, R.id.calendar, name);
    return view;
}

From source file:com.android.calendar.selectcalendars.SelectSyncedCalendarsMultiAccountAdapter.java

@Override
protected void bindChildView(View view, Context context, Cursor cursor, boolean isLastChild) {
    final long id = cursor.getLong(ID_COLUMN);
    String name = cursor.getString(NAME_COLUMN);
    String owner = cursor.getString(OWNER_COLUMN);
    final String accountName = cursor.getString(ACCOUNT_COLUMN);
    final String accountType = cursor.getString(ACCOUNT_TYPE_COLUMN);
    int color = Utils.getDisplayColorFromColor(cursor.getInt(COLOR_COLUMN));

    final View colorSquare = view.findViewById(R.id.color);
    colorSquare.setEnabled(mCache.hasColors(accountName, accountType));
    colorSquare.setBackgroundColor(color);
    final View delegateParent = (View) colorSquare.getParent();
    delegateParent.post(new Runnable() {

        @Override// www  . ja v  a  2 s  .  c o  m
        public void run() {
            final Rect r = new Rect();
            colorSquare.getHitRect(r);
            r.top -= mColorViewTouchAreaIncrease;
            r.bottom += mColorViewTouchAreaIncrease;
            r.left -= mColorViewTouchAreaIncrease;
            r.right += mColorViewTouchAreaIncrease;
            delegateParent.setTouchDelegate(new TouchDelegate(r, colorSquare));
        }
    });
    colorSquare.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (!mCache.hasColors(accountName, accountType)) {
                return;
            }
            if (mColorPickerDialog == null) {
                mColorPickerDialog = CalendarColorPickerDialog.newInstance(id, mIsTablet);
            } else {
                mColorPickerDialog.setCalendarId(id);
            }
            mFragmentManager.executePendingTransactions();
            if (!mColorPickerDialog.isAdded()) {
                mColorPickerDialog.show(mFragmentManager, COLOR_PICKER_DIALOG_TAG);
            }
        }
    });
    if (mIsDuplicateName.containsKey(name) && mIsDuplicateName.get(name) && !name.equalsIgnoreCase(owner)) {
        name = new StringBuilder(name).append(Utils.OPEN_EMAIL_MARKER).append(owner)
                .append(Utils.CLOSE_EMAIL_MARKER).toString();
    }
    setText(view, R.id.calendar, name);

    // First see if the user has already changed the state of this calendar
    Boolean sync = mCalendarChanges.get(id);
    if (sync == null) {
        sync = cursor.getInt(SYNCED_COLUMN) == 1;
        mCalendarInitialStates.put(id, sync);
    }

    CheckBox button = (CheckBox) view.findViewById(R.id.sync);
    button.setChecked(sync);
    setText(view, R.id.status, sync ? mSyncedText : mNotSyncedText);

    view.setTag(TAG_ID_CALENDAR_ID, id);
    view.setTag(TAG_ID_SYNC_CHECKBOX, button);
    view.setOnClickListener(this);
}

From source file:com.android.calendar.selectcalendars.SelectCalendarsSimpleAdapter.java

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    if (position >= mRowCount) {
        return null;
    }/*ww w. j  av a  2 s  . c  om*/
    String name = mData[position].displayName;
    boolean selected = mData[position].selected;

    int color = Utils.getDisplayColorFromColor(mData[position].color);
    View view;
    if (convertView == null) {
        view = mInflater.inflate(mLayout, parent, false);
        final View delegate = view.findViewById(R.id.color);
        final View delegateParent = (View) delegate.getParent();
        delegateParent.post(new Runnable() {

            @Override
            public void run() {
                final Rect r = new Rect();
                delegate.getHitRect(r);
                r.top -= mColorViewTouchAreaIncrease;
                r.bottom += mColorViewTouchAreaIncrease;
                r.left -= mColorViewTouchAreaIncrease;
                r.right += mColorViewTouchAreaIncrease;
                delegateParent.setTouchDelegate(new TouchDelegate(r, delegate));
            }
        });
    } else {
        view = convertView;
    }

    TextView calendarName = (TextView) view.findViewById(R.id.calendar);
    calendarName.setText(name);

    View colorView = view.findViewById(R.id.color);
    colorView.setBackgroundColor(color);
    colorView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // Purely for sanity check--view should be disabled if account has no more colors
            if (!hasMoreColors(position)) {
                return;
            }

            if (mColorPickerDialog == null) {
                mColorPickerDialog = CalendarColorPickerDialog.newInstance(mData[position].id, mIsTablet);
            } else {
                mColorPickerDialog.setCalendarId(mData[position].id);
            }
            mFragmentManager.executePendingTransactions();
            if (!mColorPickerDialog.isAdded()) {
                mColorPickerDialog.show(mFragmentManager, COLOR_PICKER_DIALOG_TAG);
            }
        }
    });

    int textColor;
    if (selected) {
        textColor = mColorCalendarVisible;
    } else {
        textColor = mColorCalendarHidden;
    }
    calendarName.setTextColor(textColor);

    CheckBox syncCheckBox = (CheckBox) view.findViewById(R.id.sync);
    if (syncCheckBox != null) {

        // Full screen layout
        syncCheckBox.setChecked(selected);

        colorView.setEnabled(hasMoreColors(position));
        LayoutParams layoutParam = calendarName.getLayoutParams();
        TextView secondaryText = (TextView) view.findViewById(R.id.status);
        if (!TextUtils.isEmpty(mData[position].ownerAccount) && !mData[position].ownerAccount.equals(name)
                && !mData[position].ownerAccount.endsWith("calendar.google.com")) {
            int secondaryColor;
            if (selected) {
                secondaryColor = mColorCalendarSecondaryVisible;
            } else {
                secondaryColor = mColorCalendarSecondaryHidden;
            }
            secondaryText.setText(mData[position].ownerAccount);
            secondaryText.setTextColor(secondaryColor);
            secondaryText.setVisibility(View.VISIBLE);
            layoutParam.height = LayoutParams.WRAP_CONTENT;
        } else {
            secondaryText.setVisibility(View.GONE);
            layoutParam.height = LayoutParams.MATCH_PARENT;
        }

        calendarName.setLayoutParams(layoutParam);

    } else {
        // Tablet layout
        view.findViewById(R.id.color).setEnabled(selected && hasMoreColors(position));
        view.setBackgroundDrawable(getBackground(position, selected));
        ViewGroup.LayoutParams newParams = view.getLayoutParams();
        if (position == mData.length - 1) {
            newParams.height = BOTTOM_ITEM_HEIGHT;
        } else {
            newParams.height = NORMAL_ITEM_HEIGHT;
        }
        view.setLayoutParams(newParams);
        CheckBox visibleCheckBox = (CheckBox) view.findViewById(R.id.visible_check_box);
        if (visibleCheckBox != null) {
            visibleCheckBox.setChecked(selected);
        }
    }
    view.invalidate();
    return view;
}