How to intentionally go to new line in an EditText? - android

I am creating an app which provides chat functionality in it. There is an EditText in the screen, on which I have set OnKeyListener. Whenever user presses the "Enter" key after typing the message, the message is posted on the screen. It works fine. Here is my EditText:
<EditText android:id="#+id/chatMessageField"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginRight="5dip" android:layout_marginLeft="5dip"
android:maxLines="2" android:minLines="1" android:scrollbars="vertical"
android:hint="#string/chatMessageHintText" android:textColorHint="#3b64a8">
</EditText>
The problem is when user wants to go to new line before the EditText wraps the text and goes to new line. Now if user wants to go to new line and presses "Enter" key, the message is sent.
In some chat messangers , I have seen that pressing "Shift+Enter"(or any other key-combination) simultaneously takes the user to new line. How can we detect "Shift+Enter"(or any other key-combination) keys pressed simultaneously in Android? Is there any way to achieve this functionality in Android?

Just try for this... It may help.
Set the input of EditText as
youredittext.setInputType(EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE);
Then add:
android:singleLine="false"
or
youredittext.setSingleLine(false);
Also go Click Here

In your OnKeyListener method, use check for Enter and Shift Enter and provide separate functionality of Send and New Line respectively.

While searching you solution I got this link. Try it it may solve your problem.
And if some other best way you got please post, this is very useful and good question..,.
Thanks..,.
EDIT......................
Also see this link for better using the custom shortcuts..,.

Here you can set 'OnKeyListener' method. For each key you can provide the functionality as follows..
editText.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch(keyCode) {
case KeyEvent.KEYCODE_0:
//handle code for pressing 0
break;
case KeyEvent.KEYCODE_1
//handles code for pressing 1
default:
break;
}
}
});
Just try to search the code for shift and enter
It may Works.
Have fun.

Related

OnKeyListener and onEditorAction not working when using swype

I'm trying to add submit functionality in an EditText when pressing the enter/return key on the soft keyboard in my app.
The following code works using the standard keyboards, but not with the swype keyboard (I have also done the equivalent onKeyListener).
et.setOnEditorActionListener(new EditText.OnEditorActionListener(){
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Log.d(TAG, "enter was pressed");
addComment();
return true;
}
});
The method doesn't get called at all. Is this just a limitation of Swype? or am I doing something wrong?
if this is a limitation of Swype, how can I get around it, I have seen other apps do this, and it works using my swype keyboard.
My EditText layout is defined as:
<EditText
android:id="#+id/comment_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:hint="#string/comment_hint"
android:visibility="gone"
/>
I think this was a design decision by Swype (and maybe also google). Basically that if the edit text is multiline, you can't override the enter key, as you should be able to press enter and expect a new line. So it's really a UX thing, which I guess makes sense
Basically I had to add an additional button to submit the comment.

Enable EditText(or button) on Long pressed

in my application EditText is there which is disable and I want to implement long press option on this edittext(while disable mode) which enable it and allowing to enter the character from softkey.
Example:-
Suppose initially I am allowing user to enter some number into the EditText. After some operation, I need to disable this EditText. Again if user want to change the number which previously he enter in the editText then first he need to long press on this editText.
After doing long press on this editText, editText get enable and again user will able to change or retype the number. I need to do some operation before changing the number in the editText and during operation user not have any option to change the number in the editText.
Code:-
<EditText
android:id="#+id/eTextBillNoFrmReturn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="2dp"
android:layout_weight="7"
android:clickable="true"
android:background="#drawable/custom_edit_text"
android:inputType="number" />
#Override
public boolean onLongClick(View v) {
// do some operation
return false;
}
but this code work only EditText is enable. Long press not work when EditText is disable.
Try this one.
#Override
public boolean onLongClick(View v) {
// do some operation
btnname.setenable(true);
btnname.setfocusable(true);
return false;
}
In case anyone stumbles across this old post like I did, I thought I would post an answer since I found some settings that worked well for my situation.
The OP was asking for a way to make the EditText function as if it is disabled yet still allow setting a long-click listener that can get called.
Set the edit text to be NOT focusable and set the input type to none. The input type to none will make it so that a cursor doesn't try to show up when clicking on it, so it looks a bit nicer than the cursor that flashes for a second with just the focusable set to false.
To do this in the XML:
<EditText
android:id="#+id/txtYourEditTextID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:inputType="none" />
To do this in the code:
EditText txtYourEditText = (EditText)findViewById(R.id.txtYourEditTextID);
txtYourEditText.setFocusable(false);
txtYourEditText.setInputType(InputType.TYPE_NULL);
To do what OP is wanting, you will implement a long-click listener:
txtYourEditText.setOnLongClickListener(new View.OnLongClickListener {
#Override
public boolean onLongClick(View v) {
((EditText)v).setInputType(InputType.TYPE_CLASS_NUMBER);
v.setFocusable(true);
return true; //or return false if you do not want to consume the event
}
});
Then on whatever the action is that you want to disable it again, call
txtYourEditText.setFocusable(false);
txtYourEditText.setInputType(InputType.TYPE_NULL);
Hopefully this will help someone who is trying to do something similar.
Have you tried to replace
return false;
by
return true;
on your onLongClick(View v) method ?
Use the toggle button which will suite perfectly your case
Toggle Button

