Java Code Examples for android.widget.Button#setOnTouchListener()

The following examples show how to use android.widget.Button#setOnTouchListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MainActivity.java    From journaldev with MIT License 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button button = findViewById(R.id.button);
    button.setOnTouchListener(new OnSwipeTouchListener(this) {
        public void onSwipeTop() {
            Toast.makeText(getApplicationContext(), "Swiped top", Toast.LENGTH_SHORT).show();
        }

        public void onSwipeRight() {
            Toast.makeText(getApplicationContext(), "Swiped right", Toast.LENGTH_SHORT).show();
        }

        public void onSwipeLeft() {
            Toast.makeText(getApplicationContext(), "Swiped left", Toast.LENGTH_SHORT).show();
        }

        public void onSwipeBottom() {
            Toast.makeText(getApplicationContext(), "Swiped bottom", Toast.LENGTH_SHORT).show();
        }

    });
}
 
Example 2
Source File: TestActivity.java    From android-art-res with Apache License 2.0 6 votes vote down vote up
public void onButtonClick(View v) {
    if (v == mCreateWindowButton) {
        mFloatingButton = new Button(this);
        mFloatingButton.setText("click me");
        mLayoutParams = new WindowManager.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0, 0,
                PixelFormat.TRANSPARENT);
        mLayoutParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL
                | LayoutParams.FLAG_NOT_FOCUSABLE
                | LayoutParams.FLAG_SHOW_WHEN_LOCKED;
        mLayoutParams.type = LayoutParams.TYPE_SYSTEM_ERROR;
        mLayoutParams.gravity = Gravity.LEFT | Gravity.TOP;
        mLayoutParams.x = 100;
        mLayoutParams.y = 300;
        mFloatingButton.setOnTouchListener(this);
        mWindowManager.addView(mFloatingButton, mLayoutParams);
    }
}
 
Example 3
Source File: SecureView.java    From codeexamples-android with Eclipse Public License 1.0 6 votes vote down vote up
private void setTouchFilter(Button button) {
    button.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if ((event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    new AlertDialog.Builder(SecureView.this)
                        .setTitle(R.string.secure_view_caught_dialog_title)
                        .setMessage(R.string.secure_view_caught_dialog_message)
                        .setNeutralButton(getResources().getString(
                                R.string.secure_view_caught_dialog_dismiss), null)
                        .show();
                }
                // Return true to prevent the button from processing the touch.
                return true;
            }
            return false;
        }
    });
}
 
Example 4
Source File: WindowActivity.java    From android-open-source-project-analysis with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_window);

    findViewById(R.id.btn_add_view).setOnClickListener(this);
    findViewById(R.id.btn_remove_view).setOnClickListener(this);

    windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
    layoutParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT
            , WindowManager.LayoutParams.WRAP_CONTENT, 0, 0, PixelFormat.TRANSLUCENT);
    layoutParams.x = 100;
    layoutParams.y = 100;
    layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
    layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION;

    button = new Button(this);
    button.setText("Button");

    button.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_MOVE:
                    if (windowManager != null) {
                        layoutParams.x = (int) event.getRawX();
                        layoutParams.y = (int) event.getRawY();
                        windowManager.updateViewLayout(button, layoutParams);
                    }
                    break;
            }

            return false;
        }
    });
}
 
Example 5
Source File: MainActivity.java    From AndroidAnimations with Apache License 2.0 5 votes vote down vote up
@Override protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  Button button = (Button) findViewById(R.id.btn_rebound);
  button.setOnTouchListener(new Rebound.SpringyTouchListener() {

    @Override public void onClick(View v) {
      startActivity(new Intent(MainActivity.this, ReboundDemoActivity.class));
    }
  });

}
 
Example 6
Source File: SeekBarArrows.java    From FaceRecognitionApp with GNU General Public License v2.0 5 votes vote down vote up
OnArrowListener(View v, SeekBar mSeekbar, boolean positive) {
    Button mButton = (Button) v;
    this.mSeekbar = mSeekbar;
    this.positive = positive;

    mButton.setOnClickListener(this);
    mButton.setOnLongClickListener(this);
    mButton.setOnTouchListener(this);
}
 
