Bad cursor color on custom edittext - android

I tried to override the AutoCompleteTextView and the cursor in the field is now white. Because of bright gray background it is difficult to recognize them. I don't understand how it could be possible, I didn't set any custom style, the other fields are ok and show standart green cursor.
Here is my custom class:
public class CustomerAutoCompleteTextView extends AutoCompleteTextView {
NewTransactionDialogFragment dialog;
public CustomerAutoCompleteTextView(Context context) {
super(context);
}
public CustomerAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomerAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK &&
event.getAction() == KeyEvent.ACTION_UP) {
if(dialog!=null){
dialog.showFieldsOverCustomer();
}
}else if(keyCode == KeyEvent.KEYCODE_ENTER &&
event.getAction() == KeyEvent.ACTION_UP){
if(dialog!=null){
dialog.hideKeyboard(); //this part doesn't work if I press ENTER, but it will be my other question
}
}
return super.onKeyPreIme(keyCode, event);
}
public void setDialog(NewTransactionDialogFragment dialog){
this.dialog=dialog;
}
}
And here is my layout:
<my_package.CustomerAutoCompleteTextView
android:id="#+id/inputCustomer"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="#drawable/apptheme_edit_text_holo_light"
android:hint="#string/hint_customer_email"
android:cursorVisible="true"
android:singleLine="true"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
android:drawablePadding="10dp"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:imeOptions="actionDone"
android:inputType="none" />

If you're using the support-v7 AppCompat libraries, all of your custom views should extend their AppCompat counterparts. Try extending AppCompatAutoCompleteTextView.
Source

You can set android:textCursorDrawable attribute to #null the cursor colour will be the same as your text colour.
The cursor probably changed because you change themes.

Related

AutoCompleteTextView threshhold is not working as expected

I want to show dropdown list even for completely empty string. Because my coded logic in Adapter is that if filtering constraint is empty or null, show all results available. But for some reason, results are filtered and returned, but dropdown is hidden while text is empty.
I set threshold to 0 but its not working. I even tried negative values.
I tried to override afterTextChanged listener and force to show dropdown while query is empty or null, but AutoCompleteTextview is forcing to hide dropdown anyways.
In documentation I found this line:
When threshold is less than or equals 0, a threshold of 1 is applied.
That means I cant do this behavior?
You have to create your own custom autocomplete text view by extending e.g. the AppCompatAutoCompleteTextView.
Here is mine:
public class InstantAutoCompleteTextView extends AppCompatAutoCompleteTextView {
public InstantAutoCompleteTextView(Context context) {
super(context);
}
public InstantAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public InstantAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public boolean enoughToFilter() {
return true;
}
#Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
super.onFocusChanged(focused, direction, previouslyFocusedRect);
if (focused && getAdapter() != null) {
performFiltering(getText(), 0);
}
}
#SuppressLint("ClickableViewAccessibility")
#Override
public boolean onTouchEvent(MotionEvent event) {
if (MotionEvent.ACTION_UP != event.getAction()) {
return super.onTouchEvent(event);
}
if (!isPopupShowing() && getAdapter() != null) {
showDropDown();
}
return super.onTouchEvent(event);
}
}
Then you can use it in the layout file:
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<your.package.name.util.InstantAutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/hint"
android:imeOptions="actionDone"
android:inputType="text|textCapWords" />
</com.google.android.material.textfield.TextInputLayout>

Edit text is not editable in Android Pie 9.0

Hello Everyone,
I m having an issue with the AppCompatEditText. I have override an AppCompatEditText and add Enter button in keyboard (setImeOptions(EditorInfo.IME_ACTION_GO)). This is working fine on all Android versions, expect on 9.0. Here is the code of my Class
public class EditTextWithGOButton extends AppCompatEditText {
private EditTextAction editTextAction;
public interface EditTextAction {
void onGoButtonClicked(String text);
}
public void setEditTextAction(EditTextAction editTextAction) {
this.editTextAction = editTextAction;
}
public EditTextWithGOButton(Context context) {
super(context);
changeImeOption();
}
public EditTextWithGOButton(Context context, AttributeSet attrs) {
super(context, attrs);
changeImeOption();
}
public EditTextWithGOButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
changeImeOption();
}
private void changeImeOption() {
this.setImeOptions(EditorInfo.IME_ACTION_GO);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
if (editTextAction != null){
editTextAction.onGoButtonClicked(getText().toString());
}
}
// Returning false allows other listeners to react to the press.
return false;
// Handle all other keys in the default way
// return super.onKeyDown(keyCode, event);
}
}
And My XML.
<com.app.common.views.EditTextWithGOButton
android:id="#+id/edt_search_bar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3.2"
android:background="#drawable/search_edittext_rounded_corner_background"
android:drawableRight="#android:drawable/ic_menu_search"
android:drawableTint="#color/colorPrimary"
android:hint="#string/search_your_activity"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:maxLines="1"
android:paddingTop="05dp"
android:paddingBottom="05dp"
android:textColorHint="#color/colorPrimary" />
I have tired to set it focus able manually. When I remove this.setImeOptions(EditorInfo.IME_ACTION_GO); and onKeyDown listener, AppCompatEditText became editable , But keyboard won't pop up. I have tried EditText as well.But issue remain same. Any Solution ??

