I am working on an android project and I am using the android:nextFocusButton so the user can press the next button on the soft keyboard to move through EditText without needing to tap each edit text to change the focus.
In the layout I am asking for the users first name and last name, in separate EditText within a Linear Layout which is in horizontal
Then on the next line, in another linear layout, I then ask for the company name. Below is a screenshot to show what I mean.
What I am trying to do, is in the first name edit text, the user can press next, which should then switch focus to the last name edit text, they then press next again and it moves to the company edit text.
Instead what is happening, is the first name has focus, the user presses the next button, and it goes to the company edit text instead of the last name.
Below is a snippet of my XML layout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText android:id="#+id/createAccount_txtFirstName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/first_name"
android:singleLine="true"
android:inputType="textCapSentences"
android:nextFocusForward="#+id/createAccount_txtLastName"/>
<EditText android:id="#+id/createAccount_txtLastName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/last_name"
android:singleLine="true"
android:inputType="textCapSentences"
android:nextFocusForward="#+id/createAccount_txtCompanyName"/>
</LinearLayout>
<EditText android:id="#+id/createAccount_txtCompanyName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/company_name"
android:singleLine="true"
android:inputType="textCapSentences"
android:nextFocusForward="#+id/createAccount_txtEmail"/>
just place this inside your first edittext. Also replace nextFocusForward with nextFocusDown
<EditText android:id="#+id/createAccount_txtFirstName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/first_name"
android:singleLine="true"
android:inputType="textCapSentences"
android:nextFocusDown="#+id/createAccount_txtLastName">
<requestFocus />
</EditText>
It seems to be bugged when running this code on the emulator. However, it works on phones
It might be that your XML is ignored by Android focus algorithm. Documentation says following:
In rare cases, the default algorithm may not match the intended
behavior of the developer. In these situations, you can provide
explicit overrides with the following XML attributes in the layout
file: nextFocusDown, nextFocusLeft, nextFocusRight, and nextFocusUp.
I had similar problem and i solved it by using nextFocusDown.
More on official documentation
None of the solutions worked for me except for listening for editor action listener and programmatically setting the focus to the next EditText.
mFirstName.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
mLastName.requestFocus();
return true;
}
});
Related
<LinearLayout
android:id="#+id/loginInputs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/txtUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:ems="10"
android:hint="User Name"
android:imeOptions="actionNext"
android:text="" />
<EditText
android:id="#+id/txtPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:ems="10"
android:inputType="textPassword"
android:text="" />
</LinearLayout>
I want to get the next focus of txtUsername in code. If I set the android:nextFocusDown attribute, I can get it by using txtUsername.getNextFocusDownId();. ( When softkey enter pressed focus moved to nextFocusDown) I want to know that is there any method to get the default next focus by code without setting focus attributes.
I can get the same functionality by setting focus attributes to all the EditTexts. But I feel there should be a better way of doing it.
P.W. I want to do some logic according to the next focus. I don't want to change the focus.
Based on your P.W. and your question comment, I would recommend using View.OnFocusChangeListener
https://developer.android.com/reference/android/view/View.OnFocusChangeListener.html
Attach a listener to your EditText (or any other view in your hierarchy that could receive focus)
txtUsername.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus)
// do some logic here, removing focus if necessary
}
});
This way, you can field for focus change events and handle any additional logic you want to perform. You can also remove focus from this object and set it elsewhere if you wish.
Android will do its best to direct focus, but it isn't perfect when focus attributes aren't defined. It is our job as developers to fill in the gaps and create the best navigation experience.
See Making Apps More Accessible for more information.
I have the following issue.
I have an edit text where the user puts in the movie name. If the user presses enter I want the focus to go to the next edittext, where the user types the description of the movie.
I do not want the user to be able to put a movie title with two lines, ie I want it all on one line without enter.
However when I use android:singleLine="true", or android:maxLines, the movie title is printed on one line, but if the movie title is long it will not be all displayed at once (have to scroll across).
I want the edittext to grow in height to numerous lines if necessary, but I don't want the user to use enter to make new lines.
How do I do this.
Here is my code:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Movie title"
android:textSize="15sp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:id="#+id/et_AddMovieTitle"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Movie description"
android:textSize="15sp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:id="#+id/et_AddMovieDescription"
/>
Thank you
Maybe and idea would be to create your custom editText, you could override the onKeyDown method and check if the user has pressed enter, if so you could hide the keyboard or something.
class CustomEditTexxt extends EditText
{
...
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode==KeyEvent.KEYCODE_ENTER)
{
// hide the keyboard
return true;
}
// handle all other keys in the default way
return super.onKeyDown(keyCode, event);
}
}
use
android:inputType="textMultiline"
& remove
android:imeOptions="actionDone"
try it.
Add input type in the edit text.
for Single line
android:inputType="text"
for multiline
android:inputType="textMultiline"
I think you will have to override the enter key functionality in order to prevent the user from using "Enter" to add a line in an EditText. You should be able to simply change the focus to the next EditText here and override the new line logic.
I think this post might be of value to you.
Handling "Enter" key in an EditText
I had the same problem as you, and setting only textMultiLine will not work, add android:scrollbars="vertical" and you want to define a android:maxHeight="max_size_in_dp" if it overflow the size user must scroll.
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enviar seu comentário"
android:scrollbars="vertical"
android:maxHeight="100dp"
android:inputType="textCapSentences|textMultiLine"/>
editText.setText("big_text_description_here");
The text will be displayed as you want.
In the past, I've used the android:imeOptions="actionNext" to force the user to the next input field.
You can check the action docs to verify imeOptions is what you need. You may also want to take a look at using the actionDone option for easily submitting the form when the last field is selected.
So a quick/cheap way is to setup as a single-lined EditText with enabled "Next" button:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:imeOptions="actionNext"
android:singleLine="true"
android:id="#+id/et_AddMovieTitle"
/>
And then force the EditText to do soft-wrapping, by applying the following programmatic configuration:
editText.setHorizontallyScrolling(false);
editText.setMaxLines(Integer.MAX_VALUE);
This worked for me testing on android 5.1, but is not really conducive for re-use. So if your needs are more demanding, you can also consider subclassing EditText
I have this stupid and seemingly trivial problem with the properties of an EditText.
The properties I am trying to achieve for the EditText, are the following:
The contents of the view should NOT contain newlines. It can be text, numbers, symbols, but no newlines.
The soft keyboard should NOT display the enter button because of the above. It should instead display something like "Send" or "Done".
The contents of the view should NOT continue horizontally when reaching the edge of the screen. Instead I want to wrap the text, displaying it on multiple lines.
I have tried many different combinations, but I can not achieve this combination.
What I currently have is this, which is inside a RelativeLayout:
<EditText
android:id="#+id/comment_box"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_below="#id/preparation_text"
android:hint="#string/comment_hint"
android:inputType="textCapSentences|textAutoCorrect|text"
android:scrollHorizontally="false"
android:maxLength="400"
android:imeOptions="actionSend"/>
It achieves 2 of 3. No newlines possible, keyboard displays "Send" rather than the enter-key for me, but the text continues on one line.
Changing inputType="text" to "textMultiLine" wraps text correctly on multiple lines, but also overrides the keyboard to always display the enter button.
I have tried different solutions around the Internet, including setting the properties maxLines="4", singleLine="true" and possible others that I have forgotten again.
I can not find a combination that works.
Output:
Exp:
The op is on right track. I did some research found that some options gets ignored which are specified in XML. but if the same options are set in code then it should do the trick. I used the same XML fragment specified in the question.
<EditText
android:id="#+id/edit_text"
android:layout_width="match_parent"
android:layout_height="80dp"
android:hint="hint"
android:inputType="textCapSentences|textAutoCorrect|text"
android:scrollHorizontally="false"
android:maxLength="400"
android:imeOptions="actionSend"
/>
And by adding the following lines in the code, it helped in achieving what you want.
edit_text.setMaxLines(Integer.MAX_VALUE);
edit_text.setHorizontallyScrolling(false);
textShortMessage is the inputType that you are looking for:
<EditText
android:id="#+id/commentEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="100dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:gravity="left"
android:text=""
android:hint="hint"
android:textSize="16dp"
android:textColor="#android:color/black"
android:lineSpacingExtra="0dp"
android:lineSpacingMultiplier="1"
android:background="#android:color/white"
android:selectAllOnFocus="true"
android:inputType="textShortMessage|textMultiLine|textCapSentences"
android:singleLine="false" />
Refere to this to prevent paste function. To prevent pasting a new line.
Please use android:imeOptions="actionSend"
to solve your problem.
I have the same issue a long time ago, you have to programmatically change Edittext property on OnCreate().
So in XML, create your Edittext like this
<EditText
android:id="#+id/comment_box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="80dp"
android:hint="Comment here"
android:maxLength="400"
android:inputType="textMultiLine" />
And on onCreate() (I wrote in kotlin not Java)
comment_box.imeOptions = EditorInfo.IME_ACTION_SEND
comment_box.setRawInputType(InputType.TYPE_CLASS_TEXT)
I'm trying to implement something like a text editor.
I want to have a possibility to edit a long string without word-wrapping.
The edited string should be scrolled horizontally.
New line should be entered only when I press Enter.
So, my editor should be able to edit as long string as user desires (with horizontal scrolling) and be simultaneously multi-lined.
<EditText
android:id="#+id/editor"
android:background="#android:color/transparent"
android:scrollHorizontally="true"
android:lineSpacingExtra = "3sp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:typeface="monospace"
android:textSize="16sp"
android:inputType="textMultiLine"
android:scrollbars="vertical|horizontal"
android:gravity="left|top"
android:text="Code editor"
tools:ignore="HardcodedText"/>
In this case long strings are word-wrapped.
And
android:inputType="text"
gives me a possibility to edit long strings in one line... but how to enter a new line?
Pressing [Enter] doesn't enter a new string, just the next focusable element is selected.
I tried all combinations that could imagine, but nothing worked properly.
Either the text is multi-lined with word-wrapping or it's presented in one wide line.
I wondering if it's possible at all, to get such desired behaviour.
Now I'm thinking about subclassing EditText and manually handling Enter pressing.
But I don't like this approach.
How to get horizontally scrollable EditText with 'multiline' feature?
Any ideas?
You can combine the MUTILINE input type and the ability to scroll horizontally:
editText.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_FLAG_MULTI_LINE);
editText.setHorizontallyScrolling(true);
or in xml :
<EditText
android:inputType="text|textMultiLine"
android:scrollHorizontally="true"
.../>
I have the following AutoCompleteTextView :
<AutoCompleteTextView
android:id="#+id/contact_list_auto_complete_text_view"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:enabled="false"
android:hint=" Enter number"
android:singleLine="true"
android:textSize="20sp" >
</AutoCompleteTextView>
The input to this AutoCompleteTextView is done via a "Dialer" Layout I've created , thus this AutoCompleteTextView is android:enabled="false" and in order to add the user input on my AutoCompleteTextView I use the following code :
String mText =_searchAutoCompleteTextView.getText().toString()+_dialer_digits[position]; //the digit the user clicked upon
_searchAutoCompleteTextView.setText(mText);
_searchAutoCompleteTextView.performCompletion();
if(_searchAutoCompleteTextView.getText().toString().length()>2 && _searchAutoCompleteTextView.getAdapter().getCount()> 0)
{
_searchAutoCompleteTextView.showDropDown();
}
Every thing is working fine except a weird issue :
Since the input is not done by the device's keyboard and only by my dialer with setText() , if the user clicks alot of digits whihc are longer than the AutoCompleteTextView's size last digit is broken and while continuing to input more digits the AutoCompleteTextView does not scroll left. The weird thing about it is that I've tried to enable it and put some long input from the device's keyboard it works perfectly , long text is not broken and scrolling is happening. Since I cannot use the device's keyboard it must be done via my layout and setText()
Many thanks ahead
EDIT:
here is a screen shot :
You can use append instead of setText to move the cursor to the end.
I'm not sure if this will work but have you tried to put your AutoCompleteTextView in a HorizontalScrollView?
<HorizontalScrollView android:layout_width="wrap_content"
android:layout_height="wrap_content">
<AutoCompleteTextView
android:id="#+id/contact_list_auto_complete_text_view"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:enabled="false"
android:hint=" Enter number"
android:singleLine="true"
android:textSize="20sp" >
</AutoCompleteTextView>
</HorizontalScrollView>
Can you provide a screen shot so I can better understand the issue you are having?
You need to set Gravity of AutocompleteTextVIew to right:
<AutoCompleteTextView
android:id="#+id/contact_list_auto_complete_text_view"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:enabled="false"
android:hint=" Enter number"
android:singleLine="true"
android:gravity="right"
android:textSize="20sp" >
</AutoCompleteTextView>