Android EditText Long Press Behaviour - android

I'm having some problems with the EditText in Android. Usually, when a user long clicks on the EditText or double-taps it, the word the cursor is in is highlighted, and the Contextual Actionbar (CAB) for the EditText pops up (on Android 3.0 and later).
My EditText did indeed do this until recently: the issue is that now long-pressing the EditText results in the selected word being "picked up", i.e. an enlargened "ghost" image of the word is picked up, and you can drag and drop it anywhere else in the text where it is inserted. I have not touched the code for the EditText at all. Anyone running Google Chrome (desktop version) can see the type of behaviour I mean if they highlight any text and drag it with their cursor.
I think the issue may have been caused by my device's upgrade to Android 4.2.2 recently. I have looked all over Google for information, but it has turned up nothing. Also, double-tapping the word also does not bring up the CAB as expected - it flashes for a second then goes away - but I do not think this is a related issue.
I really need this behaviour to stop, as my app cannot function without the CAB. So the question is: how can I get the "normal" behaviour back? All behaviour has been seen on a Nexus 4 running Android 4.2.2. All help is much appreciated; thank you very much!
XML Layout code for the EditText:
<EditText
android:id="#+id/editor_mainText"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_above="#+id/options_bottom"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/options_top"
android:background="#FFE7E7E7"
android:imeOptions="flagNoExtractUi"
android:inputType="textMultiLine|textNoSuggestions|textVisiblePassword"
android:padding="8dp"
android:scrollbars="none"
android:textCursorDrawable="#null"
android:textSize="17sp"
android:typeface="monospace" >
<requestFocus />
</EditText>

Note: My original answer was a bit wrong. I hadn't spotted my careless mistake in the code, but now that I have, here is why it didn't work
I've solved it, but I absolutely cannot explain why this behaviour happened in the first place, and why doing the following made any difference. But anyway, it did.
The EditText had been extended in order to make it "flingable" using a Scroller and VelocityTracker. This involved Overriding onTouchEvent(MotionEvent). But there was a little mistake in the code, where super.onTouchEvent(MotionEvent) would be called twice, as I had accidentally left out one of the break; statements in the switch-case, except during the MotionEvent.ACTION_MOVE event. The app worked fine even with this error all the way up to Android 4.2.1. I hadn't touched the code following the Android 4.2.2 upgrade, but for whatever reason, in the new version of Android, this small error triggered this weird behaviour.
So basically, it was a very basic mistake, and now I have learnt: always make sure to close off a case in a switch-case with a break statement!

I faced almost exact same behavior. I coudnt find any related question on stackoverflow, so will put my answer here. Maby it will be usefull for somebody. So if you ever faced a problem, that after closing copy/paste popup (by clicking anywhere on the screen) you cant select same text again (getting that "ghost" instead) all you have to do is manually disable textIsSelectable attribute and enable it again on view. For that i used this code im my ActivityMain. Poor solution, but its worked for me
override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
if (ev?.action == MotionEvent.ACTION_DOWN) {
val v = currentFocus
if (v is TextView || v is TextInputEditText) {
val outRect = Rect()
v.getGlobalVisibleRect(outRect)
if (!outRect.contains(ev.rawX.toInt(), ev.rawY.toInt())) {
val view = when (v) {
is TextView -> v
is TextInputEditText -> v
else -> null
}
view?.setTextIsSelectable(false)
view?.refreshDrawableState()
view?.setTextIsSelectable(true)
}
}
}
return super.dispatchTouchEvent(ev)
}

Related

Not able to enter values in EditText

I'm a newbie in Android development. Recently I have been facing a problem in the EditTexts(eventhough I havent changed any attributes of EditText) used in my app which I'm currently running in Emulator.
On clicking the EditText it receives focus, but on typing something the focus changes to some other view. But I'm able to enter values if I navigate to EditText using Tab button of the keyboard. This happens to all EditTexts in my application.
Surfed net for getting a solution, but did not find one.Help...
Implementation of one of my EditText:
<EditText
android:id="#+id/edittext_testname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/hintEditText1"/>
Thanks in advance.
Ever tried your app on a physical device?
However, I cannot see anything wrong with your EditText xml. Perhaps there is some java code in your activity catching any events related to that EditText? Or maybe to the other view element you mentioned getting the focus?
I had a similar issue but with a custom style I had defined for my EditText. How I fixed it?
<item name="android:focusable">true</item>
<item name="android:focusableInTouchMode">true</item>
So try applying the "android:focusable" and "android:focusableInTouchMode" properties and see if it makes any difference. They should be applied to the EditText by default but give it a go.
(I would have posted this as a comment but I need more rep)

