How to word wrap the hint of a single line AutoCompleteTextView? - android

I have an AutoCompleteTextView where the user is to write in a single-line search, i.e. no line breaks are allowed. Using android:singleLine="true" allows this to work properly.
However, I also wish to set a hint to display when no other text has been entered, and this text does require more than one line. My problem is that with Android 4+, the hint is not wrapped to several lines. With Android 2.3, however, the content does get wrapped and multiple lines are displayed. Users are still not able enter more than one line, so this is the way I want it.
Using for example android:maxLines="2" does not help since that allows the user to insert a line break when searching. I've also tried android:scrollHorizontally="false" and android:ellipsize="none" and android:layout_weight=1 without success.
Any ideas on how I can get the hint to wrap multiple lines and still only accept a single line from users with Android 4 as well? Below is the code I'm using at the moment.
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:completionThreshold="3"
android:hint="This is the hint to be displayed. It's rather long and doesn't fit in one line"
android:singleLine="true" />

Try changing singleLine to:
android:lines="1"
And now to disable "Enter":
EDIT_TEXT.setOnKeyListener(new OnKeyListener()
{
#Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_ENTER)
{
return true;
}
return false;
}
});

Try adding an OnFocusChangeListener to change the value of singleLine or maxLines:
autoCompleteTextView.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange (View v, boolean hasFocus) {
// This might do it on its own
((AutoCompleteTextView) v).setSingleLine(hasFocus);
// If setSingleLine() doesn't work try this
AutoCompleteTextView auto = (AutoCompleteTextView) v;
if(hasFocus)
auto.setMaxLines(1);
else
auto.setMaxLines(2); // or more if necessary
// Only keep the one that works, you obviously don't need both!
}
});

Related

Trying to make the soft keyboard enter button say search or display search magnifying glass and handle its click for an EditText

I have tried the highly popular answer suggested here
Although this hasn't worked for me like it seems to have for the other users. Perhaps it is outdated or I have something conflicting in my code.
Here is the xml for my EditText:
<EditText
android:id="#+id/editText_letter_entry"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="#drawable/edit_text_rounded"
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"
android:drawableEnd="#drawable/ic_search_black_24dp"
android:hint="#string/enter_letters"
android:importantForAutofill="no"
android:inputType="text|textNoSuggestions|textVisiblePassword"
android:maxLength="15"
android:maxLines="1"
android:imeOptions="actionSearch"
android:paddingLeft="#dimen/et_padding_left_right"
android:paddingRight="#dimen/et_padding_left_right"
app:layout_constraintBottom_toTopOf="#+id/bottomBannerAdView"
app:layout_constraintEnd_toStartOf="#+id/guideline2Container"
app:layout_constraintStart_toStartOf="#+id/guideline1Container"
app:layout_constraintTop_toTopOf="parent" />
As you can see I have the android:imeOptions="search" & android:inputType="text" along with textNoSuggestions & textVisiblePassword as the inputType. I have tried this with only text as the inputType like the other answer suggested but this didn't make any difference and I don't want any suggestions on the keyboard.
As for this listener I managed to get that working although in a bit of a hacky way because the action id is coming back as IME_ACTION_UNSPECIFIED, So I just handled this instead. This can be seen here:
et_letter_entry.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_UNSPECIFIED ) {
preformSearch();
return true;
}
return false;
}
});
I've currently got an icon for a new line rather than search or a search icon.
Why doesn't the xml seem to be affected by android:imeOptions="search" in my case?
Why is the enter button displaying a new line icon?
Why is the action id returning as unspecified?
Thanks
Remove
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"
from your EditText and it will fix your problem.
The conflict was due to the android:digits is set. In this situation android:inputType is set to numeric by default disregarding what you entered there(android:inputType="text|textNoSuggestions|textVisiblePassword") in your xml, thus making your android:imeOptions="actionSearch" irrelevant.
Hope it helps.

Execute function at soft key enter

