|
25 | 25 | package com.amrdeveloper.codeview; |
26 | 26 |
|
27 | 27 | import android.content.Context; |
| 28 | +import android.content.res.Resources; |
28 | 29 | import android.graphics.Canvas; |
29 | 30 | import android.graphics.Color; |
30 | 31 | import android.graphics.Paint; |
|
53 | 54 | import java.util.HashMap; |
54 | 55 | import java.util.HashSet; |
55 | 56 | import java.util.List; |
56 | | -import java.util.Locale; |
57 | 57 | import java.util.Map; |
58 | 58 | import java.util.Set; |
59 | 59 | import java.util.SortedMap; |
@@ -92,6 +92,9 @@ public class CodeView extends AppCompatMultiAutoCompleteTextView implements Find |
92 | 92 | private CharacterStyle currentMatchedToken; |
93 | 93 | private final List<Token> matchedTokens = new ArrayList<>(); |
94 | 94 |
|
| 95 | + private int maxNumberOfSuggestions = Integer.MAX_VALUE; |
| 96 | + private int autoCompleteItemHeightInDp = (int) (50 * Resources.getSystem().getDisplayMetrics().density); |
| 97 | + |
95 | 98 | private final Handler mUpdateHandler = new Handler(); |
96 | 99 | private MultiAutoCompleteTextView.Tokenizer mAutoCompleteTokenizer; |
97 | 100 |
|
@@ -571,22 +574,51 @@ public void setMatchingHighlightColor(int color) { |
571 | 574 | matchingColor = color; |
572 | 575 | } |
573 | 576 |
|
| 577 | + /** |
| 578 | + * Modify the maximum number of suggestions to show, default is Integer.MAX_VALUE |
| 579 | + * @param maxSuggestions the maximum number of suggestions |
| 580 | + * @since 1.2.3 |
| 581 | + */ |
| 582 | + public void setMaxSuggestionsSize(int maxSuggestions) { |
| 583 | + maxNumberOfSuggestions = maxSuggestions; |
| 584 | + } |
| 585 | + |
| 586 | + /** |
| 587 | + * Modify the auto complete item height |
| 588 | + * @param height auto complete item height in dp |
| 589 | + * @since 1.2.3 |
| 590 | + */ |
| 591 | + public void setAutoCompleteItemHeightInDp(int height) { |
| 592 | + autoCompleteItemHeightInDp = (int) (height * Resources.getSystem().getDisplayMetrics().density); |
| 593 | + } |
| 594 | + |
574 | 595 | @Override |
575 | 596 | public void showDropDown() { |
576 | | - int[] screenPoint = new int[2]; |
| 597 | + final int[] screenPoint = new int[2]; |
577 | 598 | getLocationOnScreen(screenPoint); |
578 | 599 |
|
579 | 600 | final Rect displayFrame = new Rect(); |
580 | 601 | getWindowVisibleDisplayFrame(displayFrame); |
581 | 602 |
|
582 | | - int position = getSelectionStart(); |
583 | | - Layout layout = getLayout(); |
584 | | - int line = layout.getLineForOffset(position); |
585 | | - int lineButton = layout.getLineBottom(line); |
586 | | - int dropDownVerticalOffset = lineButton + 140; |
587 | | - int dropDownHorizontalOffset = (int) layout.getPrimaryHorizontal(position); |
588 | | - setDropDownVerticalOffset(dropDownVerticalOffset); |
589 | | - setDropDownHorizontalOffset(dropDownHorizontalOffset); |
| 603 | + final Layout layout = getLayout(); |
| 604 | + final int position = getSelectionStart(); |
| 605 | + final int line = layout.getLineForOffset(position); |
| 606 | + final int lineButton = layout.getLineBottom(line); |
| 607 | + |
| 608 | + int numberOfMatchedItems = getAdapter().getCount(); |
| 609 | + if (numberOfMatchedItems > maxNumberOfSuggestions) { |
| 610 | + numberOfMatchedItems = maxNumberOfSuggestions; |
| 611 | + } |
| 612 | + |
| 613 | + int dropDownHeight = getDropDownHeight(); |
| 614 | + int modifiedDropDownHeight = numberOfMatchedItems * autoCompleteItemHeightInDp; |
| 615 | + if (dropDownHeight != modifiedDropDownHeight) { |
| 616 | + dropDownHeight = modifiedDropDownHeight; |
| 617 | + } |
| 618 | + |
| 619 | + setDropDownHeight(dropDownHeight); |
| 620 | + setDropDownVerticalOffset(lineButton + dropDownHeight); |
| 621 | + setDropDownHorizontalOffset((int) layout.getPrimaryHorizontal(position)); |
590 | 622 |
|
591 | 623 | super.showDropDown(); |
592 | 624 | } |
|
0 commit comments