Android autoCompleteTextView changes with screensize?

Hello fellow Android developpers,
I have a problem regarding the autocompletetextview. I'm developping on a tablet, and I customized it nicely. When I run it on a tablet, it looks like this:
This is actually what I want - everything visible, small items, blue instead of orange. Cool.
And this is what happens when I run the exact same app on a smaller device, for example a Nexus-S:
The original views disappear, have orange stuff, and I only see one suggested item at a time or two at best. This is not what I intended.
If I run this app not in landscape, but in portrait, all works fine. Insane.
Does anybody know what I can do to keep the custom layout on the smaller screen?
The second image you shown here is the result of auto complete text view or edit text when the device is in landscape mode.
If the device is in portrait mode it will display the suggestions in Vertical list, and for landscape mode it will display in horizontal list and it tries to fill the entire screen with edit text and soft keyboard.
I too faced same problem, Please check the Android Developers website for information about Supporting multiple screens, it may help you.
New Answer
To hide soft keyboard when you press Done button you have to use imeOptions for AutoCompleteTextView. Do these changes to your code and try it.
Changes to XML layout file as below
<AutoCompleteTextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:id="#+id/actv" />
In java file
actv = (AutoCompleteTextView)findViewById(R.id.actv);
actv.setOnEditorActionListener(new 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;
}
});
I hope it may help you.

Android button setColorFilter behaviour [duplicate]