I'm trying to add a way to execute a function on a soft key enter (or whatever the bottom right hand key would be, I assume its usually a enter/done key) once a edit text field has been filled in with numbers. I also have a calculate button that I would like to keep as a back up in an attempt to 'idiot proof' the app a little bit. below is a snippet of my code thus far; this part is working:
onCalculate refers to a button I have.
I have error checking for null values and whatever else.
EditText something
public void onCalculate (View v){
do stuff....
}
I want to add something down here to 'do stuff' in the event the user presses the done/enter/bottom right hand soft key instead of pressing the button. Below is a snippet from my layout XML:
<EditText
android:id="#+id/editText1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:layout_marginTop="20dp"
android:ems="10"
android:inputType="numberDecimal"
android:textSize="15sp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_margin/>
I know I probably need to create some kind of key listener for the enter key, but I'm not sure how to go about it.
You can achieve this by associating an OnEditorActionListener to the EditText. For example:
editText1.setImeOptions(EditorInfo.IME_ACTION_DONE);
editText1.setOnEditorActionListener(new new OnEditorActionListener()
{
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if ((actionId & EditorInfo.IME_MASK_ACTION) == EditorInfo.IME_ACTION_DONE)
{
onCalculate(editText1);
return true;
}
}
});

How to add "Next" on android keyboard

I've seen in some applications that in the keyboard appears a Button called next that puts the focus on the next Edit Text. I want to add this in my application, Do you know how I can do it? Or it's only on the application's keyboard? Thanks a lot.
Sorry, I haven't more information about this.
In the layout of edittext add android:imeOptions attribute. Here you can add different action buttons like Go, Search , Send , Next, Done etc. But to use this the EditText has to be a singleLine Else there will be an enter button there. So also use the android:singleLine="true".
So here is your solution
<EditText
android:id=//put ur id
android:layout_width=//according to need
android:layout_height=//accordintoneed
android:imeOptions="actionNext"
android:singleLine="true"/>
EDIT
for addition i should add some more for future use or for the help of others..
You can handle the imeoption actions from your code. for that you have to set setOnEditorActionListener to the Edittext and when a action is pressed you can get it on public boolean onEditorAction(TextView v, int actionId, KeyEvent event) method. and if you not handle the code by yourself the imeoptions action will do the default action (usually going to next control).
Here is an example code for handling the action
EditText e = (EditText)findViewById(R.id.editText1);
e.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_NEXT ) {
// handle next button
return true;
}
return false;
}
});
You just have to use the imeOptions tag in your layout. See imeOptions in docu.
<EditText
android:id="#+id/someField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionNext"/>
You have to add these two line given below in your EditText
android:imeOptions="actionNext"
android:singleLine="true"
i tried all those answers posted here but none actually work. gladly I found myself a solution for this.
<EditText
android:id="#+id/etRegisterFirstName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Firstname"
android:imeOptions="actionNext"
android:inputType="textCapWords"
android:maxLines="1" />
<EditText
android:id="#+id/etRegisterLastName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Lastname"
android:imeOptions="actionNext"
android:inputType="textCapWords"
android:maxLines="1" />
Please do notice that I ADD android:inputType. Thats it .
Cheers / Happy coding

How to get EditText, IME Action, textMultiLine, to work for JellyBean

