I'm using Mariotti's CardsLib to create a list with Cards. I create cards (in a for loop) as shown below:
CustomCard aCard = new CustomCard(getActivity().getApplicationContext());
CardHeader aHeader = new CardHeader(getActivity().getApplicationContext());
aHeader.setTitle(item.getHeaderText());
aCard.addCardHeader(aHeader);
aCard.setSpannableMainTitle(true);
aCard.setmSsbTitle(CustomSpannableBuilderForHome.getSpannedText(item));
CardThumbnail thumbnail = new CardThumbnail(getActivity().getApplicationContext());
thumbnail.setDrawableResource(R.drawable.ic_launcher);
aCard.addCardThumbnail(thumbnail);
cards.add(0, aCard);
CustomCard is a class extending Card class. I created this class because I need to assign an SpannableStringBuilder instance, instead of simple String. This is CustomCard:
public class CustomCard extends Card {
protected boolean isSpannableMainTitle;
/**
* Main Title
*/
protected SpannableStringBuilder mSsbTitle;
public SpannableStringBuilder getmSsbTitle() {
return mSsbTitle;
}
public void setmSsbTitle(SpannableStringBuilder mSsbTitle) {
this.mSsbTitle = mSsbTitle;
}
public boolean isSpannableMainTitle() {
return isSpannableMainTitle;
}
public void setSpannableMainTitle(boolean isSpannableMainTitle) {
this.isSpannableMainTitle = isSpannableMainTitle;
}
public CustomCard(Context context) {
super(context);
}
#Override
public void setupInnerViewElements(ViewGroup parent, View view) {
//Add simple title to header
if (view != null) {
TextView mTitleView = (TextView) view.findViewById(R.id.card_main_inner_simple_title);
if (mTitleView != null)
{
mTitleView.setText(isSpannableMainTitle? mSsbTitle : mTitle);
if (isSpannableMainTitle) mTitleView.setMovementMethod(LinkMovementMethod.getInstance());
}
}
}
}
Everything worked fine prior to using maps api V2. Today, the card's title does not appear when the text does not contain spans. If it does have spans, only spans are shown. It is also worth mentioning that the title to be set, is ALWAYS an instance of SpannableStringBuilder.
For example:
Text1 = This is a text with no spans
Text2 = This is a (text) with (spans) <--- where () denote the presence of span.
This is the output:
Card 1:
Picture + Header + no text
Card 2:
Picture + Header + (text) (spans)
That is, the first card has no text, while the latter displays only spanned text (i.e. This is a with, all of them ignored).
As I said, this behavior started once I included Maps API V2. Please help!
Related
I would like to link some text on a TextView to an Activity. This is the TextView that I have:
<TextView
android:id="#+id/termsLink"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/terms"
android:layout_weight="4"/>
where #string/terms is:
<string name="terms">Accept terms & conditions..</string>
If I had a link to a webpage I would do it like this:
TextView link = (TextView) findViewById(R.id.termsLink);
link.setMovementMethod(LinkMovementMethod.getInstance());
but I do not know how to start an Activity when I press the link as when it is a real link (that it links a webpage).
EDIT: Please note that I do not have to handle the onClick event in the full text because the link is only on the part "terms & conditions".
EDIT 2: I have tried using two TextView as suggested on the comments and one of the answers below to make the same effect. But sometimes (depending on the screen) the "terms & conditions" part occupy two lines because it does not fill properly on the available space so the second line it is shown on the second TextView and not on the begining of the second line.
The effect is similar to this:
Accept terms &
conditions.
and I would like that it would be like this:
Accept terms &
conditions.
Thanks in advance!
Create a helper class with inner onClick listener
public class ClickSpan extends ClickableSpan {
private String url;
private OnClickListener listener;
public ClickSpan(String url, OnClickListener listener) {
this.url = url;
this.listener = listener;
}
#Override
public void onClick(View widget) {
if (listener != null) listener.onClick(url);
}
public interface OnClickListener {
void onClick(String url);
}
}
Then convert existing span into clickable one
public static Spannable createClickableSpans(Spanned original, ClickSpan.OnClickListener listener) {
SpannableString result = new SpannableString(original);
URLSpan[] spans = result.getSpans(0, result.length(), URLSpan.class);
for (URLSpan span : spans) {
int start = result.getSpanStart(span);
int end = result.getSpanEnd(span);
int flags = result.getSpanFlags(span);
result.removeSpan(span);
result.setSpan(new ClickSpan(span.getURL(), listener), start, end, flags);
}
return result;
}
So, final usage would be like
TextView link = (TextView) findViewById(R.id.termsLink);
link.setMovementMethod(LinkMovementMethod.getInstance());
link.setText(createClickableSpans((Spanned)link.getText(), new ClickSpan.OnClickListener(){
#Override
public void onClick(String url){
//Handle URL on text view click
}
}));
To make only part of a TextView clickable, you can use a spannable inside the TextView and set an onClick event listener. From here, you launch the activity with an intent as usual. You can limit the clickable section of the text by specifying the character positions (start to end)
Checkout this answer by #becomputer06
How to set the part of the text view is clickable
You should probably separate the text into 2 text views one with the terms and condition and one with just the accept.It would make things cleaner and easier. The following TextView is assuming its just for accept.
In the layouts corresponding java class(example: activity_main -> MainActivity):
public void start_activity(View view){
Intent newActivityIntent = new Intent(this,NewActivity.class);
startActivity(newActivityIntent);
}
NewActivity.class is just the name of the activity you want to start.
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
....
}
public void start_activity(View view){
.....
}
}
I needed TextView which would flash on text change - so I made CustomTextView like described in here. It works great but I have problem when I set that CustomTextView in list item. Because of ListView items are being reused my CustomTextView keeps flashing when scrolling up/down because it's context changes and now it 'points' to another list item.
Problem is I don't know how to determine when context of the item is changed, so I cannot put noFlash flag (Text property of the CustomTextView is not set to null, so I cannot use that either)
On the end I had to make custom adapter with some code in BindBindbleCode
protected override void BindBindableView(object source, IMvxListItemView viewToUse)
{
var newValue = source as ListItemVM;
var oldValue = viewToUse.DataContext as ListItemVM;
if (newValue.ItemID != oldValue.ItemID)
newValue.Rebinded = true;
base.BindBindableView(source, viewToUse);
}
then I added NotLoaded property on CustomTextView and binded those two together. So when Rebinded is true it sets NotLoaded to true signalizing that data in that TextView is not loaded. If data is not loaded there's no need to flash background of CustomTextView.
public string AnimatingText
{
get { return Text; }
set
{
if (Text == value)
return;
if (NotLoaded)
{
Text = value;
NotLoaded = false;
return;
}
Text = value;
// Do your animation here
}
}
i am trying to expand textview in android, i have textview with some bulk content, my requirement is to display only two lines from bulk content with finishing (...). if i click it, my full content will has to display. how to
achieve this. could you please help me?
public class MainActivity extends ActionBarActivity {
TextView t1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1 = (TextView) findViewById(R.id.text);
String str = "If your view subclass is displaying its own Drawable objects, it should override this function and return true for any Drawable it is displaying. This allows animations for those drawables to be scheduled." +
"I made the expandable list view acc to your tutorial but what i have to do if I want to populate Expandable List View with values from database?Means what changes I have to do in ";
t1.setText(str);
t1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t1.setMaxLines(Integer.MAX_VALUE);
}
});
}
}
You can set android:maxLines property of textview to 2 by default in your layout xml..
In your onCreate() function after setting the text to your textview (t1).
Add following
boolean isTextViewClicked = false;
t1.setOnClickListener(new OnClickListener() {
if(isTextViewClicked){
//This will shrink textview to 2 lines if it is expanded.
t1.setmaxLines(2);
isTextViewClicked = false;
} else {
//This will expand the textview if it is of 2 lines
t1.setMaxLines(Integer.MAX_VALUE);
isTextViewClicked = true;
}
});
This will expand the textview as per the content
I describe the best and optimize approach in a medium article.
What you need to do is running the following function in a TextView.post{} function:
private fun TextView.setCaption(senderName: String, caption: String) {
text = getFullCaption(senderName, caption)
if (lineCount > DEFAULT_LINES) {
val lastCharShown = layout.getLineVisibleEnd(DEFAULT_LINES - 1)
maxLines = DEFAULT_LINES
val moreString = context.getString(R.string.read_more)
val suffix = " $moreString"
// 3 is a "magic number" but it's just basically the length of the ellipsis we're going to insert
val actionDisplayText = context.getString(R.string.more_dots) + suffix
text = SpannableStringBuilder()
.bold { append(senderName) }
.append(" ")
.append(
caption.substring(
0,
lastCharShown - suffix.length - 3 - moreString.length - senderName.length
)
)
.color(Color.GRAY) { append(actionDisplayText) }
}
}
Calling setCapton function in an extensive function of TextView and set click listener for it to expand, is your final step to have an expandable TextView:
private fun TextView.setExpandableText(senderName: String, caption: String) {
post {
setCaption(senderName, caption)
setOnClickListener {
let {
it.maxLines = MAX_LINES
it.text = getFullCaption(senderName, caption)
}
}
}
}
I have added links to text that is surrounded by [square brackets] in a popup dialog box. The links however, are not clickable (nothing happens when they are pressed). I can't work out why(!)
Here is my dialog box activity:
public void popupDefinition(CharSequence term, LinkedHashMap<String, String> dictionaryMap){
SpannableString definition = new SpannableString(dictionaryMap.get(term)); // grab the definition by checking against the dictionary map hash
Linkify.addLinks(definition, pattern, scheme); // find text in square brackets, add links
AlertDialog alertDialog = new AlertDialog.Builder(ListProjectActivity.this).create(); // create a dialog box
alertDialog.setMessage(definitionFormatted); // set dialog box message
alertDialog.show(); // actually display the dialog box
}
'scheme' and 'pattern' are defined earlier, as follows:
final static Pattern pattern = Pattern.compile("\\[[^]]*]"); // defines the fact that links are bound by [square brackets]
final String scheme = "http://example.com/"; // THIS IS NOT WORKING
Why, when I tap the links that appear (they appear nicely underlined in blue), do they not cause any response?
I'm not actually trying to launch URL links (I'll be redirecting the ACTION_VIEW intent when it does occur), but I need to confirm that some sort of response is happening before I get to that...
I'm not actually trying to launch URL links
Since you don't need to use URLs, don't bother with trying to create a custom Linkify scheme since it only creates URLSpans. You simply want to start an Activity from a keyword in a TextView. As I stated in one of your similar, but not duplicate, questions I would use a custom span, introducing ActivitySpan:
public class ActivitySpan extends ClickableSpan {
String keyword;
public ActivitySpan(String keyword) {
super();
this.keyword = keyword;
}
#Override
public void onClick(View v) {
Context context = v.getContext();
Intent intent = new Intent(context, AnotherActivity.class);
intent.putExtra("keyword", keyword);
context.startActivity(intent);
}
}
There are no bells or whistles here, this span takes a [keyword] that you surrounded in brackets and passes it to the another Activity.
While I don't like the idea of using Linkify because of URLSpans, its pattern matching and span creation is great, so I copied and modified it:
private void addLinks(TextView textView, Pattern pattern) {
SpannableString spannable = SpannableString.valueOf(textView.getText());
Matcher matcher = pattern.matcher(spannable);
// Create ActivitySpans for each match
while (matcher.find())
spannable.setSpan(new ActivitySpan(matcher.group()), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// Set new spans in TextView
textView.setText(spannable);
// Listen for spannable clicks, if not already
MovementMethod m = textView.getMovementMethod();
if ((m == null) || !(m instanceof LinkMovementMethod)) {
if (textView.getLinksClickable()) {
textView.setMovementMethod(LinkMovementMethod.getInstance());
}
}
}
As a note, this method doesn't remove the [brackets] surrounding each keyword, but you can easily do this in the while-loop.
To use this, simply pass a TextView and Pattern to addLinks() in onCreate() and Voila!
A working example for you:
public class Example extends Activity {
Pattern pattern = Pattern.compile("\\[[^]]*]");
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
popupDefinition("Example: A [pattern] or [model], as of something to be [imitated] or [avoided]");
}
// It seems like you can call "popupDefinition(dictionaryMap.get(term));" rather than pass both.
public void popupDefinition(String string){
SpannableString spannable = new SpannableString(string);
Matcher matcher = pattern.matcher(spannable);
// Create ActivitySpans for each match
while (matcher.find())
spannable.setSpan(new ActivitySpan(matcher.group()), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// Create a new TextView with these spans and enable the clickable links
TextView textView = new TextView(this);
textView.setText(spannable);
textView.setMovementMethod(LinkMovementMethod.getInstance());
// Create and display an AlertDialog with this TextView
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setView(textView)
.create();
alertDialog.show();
}
public class ActivitySpan extends ClickableSpan {
String keyword;
public ActivitySpan(String keyword) {
super();
this.keyword = keyword;
}
#Override
public void onClick(View v) {
Context context = v.getContext();
Toast.makeText(context, keyword, Toast.LENGTH_SHORT).show();
}
}
}
I have an application where I need to display a list of numbers, but the numbers need to be formatted based on their value. Negative numbers are shown in normal text, positive numbers shown as bold. Also, the number needs to always appear positive in the text view. I tried extending TextView with setText overriden as such:
#Override
public void setText(CharSequence text, TextView.BufferType type) {
double number = Double.parseDouble(text.toString());
if (number > 0) {
this.setTypeface(this.getTypeface(), BOLD);
} else {
this.setTypeface(this.getTypeface(), NORMAL);
number = Math.abs(number);
}
super.setText(number + "", type);
}
This didn't quite work, as the setText was being called multiple times on the same MyTextView. This resulted in every number appearing bold, as it was positive the next time through.
I would like to keep this logic in a widget, as opposed to where the text is being set, as this is a very common occurrence in my application.
Is there a way that I can do this in a widget?
Just add a member variable to your class to check whether it was already modified or to keep the original value.
private double originalValue = 0;
#Override
public void setText(CharSequence text, TextView.BufferType type) {
if(originalValue==0) {
originalValue = Double.parseDouble(text.toString());
}
this.setTypeface(this.getTypeface(), originalValue>0 ? BOLD : NORMAL);
super.setText(Math.abs(originalValue), type);
}
Ok, I ended up just making an adapter for each list that used this special case, and took care of it in the activity for any other instance of it. Something like this:
#Override
public void bindView(View view, Context context, Cursor cursor) {
TextView text = (TextView) view.findViewById(R.id.special_text);
double amount = cursor.getDouble(cursor.getColumnIndex(DbAdapter.KEY_NUMBER));
if (amount > 0) {
amountText.setTypeface(null, Typeface.BOLD);
} else {
amountText.setTypeface(null, Typeface.NORMAL);
amount = Math.abs(amount);
}
text.setText(amount);
}