Example 7
Source File: BroadcastActivity.java    From cineio-broadcast-android with MIT License 5 votes vote down vote up
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        Bundle extras = getIntent().getExtras();
        int layout = extras.getInt("LAYOUT", R.layout.activity_broadcast_capture);
        setContentView(layout);
        initializeEncodingConfig(extras);
        initializeMuxer();
        initializeAudio();
        initializeVideo();
        initializeGLView();
//        setButtonHolderLayout();
        // http://stackoverflow.com/questions/5975168/android-button-setpressed-after-onclick
        Button toggleRecording = (Button) findViewById(R.id.toggleRecording_button);

        toggleRecording.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // show interest in events resulting from ACTION_DOWN
                if(event.getAction()==MotionEvent.ACTION_DOWN) return true;
                // don't handle event unless its ACTION_UP so "doSomething()" only runs once.
                if(event.getAction()!=MotionEvent.ACTION_UP) return false;
                toggleRecordingHandler();
                return true;
            }
        });

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        Log.d(TAG, "onCreate complete: " + this);
    }
 
Example 8
Source File: IMSingleNumber.java    From opensudoku with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected View createControlPanelView() {
    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View controlPanel = inflater.inflate(R.layout.im_single_number, null);

    mNumberButtons = new HashMap<>();
    mNumberButtons.put(1, controlPanel.findViewById(R.id.button_1));
    mNumberButtons.put(2, controlPanel.findViewById(R.id.button_2));
    mNumberButtons.put(3, controlPanel.findViewById(R.id.button_3));
    mNumberButtons.put(4, controlPanel.findViewById(R.id.button_4));
    mNumberButtons.put(5, controlPanel.findViewById(R.id.button_5));
    mNumberButtons.put(6, controlPanel.findViewById(R.id.button_6));
    mNumberButtons.put(7, controlPanel.findViewById(R.id.button_7));
    mNumberButtons.put(8, controlPanel.findViewById(R.id.button_8));
    mNumberButtons.put(9, controlPanel.findViewById(R.id.button_9));
    mNumberButtons.put(0, controlPanel.findViewById(R.id.button_clear));

    for (Integer num : mNumberButtons.keySet()) {
        Button b = mNumberButtons.get(num);
        b.setTag(num);
        b.setOnClickListener(mNumberButtonClicked);
        b.setOnTouchListener(mNumberButtonTouched);
    }

    mSwitchNumNoteButton = controlPanel.findViewById(R.id.switch_num_note);
    mSwitchNumNoteButton.setOnClickListener(v -> {
        mEditMode = mEditMode == MODE_EDIT_VALUE ? MODE_EDIT_NOTE : MODE_EDIT_VALUE;
        update();
    });

    return controlPanel;
}
 
Example 9
Source File: RegisterActivity.java    From solutions-mobile-shopping-assistant-backend-java with Apache License 2.0 5 votes vote down vote up
private void updateState(State newState) {
  Button registerButton = (Button) findViewById(R.id.regButton);
  switch (newState) {
  case REGISTERED:
    registerButton.setText("Unregister");
    registerButton.setOnTouchListener(unregisterListener);
    registerButton.setEnabled(true);
    break;

  case REGISTERING:
    registerButton.setText("Registering...");
    registerButton.setEnabled(false);
    break;

  case UNREGISTERED:
    registerButton.setText("Register");
    registerButton.setOnTouchListener(registerListener);
    registerButton.setEnabled(true);
    break;

  case UNREGISTERING:
    registerButton.setText("Unregistering...");
    registerButton.setEnabled(false);
    break;
  }
  curState = newState;
}
 