I've run into quite the conundrum and am failing to find a solution. Apparently JellyBean changes how IME actions are handled. I've found many websites offering a solution which indeed works but only for single lined EditTexts. Example: Stackoverflow: onEditorAction
My EditText widgets worked perfectly until JellyBean. It would properly word-wrap until the user hit the "Done" (return) key. Then it'd catch the event with the OnEditorActionListener and process accordingly. I've tried multiple variations of changing the settings with the following XML attributes to no avail:
singleLined
scrollHorizontally
inputType
imeOptions
lines
I could only get word-wrapping with no onEditorAction event fired or no word-wrapping with the onEditorAction event firing. How can I get word-wrapping and handle the softkeyboard enter key at the same time for JellyBean?
Update 1: Including requested code. Note this is how it stands now which works for all platforms except JellyBean. As I said earlier, tried multiple different XML settings to no avail.
Update 2: Managed to get a hold of an Asus Transformer running JellyBean 4.1.1. Works fine. So perhaps this is a device specific bug? My other JellyBean device is a Nexus 7 running 4.1.2. Can anyone verify this with other devices?
Code:
private class OnMyEditorActionListener implements OnEditorActionListener {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_GO) {
doSomething();
return true;
}
return false;
}
}
<EditText
android:id="#+id/editbox_box_et"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#null"
android:gravity="top|center_horizontal"
android:imeOptions="actionGo"
android:inputType="textMultiLine|textNoSuggestions"
android:padding="#dimen/spacing_half"
android:textSize="24sp" >
</EditText>
Provide an ID by yourself to your submit/go button
In activity:
private class OnMyEditorActionListener implements OnEditorActionListener {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == R.id.your_new_ID || actionId == EditorInfo.IME_Null) {
doSomething();
return true;
}
return false;
}
}
In xml:
<EditText
android:id="#+id/editbox_box_et"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#null"
android:gravity="top|center_horizontal"
android:inputType="textMultiLine|textNoSuggestions"
android:padding="#dimen/spacing_half"
android:textSize="24sp"
android:imeActionId="#+id/your_new_ID"
android:imeActionLabel="Go"> </EditText>
try the following:
android:inputType="text"
android:imeOptions="actionNone"
and also check out with different options for word-wrapping in imeOptions xml file
After a lot of testing, I've determined this is a bug specific to the Nexus 7 and there is nothing code wise I can do to work around it. Interestingly, if I download a different keyboard from Google Play, then the code actually works!
Try adding attribute android:imeActionId with integer value (2 for actionGo). http://developer.android.com/reference/android/widget/TextView.html#attr_android:imeOptions
I had issue getting the event triggered for Jelly Bean 4.1.2. Adding input type helped me.
android:imeOptions="actionGo"
android:inputType="text"

AutocompleteTextView: on "NEXT" highlight next TextView, on "DONE", make keyboard disappear

I have two AutocompleTextViews and I want to switch to the next if the user presses "NEXT", and make the virtual keyboard disappear when he hits "DONE" at the second AutocompleTextView. So far, the buttons "NEXT"/"DONE" do nothing at all.... Unfortunately I found no resources addressing this problem.
Any suggestions?
thx
EDIT: Just want to add that this was asked when Android was on version 2.3 or something like that.
I ran into this problem and fixed it by setting the imeOptions on the AutocompleteTextView to actionNext.
Example:
<AutoCompleteTextView
android:id="#+id/dialog_product_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:completionThreshold="1"
android:imeOptions="actionNext"
/>
I found this solution for the "NEXT" problem: in your View source code write something like this
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
if (firstAutoComplete.hasFocus()) {
// sends focus to another field (user pressed "Next")
otherText.requestFocus();
return true;
}
else if (secondAutoComplete.hasFocus()) {
// sends focus to another field (user pressed "Next")
anotherText.requestFocus();
return true;
}
}
return false;
}
It seems to be an old Android http://code.google.com/p/android/issues/detail?id=4208.
Here I found my solution: http://groups.google.com/group/android-developers/browse_thread/thread/e53e40bfe255ecaf.
Just add these two lines into AutoCompleteTextView xml Code:
android:completionThreshold="1"
android:imeOptions="actionNext"
Don't forget to add inputType="text" otherwise you'll have enter button instead of next/done one:
<AutoCompleteTextView
android:id="#+id/suppliers"
android:layout_width="#dimen/summary_input_width"
android:layout_height="wrap_content"
android:hint="#string/current_supplier"
android:imeOptions="actionNext"
android:inputType="text"
android:lines="1" />

Categories

Resources