Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set Color programmatically or with XML #253

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import android.os.Build;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.ColorRes;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.support.v4.graphics.drawable.DrawableCompat;
Expand Down Expand Up @@ -543,6 +544,10 @@ private void applyXmlAttributes(AttributeSet attrs) {
, Util.getColor(getContext(), R.color.hint_color)));
setSuggestionRightIconColor(a.getColor(R.styleable.FloatingSearchView_floatingSearch_suggestionRightIconColor
, Util.getColor(getContext(), R.color.gray_active_icon)));

mSearchInput.setCursorColor(a.getColor(R.styleable.FloatingSearchView_floatingSearch_cursorColor
, Util.getColor(getContext(), R.color.gray_active_icon)));

} finally {
a.recycle();
}
Expand Down Expand Up @@ -1646,6 +1651,15 @@ public void onAnimationEnd(Animator animation) {
}
}

/**
* Sets the cursor color of the EditText default = #787878
*
* @param color that needs to be set
*/
public void setCursorColor(@ColorRes int color) {
mSearchInput.setCursorColor(Util.getColor(getContext(), color));
}

/**
* Sets the listener that will be notified when the suggestion list's height
* changes.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package com.arlib.floatingsearchview.util.view;

import android.content.Context;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.lang.reflect.Field;

public class SearchInputView extends EditText {

Expand Down Expand Up @@ -42,6 +48,34 @@ private void init() {
setOnKeyListener(mOnKeyListener);
}

public void setCursorColor(int color) {
try {
// Get the cursor resource id
Field field = TextView.class.getDeclaredField("mCursorDrawableRes");
field.setAccessible(true);
int drawableResId = field.getInt(this);

// Get the editor
field = TextView.class.getDeclaredField("mEditor");
field.setAccessible(true);
Object editor = field.get(this);

// Get the drawable and set a color filter
Drawable drawable = ContextCompat.getDrawable(this.getContext(), drawableResId);
drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
Drawable[] drawables = {drawable, drawable};

// Set the drawables
field = editor.getClass().getDeclaredField("mCursorDrawable");
field.setAccessible(true);
field.set(editor, drawables);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}

@Override
public boolean onKeyPreIme(int keyCode, KeyEvent ev) {
if (ev.getKeyCode() == KeyEvent.KEYCODE_BACK && mOnKeyboardDismissedListener != null) {
Expand Down
1 change: 1 addition & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@
<attr name="floatingSearch_dividerColor" format="color"/>
<attr name="floatingSearch_dimBackground" format="boolean"/>
<attr name="floatingSearch_suggestionRightIconColor" format="color"/>
<attr name="floatingSearch_cursorColor" format="color" />
</declare-styleable>
</resources>