Example 10
Source File: ChatActivity.java    From TestChat with Apache License 2.0 4 votes vote down vote up
private void initBottomView() {
        add = (Button) findViewById(R.id.btn_chat_bottom_add);
        emotion = (Button) findViewById(R.id.btn_chat_bottom_emotion);
        input = (EditText) findViewById(R.id.et_chat_bottom_input);
        speak = (Button) findViewById(R.id.btn_chat_bottom_speak);
        send = (Button) findViewById(R.id.btn_chat_bottom_send);
        voice = (Button) findViewById(R.id.btn_chat_bottom_voice);
        keyboard = (Button) findViewById(R.id.btn_chat_bottom_keyboard);
        l1_more = (LinearLayout) findViewById(R.id.l1_chat_bottom_more);
        mViewPager = (ViewPager) findViewById(R.id.vp_chat_bottom_emotion);
        r1_emotion = (RelativeLayout) findViewById(R.id.r1_chat_bottom_emotion);
        l1_add = (LinearLayout) findViewById(R.id.l1_chat_bottom_add);
        picture = (TextView) findViewById(R.id.tv_chat_bottom_picture);
        camera = (TextView) findViewById(R.id.tv_chat_bottom_camera);
        location = (TextView) findViewById(R.id.tv_chat_bottom_location);
        add.setOnClickListener(this);
        emotion.setOnClickListener(this);
        input.addTextChangedListener(this);
        input.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                        if (hasFocus) {
                                LogUtil.e("聚焦");
                                scrollToBottom();
                                if (l1_more.getVisibility() == View.VISIBLE) {
                                        l1_more.setVisibility(View.GONE);
                                }
                        }
                }
        });
        voice.setOnClickListener(this);
        send.setOnClickListener(this);
        keyboard.setOnClickListener(this);
        location.setOnClickListener(this);
        picture.setOnClickListener(this);
        camera.setOnClickListener(this);
        speak.setOnTouchListener(this);
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                        mSwipeRefreshLayout.setRefreshing(true);
                        BaseMessage message = mAdapter.getData(0);
                        LoadMessage(message);
                }
        });
}
 
Example 11
Source File: ChatKeyboardLayout.java    From ChatKeyboard with Apache License 2.0 4 votes vote down vote up
private void initView(Context context) {
    // must be in front of inflate
    EmoticonHandler.getInstance(context).loadEmoticonsToMemory();
    LayoutInflater.from(context).inflate(R.layout.keyboard_bar_layout, this);

    rlInput = (ViewGroup) findViewById(R.id.view_keyboard_input_layout);
    lyBottomLayout = (LinearLayout) findViewById(R.id.view_keyboard_bottom);
    btnEmoticon = (ImageView) findViewById(R.id.view_keyboard_face_icon);
    leftIconView = (ImageView) findViewById(R.id.view_keyboard_left_icon);
    btnRecording = (Button) findViewById(R.id.view_keyboard_recording_bar);
    rightIconView = (ImageView) findViewById(R.id.view_keyboard_right_icon);
    btnSend = (Button) findViewById(R.id.view_keyboard_send_button);

    etInputArea = (HadEditText) findViewById(R.id.et_chat);

    setAutoHeightLayoutView(lyBottomLayout);
    leftIconView.setOnClickListener(new LeftIconClickListener());
    rightIconView.setOnClickListener(new RightIconClickListener());
    rightIconView.setVisibility(GONE);
    btnEmoticon.setOnClickListener(new FaceClickListener());
    btnEmoticon.setVisibility(GONE);
    btnSend.setOnClickListener(new SendClickListener());
    btnRecording.setOnTouchListener(new RecordingTouchListener());

    etInputArea.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (!etInputArea.isFocused()) {
                etInputArea.setFocusable(true);
                etInputArea.setFocusableInTouchMode(true);
            }
            return false;
        }
    });
    etInputArea.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean b) {
            if (b) {
                setEditableState(true);
            } else {
                setEditableState(false);
            }
        }
    });
    etInputArea.setOnTextChangedInterface(new HadEditText.OnTextChangedInterface() {
        @Override
        public void onTextChanged(CharSequence arg0) {
            String str = arg0.toString();
            if (mOnChatKeyBoardListener != null) {
                mOnChatKeyBoardListener.onInputTextChanged(str);
            }
            if (!isShowRightIcon) {
                return;
            }
            if (TextUtils.isEmpty(str)) {
                rightIconView.setVisibility(VISIBLE);
                btnSend.setVisibility(GONE);
            } else {
                rightIconView.setVisibility(GONE);
                btnSend.setVisibility(VISIBLE);
            }
        }
    });
}
 
