1

I have implemented this spinner in my app. I'm displaying a dialog and I have my spinner in that dialog. I want my spinner to open as the dialog comes up. I have tried performClick() event right after setAdapter() method but it doesn't seem to work.

My code looks like this:

final MaterialBetterSpinner selectBrandForSODRating = dialog.findViewById(R.id.selectBrandForSODspinner);

final ArrayList<Products> brandList = new ArrayList<Products>();
Cursor crsCheckSODData = database.rawQuery(myQuery, null);
if(crsCheckSODData.getCount() > 0){
  while (crsCheckSODData.moveToNext()) {
     //data...
     brandList.add(data);
  }
}

crsCheckSODData.close();

final ArrayAdapter<Products> SODBrandAdapter = new ArrayAdapter<Products>(myView.this, android.R.layout.simple_dropdown_item_1line, brandList);
selectBrandForSODRating.setAdapter(SODBrandAdapter);
selectBrandForSODRating.performClick(); //this right here isnt working...
Thomas Mary
  • 1,535
  • 1
  • 13
  • 24
3iL
  • 2,146
  • 2
  • 23
  • 47

1 Answers1

0

If you are using https://github.com/Lesilva/BetterSpinner, the code is overriding onTouchEvent here, but not overriding onClick. That's why performClick will not work.

You could simulate touch event instead to the view. Here's how:

int[] coords = new int[2];
selectBrandForSODRating.getLocationOnScreen(coords);
float x = (float) coords[0];
float y = (float) coords[1];

// List of meta states found here: developer.android.com/reference/android/view/KeyEvent.html#getMetaState()
int metaState = 0;
// Obtain MotionEvent object
long downTime1 = SystemClock.uptimeMillis();
long eventTime1 = SystemClock.uptimeMillis() + 100;
MotionEvent motionEventDown = MotionEvent.obtain(
    downTime1, 
    eventTime1, 
    MotionEvent.ACTION_DOWN, 
    x, 
    y, 
    metaState
);

// Dispatch touch event to view
selectBrandForSODRating.dispatchTouchEvent(motionEventDown);

long downTime2 = SystemClock.uptimeMillis();
long eventTime2 = SystemClock.uptimeMillis() + 100;
MotionEvent motionEventUp = MotionEvent.obtain(
    downTime2, 
    eventTime2, 
    MotionEvent.ACTION_UP, 
    x, 
    y, 
    metaState
);

// Dispatch touch event to view
selectBrandForSODRating.dispatchTouchEvent(motionEventUp);

You could also directly call the methods from here

selectBrandForSODRating.requestFocus();
selectBrandForSODRating.showDropDown();

But I don't recommend it, because there is another boolean state, isPopUp, that has private access and you can't access.

HendraWD
  • 2,984
  • 2
  • 31
  • 45