So I need an input box for text in my android app.
I have this in my xml:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/inputCodePA"
android:textSize="18sp"
android:inputType="text"
android:hint="#string/inputHint"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/arrowRightPA"
android:layout_toEndOf="#+id/arrowRightPA">
<requestFocus/>
</EditText>
and this:
public void setOnClickListners(){
final EditText inputBox = (EditText)findViewById(R.id.inputCodePA);
inputBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
inputBox.clearFocus();
inputBox.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(inputBox, InputMethodManager.SHOW_FORCED);
}
});
inputBox.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
inputBox.setText("");
inputBox.clearFocus();
inputBox.requestFocus();
return false;
}
});
}
For my code in my activity's class (setOnClickListner() is called in my onCreate()).
But, whenever after I type something into the EditText box and press Enter, I can not open the keyboard to enter text again.
I know I'm making a really basic error, but I can't seem to figure out what.
After all, this is the first time an EditText failed me.
HA!
I found out the problem!
It was because I had a ScrollView below the EditText in the code (which made the ScrollView become 'in front'), which did not allow the user to click on the view at all!
Hope this helps other people who makes these stupid mistakes!
Related
Can someone please provide a solution to get a working listener for the soft-keyboard DONE button, and/or explain what I'm doing wrong in my current approach?
My Xml & Java Setup
In Xml, there's a simple EditText is set with android:imeOptions="actionDone"
In Java, it just has a basic TextView.OnEditorActionListener declared
So setOnEditorActionListener() is not getting called when there is a click from device soft-keyboard submit (aka DONE) button - the green arrow button below ; only the EditText field is cleared
But that listener DOES get called when clicking the computer Enter key (attached via ADB).
I would figure it should work for both those buttons .. is this not right?
XML Layout File:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.demo.MainActivity">
<EditText
android:id="#+id/comment_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:maxLines="1"
android:imeOptions="actionDone"
/>
</LinearLayout>
Java Activity File:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((EditText) findViewById(R.id.comment_text)).setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
int i = 5; // added this to set a break point
return false;
}
});
}
}
Update for Solution
Thanks to #Apoorv Mehrotra's answer, it turns out that my EditText was missing this one attribute in order for the soft keyboard event to be recognized. Adding this to the above Xml solves the problem.
android:inputType="text"
I had the same problem, for me, adding android:singleLine="true" in EditText did the trick
try this code this is a code with 2 fields in which i have user time feature. in first one keyboard shows next button and in second one soft keypad shows done option and on done option i hide the soft keypad.
below is the xml file code.
<EditText
android:id="#+id/txt1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:gravity="top|left"
android:imeOptions="actionNext"
android:inputType="text|textNoSuggestions"
android:text=""
android:visibility="visible" />
<EditText
android:id="#+id/txt2"
android:layout_width="match_parent"
android:layout_height="100dp"
android:gravity="top|left"
android:imeOptions="actionDone"
android:inputType="text|textNoSuggestions"
android:text=""
android:visibility="visible"
/>
now below is the java code: for hiding soft keypad on next and same can be performed for done option as well.
this code add in onCreate() of your class.
EditText first_txt = (EditText) findViewById(R.id.txt1);
EditText second_txt = (EditText) findViewById(R.id.txt2);
first_txt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_NEXT) {
InputMethodManager imm = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
return true; // Focus will do whatever you put in the logic.
}
return false; // Focus will change according to the actionId
}
});
second_txt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
InputMethodManager imm = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
return true; // Focus will do whatever you put in the logic.
}
return false; // Focus will change according to the actionId
}
});
This code works for , hope it helps you out!
This is an old issue that seems not to be solved yet. There's a workaround for these cases. Simply take the EditText (or whatever View you might be needing this and assign it a onEditorActionListener, like this:
final EditText editor = (EditText) findViewById(R.id.myEditText);
editor.setOnEditorActionListener(EnterOnText);
Now define the EnterOnText implementation like this:
TextView.OnEditorActionListener EnterOnText = new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_GO) {
final String msg = view.getText().toString();
if (!msg.isEmpty()) {
// Do whatever you need here
...
}
}
return true;
}
};
Now, simply change the layout's imeOptions attribute to:
android:imeOptions="actionGo"
That will, oppositely, probably make your emulator's enter key not work, but will (should) do on all physical devices.
((EditText) findViewById(R.id.comment_text)).setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_DONE){
//do something
}
return false;
}
});
Try this,
((EditText) findViewById(R.id.comment_text)).setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (event != null && (actionId == EditorInfo.IME_ACTION_GO
|| event.getKeyCode() == event.KEYCODE_ENTER))
{
//do whatever you want
}
}
});
<EditText
android:id="#+id/comment_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:maxLines="1"
android:imeOptions="actionGo"
/>
It happens when the user dismisses the softkeyboard and then tries to click/gain focus on the EditText again, nothing happens only the cursor is shown - I want to show the keyboard again.
I've tried:
Using an onclick event
Using a focuschanged event
Changing properties of the EditText (focusable etc...)
Note: I am currently using Paranoid Android. The EditText is Multiline.
I found the solution, I just had to remove the following attribute from my EditText:
android:textIsSelectable="true"
Please start by omitting any requestFocus defined for your EditText.
there's a known bug that prevents keyboard from showning if the latter is set.
If that doesn't work for you, create a focus listener and in it programmatically open the virt keyboard:
editTxt.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) {
// show keyboard
InputMethodManager imm = (InputMethodManager)this.getSystemService(Service.INPUT_METHOD_SERVICE);
imm.showSoftInput(editTxt, 0);
}
}
});
Here is a solution:
final InputMethodManager imm=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
EditText e= (EditText) findViewById(R.id.editText1);
e.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
imm.showSoftInput(e, InputMethodManager.SHOW_IMPLICIT);
}
});
e.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
// TODO Auto-generated method stub
if(actionId==EditorInfo.IME_ACTION_GO){
imm.hideSoftInputFromWindow(e.getWindowToken(), 0);
//Do you work here
}
return false;
}
});
and the edittext will be:
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:imeOptions="actionGo"/>
I'd like for my enter key to complete an action without hiding the keyboard afterwards. Currently every time I enter something and press the enter button the EditText input is cleared to accept the next input, but as the keyboard disappears I have to click the EditText again so that the keyboard would appear again...
Right now this is on my layout:
<EditText
android:id="#+id/etCommand"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/commandHint"
android:inputType="textNoSuggestions"
android:imeOptions="actionSend" >
</EditText>
And the code:
etCommand = (EditText) findViewById(R.id.etCommand);
etCommand.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEND) {
sendCommand();
}
return false;
}
});
EDIT
As swayam suggested I had to return true inside onEditorAction so that the OS would think the action was handled and leave it alone.
Have a go at this code snippet :
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
Add the KeyListener for EditText for Enter key press and after the task completed requestfocus on edittext again would be solution
I have an EditText field in my app:
<EditText
android:id="#+id/task_buy_edit_mass_editText1"
android:minWidth="100dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:imeOptions="actionDone" >
</EditText>
The problem is that on my Kindle Fire, the keyboard that appears when the user wants to edit the EditText has no Done button, just a button that hides the soft keyboard
That wouldn't be a problem if I could intercept the 'hide keyboard' button that I highlighted on the above screenshot. However, it doesn't seem to trigger the OnEditorActionListener I've attached to the EditText, so I'm a bit stuck what to do aside from having my own 'Done' button in my actual layout to take focus away from the EditText, hide the keyboard and make any changes that result from the number being changed
Is there a way to intercept the 'hide keyboard' button, or is there some alternative solution?
Incidentally, I have a regular EditText view elsewhere in my app that has no imeOptions nor specifies inputType and that does have a Return key
Here's the rest of the code for reference:
editText.setOnEditorActionListener(new OnEditorActionListener(){
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_DONE || actionId==EditorInfo.IME_ACTION_NEXT || actionId==EditorInfo.IME_NULL || event.getKeyCode()==KeyEvent.FLAG_EDITOR_ACTION || event.getKeyCode()==KeyEvent.KEYCODE_ENTER){
closeKeyboard(v, done_button);
return true;
}
return false;
}});
// set done button for keyboards without correct IME options
final EditText finalEditText = editText;
editText.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
done_button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
closeKeyboard(finalEditText, v);
}});
done_button.setVisibility(View.VISIBLE);
return false;
}
});
.
private void closeKeyboard(TextView v, View buttonv){
demand.setAmount(Double.parseDouble((v.getText().toString()))); // update the game
// close keyboard
InputMethodManager inputManager = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
v.clearFocus();
buttonv.setVisibility(View.GONE);
}
android:inputType="textMultiLine" and android:singleLine="true" as #James Wald indicated works.
EDIT:
Doing the above will make it the EditText multi line. Instead android:imeOptions="actionGo" worked for my purposes on all devices (AFAIK).
Hi guys i was assigned a task in my project,there i need to add a Done button on the top of Android keyboard.In my screen i have an EditText,whenever i click on the EditText it should open a keyboard along with the "Done" Button on the top of an Android keyboard.So that i can attach a Listener to that button to perform my task.Any Suggestions.
Thanks&Regards,
E.N.Krishna.
If you need the keyboard to display the Done button, you need to define this in your EditText
<EditText android:text="EditText" android:layout_width="fill_parent"
android:id="#+id/editText1" android:layout_height="wrap_content"
android:imeOptions="actionDone"/>
You can then catch when the user presses the Done button by using OnEditorActionListener
class DoneOnEditorActionListener implements OnEditorActionListener {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
return true;
}
return false;
}
}
Your answer could be found if you read this.
Namely the concept of IME action.