Example 12
Source File: FloatMeasurementView.java    From openScale with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected View getInputView() {
    final LinearLayout view = (LinearLayout) LayoutInflater.from(getContext())
            .inflate(R.layout.float_input_view, null);

    final EditText input = view.findViewById(R.id.float_input);
    input.setText(formatValue(value));

    final TextView unit = view.findViewById(R.id.float_input_unit);
    unit.setText(getUnit());

    if (getDecimalPlaces() == 0) {
        INC_DEC_DELTA = 10.0f;
    } else {
        INC_DEC_DELTA = 0.1f;
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View button) {
            float newValue = validateAndGetInput(view);
            if (newValue < 0) {
                return;
            }

            if (button.getId() == R.id.btn_inc) {
                newValue += INC_DEC_DELTA;
            }
            else {
                newValue -= INC_DEC_DELTA;
            }

            input.setText(formatValue(clampValue(newValue)));
            input.selectAll();
        }
    };

    RepeatListener repeatListener =
            new RepeatListener(400, 100, onClickListener);

    final Button inc = view.findViewById(R.id.btn_inc);
    inc.setText("\u25b2 +" + formatValue(INC_DEC_DELTA));
    inc.setOnClickListener(onClickListener);
    inc.setOnTouchListener(repeatListener);

    final Button dec = view.findViewById(R.id.btn_dec);
    dec.setText("\u25bc -" + formatValue(INC_DEC_DELTA));
    dec.setOnClickListener(onClickListener);
    dec.setOnTouchListener(repeatListener);

    return view;
}
 
Example 13
Source File: RegisterActivity.java    From solutions-mobile-shopping-assistant-backend-java with Apache License 2.0 4 votes vote down vote up
@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_register);
  Button regButton = (Button) findViewById(R.id.regButton);

  registerListener = new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
      switch (event.getAction() & MotionEvent.ACTION_MASK) {
      case MotionEvent.ACTION_DOWN:
        if (GCMIntentService.PROJECT_NUMBER == null
            || GCMIntentService.PROJECT_NUMBER.length() == 0) {
          showDialog("Unable to register for Google Cloud Messaging. "
              + "Your application's PROJECT_NUMBER field is unset! You can change "
              + "it in GCMIntentService.java");
        } else {
          updateState(State.REGISTERING);
          try {
            GCMIntentService.register(getApplicationContext());
          } catch (Exception e) {
            Log.e(RegisterActivity.class.getName(),
                "Exception received when attempting to register for Google Cloud "
                    + "Messaging. Perhaps you need to set your virtual device's "
                    + " target to Google APIs? "
                    + "See https://developers.google.com/eclipse/docs/cloud_endpoints_android"
                    + " for more information.", e);
            showDialog("There was a problem when attempting to register for "
                + "Google Cloud Messaging. If you're running in the emulator, "
                + "is the target of your virtual device set to 'Google APIs?' "
                + "See the Android log for more details.");
            updateState(State.UNREGISTERED);
          }
        }
        return true;
      case MotionEvent.ACTION_UP:
        return true;
      default:
        return false;
      }
    }
  };

  unregisterListener = new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
      switch (event.getAction() & MotionEvent.ACTION_MASK) {
      case MotionEvent.ACTION_DOWN:
        updateState(State.UNREGISTERING);
        GCMIntentService.unregister(getApplicationContext());
        return true;
      case MotionEvent.ACTION_UP:
        return true;
      default:
        return false;
      }
    }
  };

  regButton.setOnTouchListener(registerListener);
  
  /*
   * build the messaging endpoint so we can access old messages via an endpoint call
   */
  MessageEndpoint.Builder endpointBuilder = new MessageEndpoint.Builder(
      AndroidHttp.newCompatibleTransport(),
      new JacksonFactory(),
      new HttpRequestInitializer() {
        public void initialize(HttpRequest httpRequest) { }
      });

  messageEndpoint = CloudEndpointUtils.updateBuilder(endpointBuilder).build();
}