The Android autocomplete only starts after two letters. How can I make it so the list appears when the field is just selected?
To get the autocomplete to show on focus add focus listener and show the drop down when the field gets focus, like this:
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
if(hasFocus){
editText.showDropDown();
}
}
});
Or just call editText.showDropDown() if you don't need the focus part.
Have a look to setThreshold method:
public void setThreshold (int
threshold)
Since: API Level 1
Specifies the minimum number of
characters the user has to type in the
edit box before the drop down list is
shown.
When threshold is less than or equals 0, a threshold of 1 is applied.
Extend the AutoCompleteTextView, overriding the enoughToFilter() methods and the threshold methods so that it doesn't replace the 0 threshold with a 1 threshold:
public class MyAutoCompleteTextView extends AutoCompleteTextView {
private int myThreshold;
public MyAutoCompleteTextView(Context context) {
super(context);
}
public MyAutoCompleteTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public void setThreshold(int threshold) {
if (threshold < 0) {
threshold = 0;
}
myThreshold = threshold;
}
#Override
public boolean enoughToFilter() {
return getText().length() >= myThreshold;
}
#Override
public int getThreshold() {
return myThreshold;
}
}
For people who want to change threshold using SearchView you have to use:
SearchView.SearchAutoComplete complete = (SearchView.SearchAutoComplete)search.findViewById(R.id.search_src_text);
complete.setThreshold(0);
Pad your adapter with one/two white character on left depending on the threshold setting.
Alternate method of changing the setting in your XML:
As mentioned by others you need to set your 'Auto Completion Threshold' to 1
Other than what #systempuntoout mentioned.
You can also do that in your XML file as shown
<AutoCompleteTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/edittext_id"
android:inputType="textAutoComplete"
android:completionThreshold="1"
/>
Note the line : android:completionThreshold="1"
Related
Background
I have an App with multiple custom TextViews in it.
These four Custom TextViews are all set up like this:
public class TextView_Light extends android.support.v7.widget.AppCompatTextView {
public TextView_Light(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public TextView_Light(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public TextView_Light(Context context) {
super(context);
init();
}
public void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Light.ttf");
setTypeface(tf ,1);
}}
I do have TextView_Bold, TextView_Light, TextView_Regular and TextView_Thin. Each of them uses another font which is saved in assets/fonts
I use the TextViews like that:
<de.mayr.wap.app.helper.TextView_Light
android:id="#+id/textView_Light7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="#string/overviewViaBarcode"
android:textSize="16sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
When the App is started and a view shows up for the first time everything looks great, like that:
The Problem
In this view the user klicks on one of the tablerows, an Dialog appears where the user types in some data. On save the onResume method of the view is called, which calls an Webservice etc. like that:
#Override
public void onResume() {
super.onResume();
callSerNrWS();
}
public void callSerNrWS() {
if (CheckNetwork.isNetworkAvailable(getContext())) {
final AsyncResponseFortschritt<ResponseObject<PruefauftraegeVO>> response = new AsyncResponseFortschritt<ResponseObject<PruefauftraegeVO>>() {
#Override
public void processFinishSerNr(ResponseObject<PruefauftraegeVO> output) {
anw = createPruefAnwArraylist();
seriennummerVO = findDataToSerNr();
adapt = new PruefAnwAdapter(getContext(), anw, seriennummerVO);
pruefListView.setAdapter(adapt);
setVisibilityLoadAnimation(View.GONE);
if (output.getException() != null && !output.getException().isEmpty())
AlertCreator.makeFailAlert(R.string.titelKeinFortschritt, R.string.textKeinFortschritt,getContext());
}
#Override
public void processStartSerNr() {
setVisibilityLoadAnimation(View.VISIBLE);
}
};
AsyncCallSerNrWS ws = new AsyncCallSerNrWS(response, pruefauftraegeVO);
ws.execute();
}
}
And after that, theres in 90% of cases one or more TextView which changed its looking from eg. TextView_Light to TextView_Thin. Like in the following picture: The appearence of the 'Anzugstrom [A]' in the third row now looks like a TextView_Thin and the '9/48' in the fifth row looks like a TextView_Light
What I know
This doesnt happen when the view is shown for the first time
There isn't any system behind it. It's totally random.
It happens in activitys and fragments
It happens in every view i have whatever the TextView contains 'hard-coded' text or text that comes from an Webservice
Its not just in lists, also in 'normal' views
I am using autocomplete textview with arraylist value [aaa0,aaa1,...aaa100,aab0,aab1,...,aab100], I enter value 'aa' and scroll down till next 10 value ie.'aa10'and then i enter value 'aab' the suggestion list starts from 10th value i.e 'aab10'.
I have to scroll up to see the first value i.e 'aab0'.I want first value of suggestion list to appear when i change the search text.suggestion list screenshot
You need to extend the AutoCompleteTextView class
and override the onFilterComplete method
where you have to dismiss and show the drop down again to refresh the list view.
Also I would suggest instead of extending AutoCompleteTextView
extend the android.support.v7.widget.AppCompatAutoCompleteTextView
for obviously better support
here's the code
package your.package.name;
import android.content.Context;
import android.util.AttributeSet;
import android.support.v7.widget.*;
public class CustomAutoCompleteTextView extends AppCompatAutoCompleteTextView {
public CustomAutoCompleteTextView(Context context) {
super(context);
}
public CustomAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public void onFilterComplete(int count) {
super.onFilterComplete(count);
//show and dismiss the drop down to refresh the list in it
this.dismissDropDown();
this.showDropDown();
}
}
EDIT
Also you can use the AutoCompleteTextView.setListSelection() method to scroll to the top.
Instead of dismissing and showing the DropDown on every filter
which is more efficient.
#Override
public void onFilterComplete(int count) {
super.onFilterComplete(count);
//set list selection to 0
if(getListSelection() != 0)
this.setListSelection(0);
}
I want to use gif images on the custom keyboard.can anyone please help me how to implement custom Gif keyboard?.
You need to Make a Custom EditText Class like this
public class GifEditText extends EditText {
public GifEditText(Context context) {
super(context);
}
public GifEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public GifEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public InputConnection onCreateInputConnection(EditorInfo editorInfo) {
final InputConnection ic = super.onCreateInputConnection(editorInfo);
EditorInfoCompat.setContentMimeTypes(editorInfo,
new String[]{"image/gif"});
final InputConnectionCompat.OnCommitContentListener callback =
new InputConnectionCompat.OnCommitContentListener() {
#Override
public boolean onCommitContent(InputContentInfoCompat inputContentInfo,
int flags, Bundle opts) {
// read and display inputContentInfo asynchronously
if (BuildCompat.isAtLeastNMR1() && (flags &
InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION) != 0) {
try {
inputContentInfo.requestPermission();
} catch (Exception e) {
return false; // return false if failed
}
}
// read and display inputContentInfo asynchronously.
// call inputContentInfo.releasePermission() as needed.
return true; // return true if succeeded
}
};
return InputConnectionCompat.createWrapper(ic, editorInfo, callback);
}
}
And use like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.test.GifEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Test gif" />
</LinearLayout>
For more details you can refer the official documentation :
https://developer.android.com/guide/topics/text/image-keyboard.html
You can create a custom keyboard by following this tutorial:
https://code.tutsplus.com/tutorials/create-a-custom-keyboard-on-android--cms-22615
Then in implementing your own images or gif, create a custom popup window with your images in it by adding a class that extends popup window. See this answer:
implement popup window on keyboard in android to add images in custom keyboard
Add a key with unique keyCode in your keyboard that will trigger and show the popup window.
I got the sample for this by implementing this we can simply create custom gif keyboard
https://github.com/googlesamples/android-CommitContentSampleIME
When a user want to paste text in MultiAutoCompleteTextView I want to overwrite it.
Means whatever he copies it must paste in my MultiAutoCompleteTextView as "Java is rocket".
I may achieve it from previous question here but I don't know how to hook the class MonitoringEditText to my MultiAutoCompleteTextView.
Can I achieve it or it is impossible.
You can do this using the same concept provided in the code from the link you provided. Extend the MultiAutoCompleteTextView and override the onTextContextMenuItem method.
Something along the lines of:
public class MonitoringMultiAutoCompleteTextView extends MultiAutoCompleteTextView {
public MonitoringMultiAutoCompleteTextView(Context context) {
super(context);
}
public MonitoringMultiAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MonitoringMultiAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
//based on http://stackoverflow.com/a/14981376/1566836
#Override
public boolean onTextContextMenuItem(int id) {
// Do your thing:
boolean consumed = super.onTextContextMenuItem(id);
// React:
if (android.R.id.paste == id) {
setText("Java is rocket");
}
return consumed;
}
}
Then change your MultiAutoCompleteTextView in your layout file to whatever.your.full.package.is.MonitoringMultiAutoCompleteTextView.
After that, any attempt to paste into your MultiAutoCompleteTextView will result in the text being changed to "Java is rocket"
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to change the Font Size in a whole Application programmatically, Android?
This is my Code : I have created Spinner with a list of FontSize options. If I click FontSize "26" then it should be able to change in that specific FontSize. Below I have an EditBox. So if I clicked Fontsize as 40 and in a Italic style than usual Bold. So I should be able to type inside EditBox with this selected "Font" : FontSize "40" and Italic style.
How could I do this programmatically in Android?
font=new Spinner(con);
option= new String[] {"Select Font Size","8","10","12","14","16","18","20",
"22","24","26","28","30","32","34","36","38","40","50"};
ArrayAdapter<String> adapter= new ArrayAdapter<String>(con,android.R.layout.simple_spinner_dropdown_item,option);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
font.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long id) {
option[1]="8";
selectedItem= option[position];
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
to change
textSize use editText.setTextSize(20)
font and style use editText.setTypeface(yourTypeFace, Typeface.BOLD)
UPDATE
public class MyEditText extends EditText{
public MyEditText(Context context) {
super(context);
init();
}
public MyEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
void init() {
this.setTextSize(20);
this.setTypeface(yourTypeFace, Typeface.BOLD);
}
// method to change font settings
void setFont(TypeFace tf){
this.setTypeFace(tf);
}
//add whatever method you want
}
and then instead of using EditText you use this class, and don't forget in your XML to use
<yourpackage.MyEditText
android:layout_height=".."
android:layout_width=".."
... />
The probable solution would be you create a base class which extends TextView, and use this text view class as edit text. Hope you are asking for size in first screen. In any case, u set the text size in the base class. This will solve your problem.
like u create this class in package com.example and class name is BaseTextView, then in xml file instead of you will write
Hope this helps.
First of all get the id of the EditBox.
Then selected item's position. according to that you can make a formula like this:
public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long id) {
int p=(8+(pos*2));
editText.setTextSize(p);
}