InputType has no effect on a custom EditText

I created this custom view extended from AppCompatEditText :
public class NoteEditText extends AppCompatEditText {
// INTERFACES ----------------------------------------------------------------------------------------------------
public interface OnKeyPreImeListener {
void onKeyPreIme(int keyCode, KeyEvent event);
}
// ATTRIBUTES ----------------------------------------------------------------------------------------------------
private MovementMethod movementMethod;
private KeyListener keyListener;
private OnKeyPreImeListener onKeyPreImeListener;
// CONSTRUCTORS ----------------------------------------------------------------------------------------------------
public NoteEditText(Context context) {
super(context);
this.movementMethod = this.getMovementMethod();
this.keyListener = this.getKeyListener();
}
public NoteEditText(Context context, AttributeSet attrs) {
super(context, attrs);
this.movementMethod = this.getMovementMethod();
this.keyListener = this.getKeyListener();
}
public NoteEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.movementMethod = this.getMovementMethod();
this.keyListener = this.getKeyListener();
}
// SETTERS ----------------------------------------------------------------------------------------------------
public void setOnKeyPreImeListener(OnKeyPreImeListener onKeyPreImeListener) {
this.onKeyPreImeListener = onKeyPreImeListener;
}
// METHODS ----------------------------------------------------------------------------------------------------
public void enable() {
this.setMovementMethod(this.movementMethod);
this.setKeyListener(this.keyListener);
this.setFocusableInTouchMode(true);
this.setImeOptions(EditorInfo.IME_ACTION_DONE);
this.setRawInputType(InputType.TYPE_CLASS_TEXT);
}
public void disable() {
this.setMovementMethod(null);
this.setKeyListener(null);
}
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (this.onKeyPreImeListener != null)
this.onKeyPreImeListener.onKeyPreIme(keyCode, event);
return true;
}
}
I call it like this :
<com.company.adapters.items.NoteEditText
android:id="#+id/note"
style="#style/AppTheme.SubItem"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/content_margin_xs"
android:gravity="center_vertical"
android:hint=""
android:imeOptions="actionDone"
android:inputType="textMultiLine|textCapSentences|textNoSuggestions"
android:minHeight="#dimen/content_subitem_height"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#id/note_icon"
app:layout_constraintTop_toBottomOf="#+id/name" />
It works well, except the "textMultiLine|textCapSentences|textNoSuggestions" inputTypes. Neither "textCapSentences" nor "textNoSuggestions" are applied, although "textMultiLine" is working.
If I use the exact same configuration but with the original EditText view, all the inputTypes work... very strange.
this.setRawInputType(InputType.TYPE_CLASS_TEXT);
From your enable function. This is overriding the input type.

AppCompatEditText is not shown

I have created a custom EditText to prevent pressing Enter (new line) in my input field. This is the class:
public class MyTextView extends android.support.v7.widget.AppCompatEditText {
public MyTextView(Context context) {
super(context);
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyTextView(Context context,
AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
// Just ignore the [Enter] key
return true;
}
// Handle all other keys in the default way
return super.onKeyDown(keyCode, event);
}
}
First I extended the EditText class and it was working fine, but there was a warning to use AppCompatEditText instead (which extends EditText) . Now it doesn't appear. I've tried to enable, etc, but nothing seems to work. Anyone got an idea about this?
Bonus question: what about the Liskov substitution principle?
Thank you in advance.

Android TimePicker (Wheel Style) not responding correctly to flick gestures inside ScrollView

I have a dialog box that contains a Scrollview, which contains a layout with two TimePickers.
The timepickers are the newer style ones, what's in ICS.
The problem is that they seem to fight for focus when you change the time by dragging the wheel, or flicking it. It will change the time just a little, and then the layout will scroll instead.
Any ideas?
Thanks in advance.
I had the same problem when using the Holo theme, and here is where I found the solution: https://groups.google.com/forum/?fromgroups#!topic/android-developers/FkSfJI6dH8w
You must implement your custom DatePicker or TimePicker and override the following method:
#Override
public boolean onInterceptTouchEvent(MotionEvent ev)
{
if (ev.getActionMasked() == MotionEvent.ACTION_DOWN)
{
ViewParent p = getParent();
if (p != null)
p.requestDisallowInterceptTouchEvent(true);
}
return false;
}
Because the link from Klemens Zleptnig is broken, here is a complete example. This fix helps with the scroll of a TabLayout too btw. I excluded the area around the big numbers in the top of the TimePicker because they don't need the scroll event anyway.
xml:
<com.name.app.MyTimePicker
android:id="#+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
.../>
java:
public class MyTimePicker extends TimePicker {
public MyTimePicker(Context context) {
super(context);
}
//This is the important constructor
public MyTimePicker(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyTimePicker(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public MyTimePicker(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev)
{
if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
//Excluding the top of the view
if(ev.getY() < getHeight()/3.3F)
return false;
ViewParent p = getParent();
if (p != null)
p.requestDisallowInterceptTouchEvent(true);
}
return false;
}
}

Categories

Resources