Java Code Examples for androidx.appcompat.widget.ListPopupWindow#setAdapter()

The following examples show how to use androidx.appcompat.widget.ListPopupWindow#setAdapter() . 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: MenuMainDemoFragment.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
private ListPopupWindow initializeListPopupMenu(View v) {
  ListPopupWindow listPopupWindow =
      new ListPopupWindow(getContext(), null, R.attr.listPopupWindowStyle);
  ArrayAdapter<CharSequence> adapter =
      new ArrayAdapter<>(
          getContext(),
          popupItemLayout,
          getResources().getStringArray(R.array.cat_list_popup_window_content));
  listPopupWindow.setAdapter(adapter);
  listPopupWindow.setAnchorView(v);
  listPopupWindow.setOnItemClickListener(
      (parent, view, position, id) -> {
        Snackbar.make(
                getActivity().findViewById(android.R.id.content),
                adapter.getItem(position).toString(),
                Snackbar.LENGTH_LONG)
            .show();
        listPopupWindow.dismiss();
      });
  return listPopupWindow;
}
 
Example 2
Source File: Popup.java    From candybar with Apache License 2.0 5 votes vote down vote up
private Popup(Builder builder) {
    mPopupWindow = new ListPopupWindow(builder.mContext);
    mAdapter = new PopupAdapter(builder.mContext, builder.mItems);

    int width = getMeasuredWidth(builder.mContext);
    mPopupWindow.setWidth(width);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Drawable drawable = mPopupWindow.getBackground();
        if (drawable != null) {
            drawable.setColorFilter(ColorHelper.getAttributeColor(
                    builder.mContext, R.attr.card_background), PorterDuff.Mode.SRC_IN);
        }
    } else {
        mPopupWindow.setBackgroundDrawable(new ColorDrawable(
                ColorHelper.getAttributeColor(builder.mContext, R.attr.card_background)));
    }

    mPopupWindow.setAnchorView(builder.mTo);
    mPopupWindow.setAdapter(mAdapter);
    mPopupWindow.setOnItemClickListener((adapterView, view, i, l) -> {
        if (builder.mCallback != null) {
            builder.mCallback.onClick(this, i);
            return;
        }

        mPopupWindow.dismiss();
    });
}
 
Example 3
Source File: TextInputSpinner.java    From HaoReader with GNU General Public License v3.0 5 votes vote down vote up
@SuppressLint("RestrictedApi")
private void init(Context context, AttributeSet attrs, int defStyleAttr) {
    setInputType(InputType.TYPE_NULL);
    setCursorVisible(false);
    Drawable arrow = getResources().getDrawable(R.drawable.ic_arrow_drop_down_black_24dp);
    arrow.setBounds(0, 0, arrow.getIntrinsicWidth(), arrow.getIntrinsicHeight());
    setCompoundDrawablesRelative(null, null, arrow, null);
    AppCompat.setTint(arrow, getResources().getColor(R.color.colorMenuText));

    TypedArray a = context.obtainStyledAttributes(attrs,
            R.styleable.TextInputSpinner, defStyleAttr, 0);
    mEntities = a.getTextArray(R.styleable.TextInputSpinner_android_entries);
    a.recycle();

    mPopup = new ListPopupWindow(context, attrs, defStyleAttr, R.style.AppTheme_PopupMenu_Overflow);
    mPopup.setAdapter(new ArrayAdapter<>(context, R.layout.support_simple_spinner_dropdown_item, mEntities));
    mPopup.setOnItemClickListener((parent, view, position, id) -> {
        setSelection(position);
        mPopup.dismiss();
    });
    mPopup.setAnchorView(this);
    mPopup.setVerticalOffset(-ScreenUtils.dpToPx(5));
    mPopup.setHorizontalOffset(ScreenUtils.dpToPx(4));
    mPopup.setWidth(getResources().getDisplayMetrics().widthPixels - ScreenUtils.dpToPx(16));
    mPopup.setModal(true);

    setSelection(0);
}
 
Example 4
Source File: MessageFormatSettingsActivity.java    From revolution-irc with GNU General Public License v3.0 5 votes vote down vote up
private void selectPreset(EditText editText, CharSequence[] presets) {
    ListPopupWindow menu = new ListPopupWindow(editText.getContext());
    menu.setAnchorView(editText);
    menu.setAdapter(new ArrayAdapter<>(this,
            R.layout.activity_message_format_settings_preset, presets));
    menu.setOnItemClickListener((AdapterView<?> parent, View view, int position, long id) -> {
        editText.setText(presets[position]);
        menu.dismiss();
    });
    menu.show();
}
 
Example 5
Source File: MaterialAutoCompleteTextView.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
public MaterialAutoCompleteTextView(
    @NonNull Context context, @Nullable AttributeSet attributeSet, int defStyleAttr) {
  super(wrap(context, attributeSet, defStyleAttr, 0), attributeSet, defStyleAttr);
  // Ensure we are using the correctly themed context rather than the context that was passed in.
  context = getContext();

  TypedArray attributes =
      ThemeEnforcement.obtainStyledAttributes(
          context,
          attributeSet,
          R.styleable.MaterialAutoCompleteTextView,
          defStyleAttr,
          R.style.Widget_AppCompat_AutoCompleteTextView);

  // Due to a framework bug, setting android:inputType="none" on xml has no effect. Therefore,
  // we check it here in case the autoCompleteTextView should be non-editable.
  if (attributes.hasValue(R.styleable.MaterialAutoCompleteTextView_android_inputType)) {
    int inputType =
        attributes.getInt(
            R.styleable.MaterialAutoCompleteTextView_android_inputType, InputType.TYPE_NULL);
    if (inputType == InputType.TYPE_NULL) {
      setKeyListener(null);
    }
  }

  accessibilityManager =
      (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);

  modalListPopup = new ListPopupWindow(context);
  modalListPopup.setModal(true);
  modalListPopup.setAnchorView(this);
  modalListPopup.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED);
  modalListPopup.setAdapter(getAdapter());
  modalListPopup.setOnItemClickListener(
      new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View selectedView, int position, long id) {
          Object selectedItem =
              position < 0 ? modalListPopup.getSelectedItem() : getAdapter().getItem(position);

          updateText(selectedItem);

          OnItemClickListener userOnitemClickListener = getOnItemClickListener();
          if (userOnitemClickListener != null) {
            if (selectedView == null || position < 0) {
              selectedView = modalListPopup.getSelectedView();
              position = modalListPopup.getSelectedItemPosition();
              id = modalListPopup.getSelectedItemId();
            }
            userOnitemClickListener.onItemClick(
                modalListPopup.getListView(), selectedView, position, id);
          }

          modalListPopup.dismiss();
        }
      });

  attributes.recycle();
}