I have a problem on my Layout. In the layout you can find a lot of EditText.
When i click to edittext, than a popup dialog shows. But when I left the input, the cursor stay in.
All edittext have this settings:
editText.setFocusable(false);
editText.setClickable(true);
editText.setLongClickable(false);
editText.setTextIsSelectable(false);
<EditText
android:id="#+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableRight="#drawable/ic_close_black_24dp"
android:ems="10"
android:inputType="textPersonName" />
How can I hide the cursor when i left the input field?
UPDATE:
Problem there is when i click to "RIGHT DRAWABLE" icon.
public static boolean isDrawableClick(MotionEvent event, EditText editText, DrawablePositions drawablePosition) {
if (event.getAction() == MotionEvent.ACTION_UP) {
if (editText.getCompoundDrawables()[drawablePosition.position] != null && event.getX() >= (editText.getRight() - editText.getLeft() - editText.getCompoundDrawables()[drawablePosition.position].getBounds().width())) {
return true;
}
}
return false;
}
Thanks
Use this
setCursorVisible(false);
try this in your xml
<EditText
android:id="#+id/et_edit_account_birth_date"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:focusable="false"
android:hint="#string/string_birth_date"
/>
and in your activity file
et_edit_account_birth_date.setCursorVisible(false);
Related
here' the code
I have two edit Text in a row in my lay out,and when I touch next on keyboard it does not change the focus.
the setOnEditorActionListener does not work till i add extra editText
<EditText
android:id="#+id/firstText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:imeOptions="actionNext"
android:inputType="numberDecimal"
/>
<EditText
android:id="#+id/secondText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:imeOptions="actionDone"
android:inputType="numberDecimal"
/>
code:
firstText.setOnNextActionListener(new CallBack<KeyEvent>() {
#Override
public void call(KeyEvent data) {
secondText.requestEditTextFocus();
}
});
Give one value into edittext in xml file ..
android:singleLine="true"
You can try these
android:nextFocusDown="#+id/secondText"
android:nextFocusLeft="#+id/secondText"
android:nextFocusRight="#+id/secondText"
android:nextFocusUp="#+id/secondText"
Show below three ways, you can got you want Using any one of them
inputType ="actionNext" attribute
or
android:imeOptions="actionNext"
or
android:singleLine="true" and
android:nextFocusDown="#+id/secondText"
on your .xml you can do this.
If you want to do it programmatically in your activity or fragment then try below:-
firstText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event){
if (actionId == EditorInfo.IME_ACTION_NEXT) {
secondText.requestEditTextFocus();
return true; // Focus will do whatever you put in the logic.
}
return false; // Focus will change according to the actionId
}
});
I have tested with set Programmtically with changes secondText.requestEditTextFocus(); to secondText.requestFocus(); and its working fine.
Please show below Screenshotes
Im using
android:digits="abcdefghijklmnopqrstuvwxyz. "
and having one more EditText in the same screen when i press enter key in keypad the focus doesn't gets changed.
suppose if i remove the
android:digits
the enter button works fine and moves to the next edit text.
Add android:imeOptions="actionNext" in your EditText in xml
<EditText
android:id="#+id/et_count"
android:layout_width="100dp"
android:singleLine="true"
android:imeOptions="actionNext"
android:layout_height="wrap_content" />
You can make use of the imeOptions attribute available for EditText in xml.
Try the following. It worked in my case.
In XML:
<EditText
android:id="#+id/edit_text1"
android:layout_width="match_parent"
android:layout_height="45dp"
android:digits="abcdefghijklmnopqrstuvwxyz. "
android:imeOptions="actionNext"/>
<EditText
android:id="#+id/edit_text2"
android:layout_width="match_parent"
android:layout_height="45dp"/>
Then, in JAVA:
EditText edit_text1 = (EditText) findViewById (R.id.edit_text1);
EditText edit_text2 = (EditText) findViewById (R.id.edit_text2);
edit_text1.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_NEXT) {
edit_text2.requestFocus();
handled = true;
}
return handled;
}
});
you must be delete 'android:digits' to active the 'android:imeOptions="actionNext"' code.
I have an EditText in my app and I want it to have two lines, to show ime button instead of enter key and to move too long text to next line (like in sms apps). For now i have something like this:
<AutoCompleteTextView
android:id="#+id/name_field"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:background="#null"
android:freezesText="true"
android:hint="#string/some_hint"
android:imeOptions="actionNext"
android:maxLength="100"
android:nextFocusDown="#null"
android:lines="2"
android:ellipsize="end"
android:inputType="textImeMultiLine"
android:selectAllOnFocus="true"
android:textColor="#android:color/black"
android:textSize="16sp" />
It has two first properties I mentioned, but i can't recall any option that allows me to reach third.
For example: if one line in my EditText has 10 chars, i want to display text "abc abcd abc abcdefghijk" like that:
abc abcd
abc abc...
EDIT:
It seems problem is in android:inputType="textImeMultiLine". When i changed it to android:inputType="textMultiLine" all works fine, but... I have enter button instead of IME button, which i want to avoid.
Try this..Hope it will work!
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:background="#null"
android:freezesText="true"
android:imeOptions="actionNext"
android:maxLength="100"
android:nextFocusDown="#null"
android:lines="2"
android:ellipsize="end"
android:selectAllOnFocus="true"
android:textColor="#android:color/black"
android:textSize="16sp" />
add this line
android:inputType="textMultiLine"
add this in your xml android:maxLines="2" instead of android:lines="2"
None of these Methods worked for me.
After Googling for about an hour, I found this piece of code:
EditText editText = findViewById(R.id.editNote);
editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
editText.setImeActionLabel("DONE",EditorInfo.IME_ACTION_DONE); //Set Return Carriage as "DONE"
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if (event == null) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
// Capture soft enters in a singleLine EditText that is the last EditText
// This one is useful for the new list case, when there are no existing ListItems
editText.clearFocus();
InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
else if (actionId == EditorInfo.IME_ACTION_NEXT) {
// Capture soft enters in other singleLine EditTexts
} else if (actionId == EditorInfo.IME_ACTION_GO) {
} else {
// Let the system handle all other null KeyEvents
return false;
}
}
else if (actionId == EditorInfo.IME_NULL) {
// Capture most soft enters in multi-line EditTexts and all hard enters;
// They supply a zero actionId and a valid keyEvent rather than
// a non-zero actionId and a null event like the previous cases.
if (event.getAction() == KeyEvent.ACTION_DOWN) {
// We capture the event when the key is first pressed.
} else {
// We consume the event when the key is released.
return true;
}
}
else {
// We let the system handle it when the listener is triggered by something that
// wasn't an enter.
return false;
}
return true;
}
});
Keep your EditText like this:
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/editNote"
android:hint="Start Typing..."
android:inputType="textMultiLine"
android:gravity="start" />
I don't remember where I got this code from so can't credit the author.
I made the necessary changes according to my need.
I'm working on an Android app and I've got 2 editviews and a label. The user can enter 2 values and the label shows some calculation using input from the editviews. What I want is the following;
user enters either value with soft-keyboard
user presses "Return" softkey
editview should lose focus
the soft-keyboard should disappear
textview label should be recalculated
Now, the v.clearFocus only seems to works when there is another widget that can can get focus(?), so I've also added a dummie zero-pixel layout that can 'steal' the focus from the first editview. The Return key works now, but when the user switches focus from edit1 to edit2 by simply tapping then HideKeyboard() crashes. I've tried checking if inputMethodManager==null but that didn't help.
This all feels like I'm hacking to trick Android into doing some common UI behaviour, so I can't help but think that I'm overlooking something here.
Any help would be greatly appreciated. Btw I know this is similar to this question: How to lose the focus of a edittext when "done" button in the soft keyboard is pressed?
But I've tried that and it doesn't work.
So my layout xml is this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- Dummy control item so that first textview can lose focus -->
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
<EditText
android:id="#+id/editTest1"
android:layout_width="250px"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:imeOptions="actionDone" >
</EditText>
<EditText
android:id="#+id/editTest2"
android:layout_width="250px"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:imeOptions="actionDone" >
</EditText>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test123" />
</LinearLayout>
And the source is this:
public class CalcActivity extends Activity implements OnFocusChangeListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab2_weight);
EditText testedit = (EditText) findViewById(R.id.editTest1);
testedit.setOnFocusChangeListener(this);
testedit.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_DONE){
//Clear focus here from edittext
Log.d("test app", "v.clearFocus only works when there are other controls that can get focus(?)");
v.clearFocus();
}
return false;
}
});
}
public void hideSoftKeyboard() {
InputMethodManager inputMethodManager = (InputMethodManager) this.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
}
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus == false) {
Log.d("unitconverter", "onFocusChange hasFocus == false");
// update textview label
TextView bla = (TextView) findViewById(R.id.textView1);
bla.setText(String.format("%s + %s", (((EditText) findViewById(R.id.editTest1)).getText()), (((EditText) findViewById(R.id.editTest2)).getText())));
// hide keyboard
hideSoftKeyboard();
}
}
}
I have to different EditText like these :
EditText1 has the inputType "textCapWords"
EditText2 has the inputType "number"
When I launch my activity, I instantly request focus on the EditText1. The only way to do so that is working is to add this on the onCreate :
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
EDITTEXT1.requestFocus();
When the user clicks on "Return" on his soft Keyboard, I want the EDITTEXT2 to obtain focus. So I added this :
EDITTEXT1.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == 66) { // CODE FOR RETURN
EDITTEXT2.requestFocus();
}
return false;
}
});
But when I do so, nothing happens. All my EditText are set FocusableInTouchMode and Focusable :
<EditText
android:id="#+id/EDITTEXT1"
android:layout_width="0dip"
android:layout_height="20dip"
android:layout_weight="2"
android:background="#color/White"
android:ems="10"
android:hint="My First EditText"
android:inputType="textCapWords"
android:padding="2dip"
android:singleLine="true"
android:textColor="#color/Black"
android:textSize="12dip" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/EDITTEXT2"
android:layout_width="0dip"
android:layout_height="20dip"
android:layout_weight="2"
android:background="#color/White"
android:ems="10"
android:hint="My Second EditText"
android:inputType="textCapWords"
android:padding="2dip"
android:singleLine="true"
android:textColor="#color/Black"
android:textSize="12dip" >
</EditText>
Do you have any idea on how I could focus the EDITTEXT2 after pressing OK on EDITTEXT1 ?
Thanks in advance!
Thanks a lot for your answer. I finally found the easiest way to answer my own question :
Add :
android:nextFocusRight="#+id/EDITTEXT2"
To the properties of EDITTEXT1.
Nothing else is needed in order to go to the EDITTEXT2 after clicking "Enter" on EDITTEXT1.
you should create your own EditText which extended from EditText firstly, then override the mothod dispatchKeyEventPreIme like this:
#Override
public boolean dispatchKeyEventPreIme(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
return super.dispatchKeyEventPreIme(event);
} else if (event.getAction() == KeyEvent.ACTION_UP) {
hideInputMethod();
//change your focus at here
return true;
}
}
return super.dispatchKeyEventPreIme(event);
}
protected void hideInputMethod() {
InputMethodManager imm = (InputMethodManager)getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (null != imm) {
imm.hideSoftInputFromWindow(getWindowToken(), 0);
}
}
By the way, it not a good way to code like that if (keyCode == 66) { // CODE FOR RETURN, and also 66 is not the value of back, it's key enter.
public static final int KEYCODE_ENTER = 66;
you'd better write KeyEvent.KEYCODE_BACK or KeyEvent.KEYCODE_ENTER instead.
I wish this could help you.