2012-03-09 10:42:43 -08:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2013-03-25 15:44:45 -07:00
|
|
|
package org.mozilla.gecko.widget;
|
|
|
|
|
|
|
|
import org.mozilla.gecko.R;
|
2012-03-09 10:42:43 -08:00
|
|
|
|
|
|
|
import android.content.Context;
|
2013-03-28 08:36:07 -07:00
|
|
|
import android.graphics.Rect;
|
2012-03-09 10:42:43 -08:00
|
|
|
import android.text.SpannableString;
|
2013-03-28 08:36:07 -07:00
|
|
|
import android.text.style.BackgroundColorSpan;
|
2012-03-09 10:42:43 -08:00
|
|
|
import android.text.style.UnderlineSpan;
|
2012-07-27 17:53:54 -07:00
|
|
|
import android.util.AttributeSet;
|
2012-03-09 10:42:43 -08:00
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
public class LinkTextView extends TextView {
|
2013-03-28 08:36:07 -07:00
|
|
|
private final BackgroundColorSpan mFocusBackgroundSpan;
|
|
|
|
private boolean mFocusApplied;
|
|
|
|
|
2012-03-09 10:42:43 -08:00
|
|
|
public LinkTextView(Context context, AttributeSet attrs) {
|
|
|
|
super(context, attrs);
|
2013-03-28 08:36:07 -07:00
|
|
|
mFocusBackgroundSpan = new BackgroundColorSpan(context.getResources().getColor(R.color.highlight_focused));
|
2012-03-09 10:42:43 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setText(CharSequence text, BufferType type) {
|
|
|
|
SpannableString content = new SpannableString(text + " \u00BB");
|
|
|
|
content.setSpan(new UnderlineSpan(), 0, text.length(), 0);
|
|
|
|
|
|
|
|
super.setText(content, BufferType.SPANNABLE);
|
|
|
|
}
|
2013-03-28 08:36:07 -07:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
|
|
|
|
super.onFocusChanged(focused, direction, previouslyFocusedRect);
|
|
|
|
|
|
|
|
if (focused == mFocusApplied) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mFocusApplied = focused;
|
|
|
|
|
|
|
|
CharSequence text = getText();
|
|
|
|
if (text instanceof SpannableString) {
|
|
|
|
SpannableString spannable = (SpannableString)text;
|
|
|
|
if (focused) {
|
|
|
|
spannable.setSpan(mFocusBackgroundSpan, 0, text.length(), 0);
|
|
|
|
} else {
|
|
|
|
spannable.removeSpan(mFocusBackgroundSpan);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-09 10:42:43 -08:00
|
|
|
}
|