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
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 this EditText
<EditText
android:layout_width="match_parent"
android:layout_height="72dp"
android:hint="#string/write_message"
android:textColorHint="#color/primary_color"
android:ems="10"
android:imeOptions="actionDone"
android:inputType="textImeMultiLine"
android:id="#+id/message_input"
android:layout_gravity="center_horizontal"
android:backgroundTint="#color/primary_color"/>
When the box is filled up with user inputted text, it scrolls to the right to make room for more text, I do not like this behavior, and would prefer it if the EditText box expanded upwards when it needs more room? Is there a way to do this? Thanks.
Yes, this actually involves two things:
Making the EditText accept multi-line input.
Having its height grow as more text lines are added.
Therefore, to achieve this, you need to set up:
android:inputType="textMultiLine"
android:layout_height="wrap_content"
(Be mindful of the difference between textMultiLine and textImeMultiLine).
The full XML snippet would be:
<EditText
android:layout_width="match_parent"
android:inputType="textMultiLine"
android:layout_height="wrap_content"
android:hint="#string/write_message"
android:textColorHint="#color/primary_color"
android:ems="10"
android:imeOptions="actionDone"
android:id="#+id/message_input"
android:layout_gravity="center_horizontal"
android:backgroundTint="#color/primary_color"/>
Use both flags: textMultiLine will wrap your input, and textImeMultiLine will provide a break-line key in your keyboard.
<EditText
...
android:layout_height="wrap_content"
android:inputType="textImeMultiLine|textMultiLine"
... />
In my case I have multiline text, but it showed one line and a keyboard:
Though I have already set android:inputType="textCapSentences|textAutoCorrect|textMultiLine", it didn't help. Then I understood that when the keyboard appears in DialogFragment, it collapses the EditText. See DialogFragment and force to show keyboard to show keyboard when DialogFragment shows.
Then I added a short delay (100-300 ms) before showing the keyboard. Now I have:
In AndroidManifest I set android:windowSoftInputMode="adjustResize" for current activity.
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;
}
});
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>