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

Update and add parked text in front of text #5

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
fix bugs, add frontParked option
foxsake committed Jun 27, 2017
commit 3ebd5b8fa6b63c51ea9b6bb43864d435c75205bc
208 changes: 107 additions & 101 deletions parkedtextview/src/main/java/com/goka/parkedtextview/ParkedTextView.java
Original file line number Diff line number Diff line change
@@ -20,16 +20,14 @@ public class ParkedTextView extends android.support.v7.widget.AppCompatEditText

// Able to set
private String mParkedText = "";
private String mHintText = "";
private boolean mIsBoldParkedText = true;
private boolean mIsParkedInFront = false;
private String mParkedTextColor = DEFAULT_TEXT_COLOR;
private String mParkedHintColor = DEFAULT_TEXT_COLOR;

// Unable to set
private String mText = null;
private enum TypingState {
Start, Typed
}
private TypingState mTypingState = TypingState.Start;

public ParkedTextView(Context context) {
super(context);
@@ -45,13 +43,13 @@ public ParkedTextView(Context context, AttributeSet attrs, int defStyleAttr) {

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ParkedTextView, defStyleAttr, 0);

mIsParkedInFront = a.getBoolean(R.styleable.ParkedTextView_parkedInFront, false);

mParkedText = a.getString(R.styleable.ParkedTextView_parkedText);
if (mParkedText == null) {
mParkedText = "";
}

String hint = a.getString(R.styleable.ParkedTextView_parkedHint);

mParkedTextColor = a.getString(R.styleable.ParkedTextView_parkedTextColor);
if (mParkedTextColor == null) {
mParkedTextColor = ParkedTextView.DEFAULT_TEXT_COLOR;
@@ -64,39 +62,69 @@ public ParkedTextView(Context context, AttributeSet attrs, int defStyleAttr) {

mIsBoldParkedText = a.getBoolean(R.styleable.ParkedTextView_parkedTextBold, true);

init();
setHintText(a.getString(R.styleable.ParkedTextView_parkedHint));

if (hint != null) {
setPlaceholderText(hint);
}
init();

a.recycle();

setEnabled(true);
setFocusable(true);
setFocusableInTouchMode(true);
}

private void init() {
mText = "";
observeText();

mTypingState = TypingState.Start;
addTextChangedListener(new ParkedTextViewWatcher(this));
}

public void setHintText(String hint){
mHintText = hint;
if(mHintText == null)
mHintText = "";
setPlaceholderText();
}

public String getHintText(){
return mHintText;
}

public String getParkedText() {
return mParkedText;
}

public void setParkedText(String parkedText) {
if (!TextUtils.isEmpty(mText)) {
String typed = mText.substring(0, getBeginningPositionOfParkedText());
mText = typed + parkedText;
//TODO review
String typed;
if(!TextUtils.isEmpty(mText)){
if(mIsParkedInFront) {
typed = mText.substring(getParkedText().length());
mText = getParkedText() + typed;
}else{
typed = mText.substring(0, getBeginningPositionOfParkedText());
mText = typed + getParkedText();
}
mParkedText = parkedText;

textChanged();
} else {
}else{
mParkedText = parkedText;
}
}

public String getTypedText() {
if(!mIsParkedInFront && mText.endsWith(mParkedText)){
return mText.substring(0, getBeginningPositionOfParkedText());
}else if(mIsParkedInFront && mText.startsWith(mText) && !mText.isEmpty())
return mText.substring(getParkedText().length());
else
return mText;
}

public void setTypedText(String typedText) {
//TODO change when not empty
textChanged(typedText);
}


private int getBeginningPositionOfParkedText() {
int position = mText.length() - mParkedText.length();
if (position < 0) {
@@ -109,44 +137,33 @@ private void goToBeginningOfParkedText() {
setSelection(getBeginningPositionOfParkedText());
}

public String getTypedText() {
if (mText.endsWith(mParkedText)) {
return mText.substring(0, getBeginningPositionOfParkedText());
}
return mText;
}

public void setTypedText(String typedText) {
if(typedText.endsWith(mParkedText))
mText = typedText;
else
mText = typedText + mParkedText;
observeText();

textChanged();
private void goToEndOfParkedText() {
setSelection(getText().length());
}

private void setEmptyText() {
setTypedText("");
}

public void setPlaceholderText(String placeholderText) {
Spanned hint = null;
public void setPlaceholderText() {
Spanned hint;
String parkedTextColor = reformatColor(mParkedTextColor);
String parkedHintColor = reformatColor(mParkedHintColor);
if (mIsBoldParkedText) {
hint = fromHtml(String.format("<font color=\"#%s\">%s</font><font color=\"#%s\"><b>%s</b></font>", parkedHintColor, placeholderText, parkedTextColor, mParkedText));
} else {
hint = fromHtml(String.format("<font color=\"#%s\">%s</font><font color=\"#%s\">%s</font>", parkedHintColor, placeholderText, parkedTextColor, mParkedText));
}
if(mIsParkedInFront)
if (mIsBoldParkedText) {
hint = fromHtml(String.format("<font color=\"#%s\"><b>%s</b></font><font color=\"#%s\">%s</font>", parkedTextColor, getParkedText(), parkedHintColor, getHintText()));
} else {
hint = fromHtml(String.format("<font color=\"#%s\">%s</font><font color=\"#%s\">%s</font>",parkedTextColor , getParkedText(), parkedHintColor, getHintText()));
}
else
if (mIsBoldParkedText) {
hint = fromHtml(String.format("<font color=\"#%s\">%s</font><font color=\"#%s\"><b>%s</b></font>", parkedHintColor, getHintText(), parkedTextColor, getParkedText()));
} else {
hint = fromHtml(String.format("<font color=\"#%s\">%s</font><font color=\"#%s\">%s</font>", parkedHintColor, getHintText(), parkedTextColor, getParkedText()));
}
super.setHint(hint);
}

// Call when TypedText is changed
private String observeText() {
return mText = getTypedText() + mParkedText;
}

private String reformatColor(String color) {
if (color.startsWith("#")) {
color = color.substring(1);
@@ -160,39 +177,52 @@ private String reformatColor(String color) {

private Spanned getHtmlText() {
String parkedTextColor = reformatColor(mParkedTextColor);
if (mIsBoldParkedText) {
if(mIsParkedInFront){
if (mIsBoldParkedText) {
return fromHtml(
String.format(
"<font color=\"#%s\"><b>%s</b></font><font color=\"#%s\">%s</font>",
parkedTextColor, getParkedText(), parkedTextColor, getTypedText()));
}
return fromHtml(
String.format(
"<font color=\"#%s\">%s</font><font color=\"#%s\"><b>%s</b></font>",
parkedTextColor, getTypedText(), parkedTextColor, mParkedText));
String.format("<font color=\"#%s\">%s</font>", parkedTextColor,
getParkedText() + getTypedText()));
}else{
if (mIsBoldParkedText) {
return fromHtml(
String.format(
"<font color=\"#%s\">%s</font><font color=\"#%s\"><b>%s</b></font>",
parkedTextColor, getTypedText(), parkedTextColor, getParkedText()));
}
return fromHtml(
String.format("<font color=\"#%s\">%s</font>", parkedTextColor,
getTypedText() + getParkedText()));
}
return fromHtml(
String.format("<font color=\"#%s\">%s</font>", parkedTextColor,
getTypedText() + mParkedText));
}

private void textChanged() {
switch (mTypingState) {
case Start:
if (mText.length() <= 0) {
return;
}
setText(getHtmlText(), BufferType.SPANNABLE);
goToBeginningOfParkedText();

mTypingState = TypingState.Typed;
break;
case Typed:
if (mText.equals(mParkedText)) {
mTypingState = TypingState.Start;
setText(getHtmlText(), BufferType.SPANNABLE);
return;
}
setText(getHtmlText(), BufferType.SPANNABLE);
goToBeginningOfParkedText();
default:
break;
private void textChanged(String typed) {
if(typed.startsWith(getParkedText()) && typed.endsWith(getParkedText())){
mText = typed;
}else if(getText().length() <= 1){
if(mIsParkedInFront)
mText = getParkedText() + typed;
else
mText = typed + getParkedText();
setText(getHtmlText(), BufferType.SPANNABLE);
}else{
if(mIsParkedInFront)
if(typed.startsWith(getParkedText()))
mText = typed;
else{
if(typed.endsWith(getParkedText()))
mText = typed;
}
setText(getHtmlText(), BufferType.SPANNABLE);
}
if(mIsParkedInFront)
goToEndOfParkedText();
else
goToBeginningOfParkedText();
}

public boolean isBoldParkedText() {
@@ -222,46 +252,22 @@ public void setParkedHintColor(String parkedHintColor) {
private static class ParkedTextViewWatcher implements TextWatcher {

private ParkedTextView mParkedTextView;
private boolean mIsDeleteText;

public ParkedTextViewWatcher(ParkedTextView parkedTextView) {
ParkedTextViewWatcher(ParkedTextView parkedTextView) {
this.mParkedTextView = parkedTextView;
}

@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(before > 0) {
mIsDeleteText = true;
} else {
mIsDeleteText = false;
}
}
public void onTextChanged(CharSequence s, int start, int before, int count) {}

@Override
public void afterTextChanged(Editable s) {
mParkedTextView.removeTextChangedListener(this);
String text = s.toString();
if (mIsDeleteText) {
if (text.length() < mParkedTextView.getParkedText().length()) {
mParkedTextView.setEmptyText();
} else {
String parkedText;
if(mParkedTextView.getBeginningPositionOfParkedText() > 0)
parkedText = text.substring(mParkedTextView.getBeginningPositionOfParkedText());
else
parkedText = text;
if (!parkedText.equals(mParkedTextView.getParkedText())) {
mParkedTextView.setEmptyText();
} else {
mParkedTextView.setTypedText(text);
}
}
} else {
mParkedTextView.setTypedText(text);
}
mParkedTextView.setTypedText(text);
mParkedTextView.addTextChangedListener(this);
}
}
1 change: 1 addition & 0 deletions parkedtextview/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@
<attr name="parkedTextColor" format="string" />
<attr name="parkedHintColor" format="string" />
<attr name="parkedTextBold" format="boolean" />
<attr name="parkedInFront" format="boolean" />
</declare-styleable>

</resources>