Enable EditText(or button) on Long pressed - android

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

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.

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.

Hiding the soft keyboard when typing in EditText in Android 4

I am writing a calculator for Android, for inputting expression I use EditText. As I create my buttons - I do not need a software keyboard, but I want to change the cursor position, text selection, copy, paste. In a word - everything as it is, only the virtual keyboard is not displayed.
In version 2.3 I could write:
EditText.setInputType (InputType.TYPE_NULL);
and it worked perfectly. In version 4 of the cursor is not displayed, the menu does not work, etc. Tried a bunch of ways - you can not move the cursor, the keyboard is displayed, and it was never really explained.
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0); //cursor not showing
------------------------------------------------------------------------
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); //not working
I want to make it as in Panecal, MobiCalc Free, Scientific Calculator. I would be happy with any helpful suggestions on this.
P.S. Sorry for my English.
From the link posted below, here is an example to consume on touch for an Edittext
editText_input_field.setOnTouchListener(otl);
private OnTouchListener otl = new OnTouchListener() {
public boolean onTouch (View v, MotionEvent event) {
return true; // the listener has consumed the event
}
};
Here is another example from the same website. This claims to work but seems like a bad idea since your EditBox is NULL it will be no longer an editor:
MyEditor.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
int inType = MyEditor.getInputType(); // backup the input type
MyEditor.setInputType(InputType.TYPE_NULL); // disable soft input
MyEditor.onTouchEvent(event); // call native handler
MyEditor.setInputType(inType); // restore input type
return true; // consume touch even
}
});
Hope this points you in the right direction
The above answer was taken from - how to block virtual keyboard while clicking on edittext in android?
This might work too
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
The exact solution to this is by setting the flag textIsSelectable in EditText to true. This will retain the cursor and you'll be able to utilise the basic select/copy/cut/paste,etc functions.You can set it in your xml layout like this:
You can set it programmatically like this:
EditText edit_text = (EditText) findViewById(R.id.editText);
edit_text.setTextIsSelectable(true);
Or in your XML Layout :
<EditText
...
android:textIsSelectable="true"
/>
For anyone using API 10 and below, hack is provided here : https://stackoverflow.com/a/20173020/7550472

How to intentionally go to new line in an EditText?

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.

Android: select all text on focus on EditField

I'm trying to get Android to select all the text in an EditText field when it gets the focus. I'm using this attribute in the layout (on both fields):
android:selectAllOnFocus="true"
I'm not sure if this is relevant, but to get the cursor to the first editable field (there's also a disabled field before it), I'm using the following commands:
quizStartNum.setFocusable(true);
quizStartNum.requestFocus();
But, while the cursor does move to the desired field when the layout is first displayed, the text doesn't get highlighted; instead the cursor ends up to the left of the text, the default behavior. If I move to the second field by touching it, all the text is selected as desired. Then, if I move back to the first field, again by touching it, the text is also completely selected. I would like to have the behavior right from the start. Is there a way to do this?
If android:selectAllOnFocus="true" does not work, try calling setSelectAllOnFocus(true) on that particular EditText.
If that doesn't work either, this is another workaround from a previous SO post.
EditText dummy = ...
dummy.setOnFocusChangedListener(new OnFocusChangedListener(){
public void onFocusChange(View v, boolean hasFocus){
if (hasFocus) && (isDummyText())
((EditText)v).selectAll();
}
});
I had a similar issue and android:selectAllOnFocus="true" did not work for me.
The reason was that i was programatically requesting focus to the EditText before it was displayed. So if you are doing this for a EditText in an AlertDialog make sure you show it before you request focus to the EditText.
EditText should be focussed after it displayed.
Following workout worked for me,
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// Set Input Methode
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
}
#Override
public void onStart() {
super.onStart();
new Handler().post(new Runnable() {
#Override
public void run() {
Dialog dialog = getDialog();
if (dialog != null) {
// Set Layout
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
// Set Cancelable False
setCancelable(false);
// Set Focus Here <------------------------
uiET_myTextView.requestFocus();
}
}
});
}
Try removing your focus commands. They aren't necessary, Android should focus on the first field automatically?
I also noticed that with android:selectAllOnFocus="true" seemed not to work, I used the mouse to select EditText in the emulator, but if I used my finger and touched it, as if on the phone, it worked. If you don't have a touch screen on your computer you may have to install your app on a physical device to test it.
Android Studio may not recognize the mouse in this case
I'll refer to this older post.
If you just want your EditText to show a hint (for "what goes in here"), you might want to use the Android's XML-attribute hint (link).

Categories

Resources