Skip to content

Commit 1ebab72

Browse files
committed
Improve Auto Complete implementation and add more options like max suggestion size
1 parent b9228b6 commit 1ebab72

1 file changed

Lines changed: 42 additions & 10 deletions

File tree

codeview/src/main/java/com/amrdeveloper/codeview/CodeView.java

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
package com.amrdeveloper.codeview;
2626

2727
import android.content.Context;
28+
import android.content.res.Resources;
2829
import android.graphics.Canvas;
2930
import android.graphics.Color;
3031
import android.graphics.Paint;
@@ -53,7 +54,6 @@
5354
import java.util.HashMap;
5455
import java.util.HashSet;
5556
import java.util.List;
56-
import java.util.Locale;
5757
import java.util.Map;
5858
import java.util.Set;
5959
import java.util.SortedMap;
@@ -92,6 +92,9 @@ public class CodeView extends AppCompatMultiAutoCompleteTextView implements Find
9292
private CharacterStyle currentMatchedToken;
9393
private final List<Token> matchedTokens = new ArrayList<>();
9494

95+
private int maxNumberOfSuggestions = Integer.MAX_VALUE;
96+
private int autoCompleteItemHeightInDp = (int) (50 * Resources.getSystem().getDisplayMetrics().density);
97+
9598
private final Handler mUpdateHandler = new Handler();
9699
private MultiAutoCompleteTextView.Tokenizer mAutoCompleteTokenizer;
97100

@@ -571,22 +574,51 @@ public void setMatchingHighlightColor(int color) {
571574
matchingColor = color;
572575
}
573576

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+
574595
@Override
575596
public void showDropDown() {
576-
int[] screenPoint = new int[2];
597+
final int[] screenPoint = new int[2];
577598
getLocationOnScreen(screenPoint);
578599

579600
final Rect displayFrame = new Rect();
580601
getWindowVisibleDisplayFrame(displayFrame);
581602

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));
590622

591623
super.showDropDown();
592624
}

0 commit comments

Comments
 (0)