Android: AutoCompleteTextView with default suggestions - android

How do I show some default suggestions for AutoCompleteTextView before the user type anything? I cannot find a way to do this even with creating a custom class that extends AutoCompleteTextView.
I want to show suggestions for common input values to save the user from typing.
Any suggestions?

You should subclass AutoCompleteTextView and override enoughToFilter() to return true all the time. After that you can call performFiltering("",0) (it's a protected function, so you can export this call via a public function in your class).
Something like that:
public class ContactsAutoCompleteTextView extends AutoCompleteTextView {
public ContactsAutoCompleteTextView(Context context) {
super(context);
}
public ContactsAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ContactsAutoCompleteTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public boolean enoughToFilter() {
return true;
}
public void temp() {
performFiltering("",0);
}
}

Itay Kahana's answer is indeed correct. The only thing I would add is that instead of creating a temp() function, to override the onFocusChanged function. Personally I used the following:
#Override
protected void onFocusChanged (boolean focused, int direction, Rect previouslyFocusedRect) {
if(focused)
performFiltering("", 0);
super.onFocusChanged(focused, direction, previouslyFocusedRect);
}

If you dont need it to be dynamic I would go by having a string array in the resources, and then just load the array when the AutoCompleteTextView is about to be viewed. Like:
public class CountriesActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.countries);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, COUNTRIES);
AutoCompleteTextView textView = (AutoCompleteTextView)
findViewById(R.id.countries_list);
textView.setAdapter(adapter);
}
private static final String[] COUNTRIES = new String[] {
"Belgium", "France", "Italy", "Germany", "Spain"
};
}
Which can be found on http://developer.android.com/reference/android/widget/AutoCompleteTextView.html
Another way which I have done a couple of times which allows it to learn from the user is o use a database connection with IE a simple cursor. When you create the db you could insert some default values.
HereĀ“s an example with using simple cursor adapter: http://androidcommunity.com/forums/f4/how-to-use-autocompletetextview-with-simplecursoradapter-15875/
Edit 1:
One idea to show the list before the user starts type is to have a simple listview below the EditText. Not sure if you could call the autocompletetextview to show the suggestions, should be possible somehow. Perhaps you need to create your own autocompletetextiew class.

Related

Update the index of suggestion list in autocompletetextview on textchange

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

Overwrite pate event text in MultiAutoCompleteTextView Android

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"

Android Spinner OnItemSelected ONLY on User Interaction

I know that this question has been answered multiple times, but I have not found any of them satisfactory and certainly not elegant.
The issue is that OnItemSelected gets fired not only when the user selects an item, but also when the selection is programmatically set.
Some answers propose setting a flag for times when the programmer is setting a value for the spinner. However sometimes other android code will set the value outside of your code.
A common place for android to set the value is upon instantiation of the spinner. Some answers address that particular issue. However, there are numerous places where Android will break down and reinstantiate a spinner. It is not elegant to track down all of them.
So the question is: how does one attach their OnItemSelectedListener code ONLY to selections made by user interaction with the UI?
Spinner.setOnItemClickListener seems like it would answer this issue perfectly, but Google has it disabled
AdapterView.setOnClickListener also seems like a natural candidate but it also generates a runtime error
Next place is to extend Spinner and start overriding methods but that of course means messing with the APIs (which I'd rather not do, or I'd at least like to have other users working on it with me)
So I am posting an answer, but any criticisms, improvements, or other more elegant answers are welcome.
The key is overriding onClick to set a flag which ties the onItemSelectedListener to user interaction and fires the onItemClickedListener.
If you don't feel comfortable using the API setOnItemClickedListener (for future compatibility perhaps), you can of course substitute your own method. I just felt like onItemClickedListener should have been implemented to this effect the whole time, so that is my subtle protest.
Also, if anyone can think of a reason that the spinnerTouched flag gets short circuited (stays true for longer than it should), please let us know so that it can be addressed. It seems to work pretty well so far though.
public class OnItemClickSpinner extends Spinner implements AdapterView.OnItemSelectedListener {
boolean spinnerTouched = false;
OnItemClickListener onItemClickListener = null;
OnItemSelectedListener onItemSelectedListener = null;
public OnItemClickSpinner(Context context) {
super(context);
super.setOnItemSelectedListener(this);
}
public OnItemClickSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
super.setOnItemSelectedListener(this);
}
public OnItemClickSpinner(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
super.setOnItemSelectedListener(this);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
spinnerTouched = true;
return super.onTouchEvent(event);
}
#Override
public void setOnItemClickListener(OnItemClickListener listener) {
onItemClickListener = listener;
}
#Override
public void setOnItemSelectedListener(OnItemSelectedListener onItemSelectedListener) {
this.onItemSelectedListener = onItemSelectedListener;
super.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(spinnerTouched && this.onItemClickListener!=null) this.onItemClickListener.onItemClick(parent,view,position,id);
if(this.onItemSelectedListener!=null) this.onItemSelectedListener.onItemSelected(parent,view,position,id);
spinnerTouched=false;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
if(this.onItemSelectedListener!=null) this.onItemSelectedListener.onNothingSelected(parent);
spinnerTouched=false;
}
}
Had a similar issue with some UI component in iOS. I decided to use a similar hack.
So somewhere in the object that owns the spinner--maybe an activity--declare a member variable: private var isSelectionFromTouch: Boolean = false
The rest of the relevant code:
init {
spinner.onItemSelectedListener = this
spinner.setOnTouchListener { _, _ ->
this.isSelectionFromTouch = true
false
}
}
// On Item Selected
override
fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
if (!isSelectionFromTouch) { return }
Log.d(TAG, "item selected. do something.")
isSelectionFromTouch = false
}
override
fun onNothingSelected(parent: AdapterView<*>?) {
isSelectionFromTouch = false
}

How to apply FontSize dynamically and programmatically without using XML in Android? [duplicate]

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

Disable, reenable and the soft keyboard does not appear on touch

Take a look at this example:
public class TestEditSoftKbdActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.editText1).setFocusable(false);
findViewById(R.id.editText1).setClickable(false);
findViewById(R.id.editText1).setEnabled(false);
findViewById(R.id.editText1).setFocusable(true);
findViewById(R.id.editText1).setClickable(true);
findViewById(R.id.editText1).setEnabled(true);
findViewById(R.id.editText1).invalidate();
findViewById(R.id.editText1).requestLayout();
}
}
After this sequence of calls the edit text view would no longer pop up its soft input method upon being touched :(
Could someone explain what is going wrong here?
If you want to close soft keyboard for your text view follow this link. Here is a solution for you. But you need to define your own TextView to do that. He suggests using;
public class NoImeEditText extends EditText {
public EditTextEx(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public boolean onCheckIsTextEditor() {
return false;
}
}
Hope it works.

Categories

Resources