(I changed the question a bit, because the problem is a bit clearer now)
I have 4 buttons on my application, and when a user clickes certain button
I change that button color.
when button 3 is clicked I want to change his color to green, otherwise I want remove his green filter (when button1/2/4 are clicked).
If I click on button 3 It does get the green filter. If then I click button 4 it removes the green filter, but if I click button 1 or 2, nothing happens.
When I switched the position of the buttons in the XML, and put button3 first, It doesnt happen, ideas?
The relevant part of the layout xml is:
<Button
android:id="#+id/ans1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/ans2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/ans3"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/ans4"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
The code is:
if (answer.equals("3"))
{
question.setText("In if");
d.setColorFilter(filter);
}
else
{
question.setText("else");
d.setColorFilter(null);
}
I seem to remember having issues when creating too many ColorFilters before. It doesn't sound for certain like that's what's at fault here, since it's happening right away. Still, what you might try is having the filter as a class variable, and then using it within the if/else block. Also, as Trev mentioned, since you're just wanting to remove the green filter, you can just pass null to setColorFilter and avoid making the transparent filter, so you'd end up with something like this:
//in main class
PorterDuffColorFilter greenFilter =
new PorterDuffColorFilter(Color.GREEN, PorterDuff.Mode.SRC_ATOP);
//in CheckAnswer()
Drawable d = findViewById(R.id.ans2).getBackground();
if(answer.equals("1") d.setColorFilter(greenFilter)
else d.setColorFilter(null);
The default behavior when calling setColorFilter(ColorFilter) on a Drawable does not automatically invalidate the Drawable, meaning it will not redraw itself solely as a result of the method call.
Try calling d.invalidateSelf() after setting the ColorFilter.
Yesterday I posted a suggestion to a very similar problem that you asked here:
Android button setColorFilter behaviour
It appears that you have edited the code you originally posted there in order to incorporate the suggestions you were given (without acknowledging the answers) and then posted exactly the same code in this question.
The Drawable documentation regarding setColorFilter(ColorFilter cf) states that 'null' may be passed to remove any existing filters. So, perhaps it could be that once the TRANSPARENT filter has been applied, then your subsequent GREEN filter can't be seen? I don't know enough about .setColorFilter and PorterDuff to know for sure, but it's worth a shot. Perhaps try:
d.setColorFilter(null);
d.setColorFilter(filter);
Also you could instead use this method:
setColorFilter(int color, PorterDuff.Mode mode)
You just need to mutate each drawable before setColorFilter.
Drawable d = findViewById(R.id.ans2).getBackground();
d = d.mutate();
d.setColorFilter

Android button setColorFilter behaviour

(I changed the question a bit, because the problem is a bit clearer now)
I have 4 buttons on my application, and when a user clickes certain button
I change that button color.
when button 3 is clicked I want to change his color to green, otherwise I want remove his green filter (when button1/2/4 are clicked).
If I click on button 3 It does get the green filter. If then I click button 4 it removes the green filter, but if I click button 1 or 2, nothing happens.
When I switched the position of the buttons in the XML, and put button3 first, It doesnt happen, ideas?
The relevant part of the layout xml is:
<Button
android:id="#+id/ans1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/ans2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/ans3"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/ans4"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
The code is:
if (answer.equals("3"))
{
question.setText("In if");
d.setColorFilter(filter);
}
else
{
question.setText("else");
d.setColorFilter(null);
}
I seem to remember having issues when creating too many ColorFilters before. It doesn't sound for certain like that's what's at fault here, since it's happening right away. Still, what you might try is having the filter as a class variable, and then using it within the if/else block. Also, as Trev mentioned, since you're just wanting to remove the green filter, you can just pass null to setColorFilter and avoid making the transparent filter, so you'd end up with something like this:
//in main class
PorterDuffColorFilter greenFilter =
new PorterDuffColorFilter(Color.GREEN, PorterDuff.Mode.SRC_ATOP);
//in CheckAnswer()
Drawable d = findViewById(R.id.ans2).getBackground();
if(answer.equals("1") d.setColorFilter(greenFilter)
else d.setColorFilter(null);
The default behavior when calling setColorFilter(ColorFilter) on a Drawable does not automatically invalidate the Drawable, meaning it will not redraw itself solely as a result of the method call.
Try calling d.invalidateSelf() after setting the ColorFilter.
Yesterday I posted a suggestion to a very similar problem that you asked here:
Android button setColorFilter behaviour
It appears that you have edited the code you originally posted there in order to incorporate the suggestions you were given (without acknowledging the answers) and then posted exactly the same code in this question.
The Drawable documentation regarding setColorFilter(ColorFilter cf) states that 'null' may be passed to remove any existing filters. So, perhaps it could be that once the TRANSPARENT filter has been applied, then your subsequent GREEN filter can't be seen? I don't know enough about .setColorFilter and PorterDuff to know for sure, but it's worth a shot. Perhaps try:
d.setColorFilter(null);
d.setColorFilter(filter);
Also you could instead use this method:
setColorFilter(int color, PorterDuff.Mode mode)
You just need to mutate each drawable before setColorFilter.
Drawable d = findViewById(R.id.ans2).getBackground();
d = d.mutate();
d.setColorFilter

Android: Something better than android:ellipsize="end" to add "..." to truncated long Strings?

This property makes
"short and very-long-word"
to
"short and"
. But I want to have smth. like
"short and very-lon..."
Right now I truncate the String in Java code. However, thats based on the number of characters and not the actual length of the link. So, the result isn't very nice.
String title;
if(model.getOrganization().length() > 19) {
title = model.getText().substring(0, 15).trim() + "…";
} else {
title = model.getText();
}
((TextView) findViewById(R.id.TextViewTitle)).setText(title);
Update
Just noticed, this property actually adds "..." in a few cases. But not in all of them:
12345678901234567890 becomes
"12345678901234..."
However,
"1234567890 1234567890"
becomes "1234567890" and not
"1234567890 123..."
Update 2
Now it really gets funky! I just set singleLine=true and removed maxLine (The bug appears with and without setting ellipsize attribute)...
This is a screenshot take from Motorola Milestone with android 2.1 update 1. Same happens on HTC Desire with the same android version
Update 3
Now I use android:ellipsize="marquee". That seems to be the only properly working setting. It's also only moving, when focused. I see it in many other apps also. I guess its common practise.
If it's for a single line try this:
android:ellipsize="end"
android:maxLines="1"
android:scrollHorizontally="true"
android:singleLine="true"
It worked for me.
I had a similar problem and setting this property to the TextView helped:
android:singleLine="true"
Also, I was using a RelativeLayout so I limited the space the TextView could take by setting a left margin to a button to the right of the TextView, so that the text cannot expand further and is forced to truncate its content.
Maybe it's not the solution to your problem, but it helped me in a similar situation.
See my update 3. The workaround was using " marquee". I have seen many apps doing that at this time.
Now, with new versions of Android this feature is most likely fixed and will work as expected. (my guess)
Maybe you could take a look at how the private Ellipsizer class used by the OS works and modify it for your app?
Edit: Here's a working link - http://androidxref.com/5.1.0_r1/xref/frameworks/base/core/java/android/text/Layout.java#1830

Categories

Resources