Guest User

Untitled

a guest
Aug 15th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. How to create a drop down spinner in Android < 3.0 that looks like a dropdown spinner for android >3.0?
  2. public class SpinnerDropBox extends TextView{
  3.  
  4. int mHeight;
  5. ListView mListView;
  6. List<String> mListChoice;
  7. Context mContext;
  8. int mWidth = 0;
  9. PopupWindow popSpin;
  10. TextView pThis;
  11. int mLastPos;
  12.  
  13. public SpinnerDropBox(Context context, List<String> listChoice) {
  14. super(context);
  15. mContext = context;
  16. mListChoice = listChoice;
  17. mWidth = getMaxWidthOfList();
  18. this.setWidth(mWidth);
  19. this.setText(mListChoice.get(0));
  20. pThis = this;
  21. this.setOnClickListener(new OnClickListener() {
  22. public void onClick(View v) {
  23. showDropBox();
  24. }
  25. });
  26. }
  27.  
  28. private int getMaxWidthOfList(){
  29. int width = 0;
  30. Paint paint = new Paint();
  31. for(String s : mListChoice){
  32. if((int)this.getPaint().measureText(s) > width){
  33. width = (int)paint.measureText(s);
  34. }
  35. }
  36.  
  37. width *=3;
  38. int screenWidth = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth();
  39. if(width > screenWidth){
  40. width = screenWidth;
  41. }
  42.  
  43. return width;
  44. }
  45.  
  46. public void setChoice(int pos){
  47. this.setText(mListChoice.get(pos));
  48. }
  49.  
  50. void showDropBox(){
  51.  
  52. int xPos, yPos, arrowPos;
  53. int[] location = new int[2];
  54. this.getLocationOnScreen(location);
  55. Rect rect = new Rect(location[0], location[1], location[0] + this.getWidth(), location[1] + this.getHeight());
  56. this.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
  57. int rootHeight = this.getMeasuredHeight();
  58. int rootWidth = 0;
  59. if (rootWidth == 0) {
  60. rootWidth = this.getMeasuredWidth();
  61. }
  62.  
  63. int screenWidth = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth();
  64. int screenHeight = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getHeight();
  65. if ((rect.left + rootWidth) > screenWidth) {
  66. xPos = rect.left - (rootWidth-this.getWidth());
  67. xPos = (xPos < 0) ? 0 : xPos;
  68. arrowPos = rect.centerX()-xPos;
  69. } else {
  70. if (this.getWidth() > rootWidth) {
  71. xPos = rect.centerX() - (rootWidth/2);
  72. } else {
  73. xPos = rect.left;
  74. }
  75. arrowPos = rect.centerX()-xPos;
  76. }
  77.  
  78. popSpin = new PopupWindow(mContext);
  79. popSpin.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
  80. popSpin.setWidth(mWidth);
  81.  
  82. popSpin.setTouchable(true);
  83. popSpin.setFocusable(true);
  84.  
  85. LinearLayout laySpin = new LinearLayout(mContext);
  86. laySpin.setLayoutParams(new LayoutParams(mWidth, LayoutParams.WRAP_CONTENT));
  87.  
  88. ListView lv = new ListView(mContext);
  89. lv.setLayoutParams(new LayoutParams(mWidth, LayoutParams.WRAP_CONTENT));
  90.  
  91. lv.setItemsCanFocus(true);
  92. lv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
  93. lv.setFocusable(false);
  94. lv.setOnItemClickListener(listListener);
  95.  
  96. ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext.getApplicationContext(), R.layout.simple, mListChoice);
  97. lv.setAdapter(adapter);
  98. laySpin.addView(lv);
  99.  
  100. popSpin.setContentView(laySpin);
  101.  
  102. if((screenHeight-rect.bottom) > (rootHeight*mListChoice.size())){
  103. yPos = rect.bottom;
  104. }
  105. else{
  106. yPos = rect.top - (rootHeight*mListChoice.size()+mListChoice.size());
  107. }
  108.  
  109. popSpin.showAtLocation(laySpin, Gravity.NO_GRAVITY, xPos, yPos);
  110.  
  111. }
  112.  
  113. private OnItemClickListener listListener = new OnItemClickListener() {
  114. @Override
  115. public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
  116. mLastPos = arg2;
  117. pThis.setText(((TextView)arg1).getText().toString());
  118. popSpin.dismiss();
  119. }
  120. };
  121.  
  122. public int getLastPosition(){
  123. return mLastPos;
  124. }
Add Comment
Please, Sign In to add comment