onKey listner works properly on emulator, but not working on android device

I'm working on a android project in which i need to change a table layout according to the changes on a edit text view. The code work properly on emulator. But it doesn't works on actual device. Also in this case I'm using fragments. My onKey listener is as below.
EditProduct.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
Editable prx=EditProduct.getText();
String new_prx=prx.toString();
//mini_productList
int count=0;
if (new_prx.equals("")) {
loadtableProducts(productList);
}else {
for(int i=0;i<productList.size();i++){
if (productList.get(i).getDescription().toString().substring(0, (new_prx.length())).equalsIgnoreCase(new_prx)) {
mini_productList.add(productList.get(i));
count++;
}
}
loadtableProducts(mini_productList);
//Toast.makeText(getActivity(), "No of products "+count, Toast.LENGTH_SHORT).show();
}
return false;
}
});
Also the edit text view in my layout is as below
<EditText
android:id="#+id/edit_product"
android:layout_width="#dimen/txtWidth"
android:layout_height="#dimen/txtHeight"
android:background="#color/transparent"
android:hint="Enter the product name"
android:singleLine="true"
android:textColor="#color/black"
android:textSize="#dimen/boldtext" />
Also i added a Toast to make sure its work on the device and it shown on the entering of a key.
So can someone help me in this matter.
Thank you!
The documentation for EditText.setOnKeyListener states:
"Register a callback to be invoked when a hardware key is pressed in this view. Key presses in software input methods will generally not trigger the methods of this listener."
The emulator is using a hardware keyboard whereas your device is using a software keyboard. I suggest you use an "addTextChangedListener" as an alternative.
However I note that you say that your Toast does indicate the entering of a key. So are you actually entering your OnKeyListener when using the actual device? If so, I will delete my answer.

Android [ENTER]-key action of a multiline edittext in a dialog

I've create a dialog with a multiline edittext-field. The problem is that the [ENTER]-key of the soft keyboard is closing the keyboard instead of creating a new line. With imeOptions, it's possible to configure a lot, but not a newline-command... How can I accomplish this?
Building for a Galaxy Tab 2 with Android 4.0.3.
I found out that setting the raw inputtype of the EditText to multiline is working where the "normal" input type isn't.
final EditText remark = new EditText(MyClass.this);
remark.setRawInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
This did it for me.
You might be able to accomplish this by creating a new TextWatcher. Register this textwatcher to your EditText and implement a breakline when receiveing the return key.
EDIT:
To handle an individual key press, implement onKeyDown() or onKeyUp() as appropriate. Usually, you should use onKeyUp() if you want to be sure that you receive only one event. If the user presses and holds the button, then onKeyDown() is called multiple times.
For example:
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
/* This is a sample for handling the Enter button */
return true
default:
return super.onKeyUp(keyCode, event);
}
}
Source:
Android :
http://developer.android.com/training/keyboard-input/commands.html
A list of the KeyEvents:
http://developer.android.com/reference/android/view/KeyEvent.html

how to catch 'next' button on soft keyboard of Android

I have 6 Edittexts grouped in 6 differents layouts, all in the same view.
My problem is that my app is forced to landscape mode, and by pressing the 'next' button i want to automatically start to edit another editText rather than the one android set me by default. Example: i'm writing on EditText1, press next, and focus is taken by the EditText that is UNDER the 1st one. I want to edit the one that is RIGHT to it.
Sorry for my bad english :(
The device is a galaxy tab, with Google API8/Android 2.2
Thanks
Haven't tested it, but this should work
some_edittext.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_NEXT) {
some_button.performClick();
return true;
}
return false;
}
});
I might be wrong but I don't think there is any implicit way of doing it. You would need to write your own EditorAction.
You need to monitor everything that the user enters and whenever he presses 'NEXT', you would have to manually shift the focus to the next EditText, using EditorInfo.IME_ACTION_NEXT.

Categories

Resources