3

I have the following CustomAdapter:

package com.test.testing;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class SetRowsCustomAdapter extends ArrayAdapter<SetRows> {
    Context context;
   int layoutResourceId;
   ArrayList<SetRows> data=new ArrayList<SetRows>();
   DateFormat df = new SimpleDateFormat("EEEEE, LLLL d", Locale.US);
   String[] suspendedDates = {
            "Monday, January 20",
            "Friday, January 31",
    };
   public SetRowsCustomAdapter(Context context, int layoutResourceId, ArrayList<SetRows> data) {
       super(context, layoutResourceId, data);
       this.layoutResourceId = layoutResourceId;
       this.context = context;
       this.data = data;
   }

   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
       View row = convertView;
       ImageHolder holder = null;

       if(row == null)
       {
           LayoutInflater inflater = ((Activity)context).getLayoutInflater();
           row = inflater.inflate(layoutResourceId, parent, false);

           holder = new ImageHolder();
           holder.txtTitle = (TextView)row.findViewById(R.id.tvDateVal);
           //holder.txtTitle.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/robm.ttf"));
           holder.imgIcon = (ImageView)row.findViewById(R.id.ivIcon0);
           holder.txtDate = (TextView)row.findViewById(R.id.tvDateNum);
           holder.txtID = (TextView)row.findViewById(R.id.tvReasonVal);
           //holder.txtID.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/robm.ttf"));
           row.setTag(holder);
       }
       else
       {
           holder = (ImageHolder)row.getTag();
       }

       SetRows myImage = data.get(position);
       int inReason = myImage.name.indexOf(","); //myImage.name is the same string as suspendedDates[];
        String strR = myImage.name.substring(0, inReason);
        Spannable WordToSpan = new SpannableString(strR);
        WordToSpan.setSpan(new ForegroundColorSpan(Color.parseColor("#4787ED")), 0, WordToSpan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        String strRNext = myImage.name.substring(inReason, myImage.name.length());
        Spannable WordToSpan1 = new SpannableString(strRNext);
    WordToSpan1.setSpan(new ForegroundColorSpan(R.color.dateholiday), 0, WordToSpan1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        String strConcat = WordToSpan.toString() + WordToSpan1.toString();

       holder.txtTitle.setText(strConcat);//myImage.name);
       holder.txtID.setText(myImage.id);
       holder.txtDate.setText(myImage.date);
       int outImage=myImage.image;
       /*if (myImage.name.contains(df.format(Calendar.getInstance(Locale.US).getTime()))) {
           holder.imgIcon.setImageResource(R.drawable.caliconpressed);
       }
       else {
           holder.imgIcon.setImageResource(R.drawable.calicon);
       }*/
      return row;

   }

   static class ImageHolder
   {
       ImageView imgIcon;
       TextView txtTitle;
       TextView txtID;
       TextView txtDate;
   }
}

I am using Spannable to set a separate color of a string. It is supposed to produce something like this: enter image description here

But it is still displaying this:

enter image description here

Anyone know how to edit the Adapter to achieve what I am looking to do?

2 Answers 2

3

Do not convert your Spannable to a String. (ie. don't do: WordToSpan.toString())

Instead, set the Spannable directly into your holder, like this:

holder.txtTitle.setText(WordToSpan + WordToSpan1);
Sign up to request clarification or add additional context in comments.

4 Comments

It's giving me an error with the +, The operator + is undefined for the argument type(s) android.text.Spannable, android.text.Spannable
OK, you need to combine the Spannable otherwise (maybe you create a single spannable string, with multiple span elements). The important part of my answer, is to NOT do the toString() conversion.
You can use SpannableStringBuilder (developer.android.com/reference/android/text/…) to create your original SpannableString.
TextUtils.concat(WordToSpan, WordToSpan1) worked :) I upvoted your answer. Thanks!
3

Use spannable string as follows:

SpannableString ss = new SpannableString("hey #abc how are you.");
ss.setSpan(new ForegroundColorSpan(Color.RED), 4, 9, 0);
//Now just add the SpannableString to your textview
textView.setText(ss);

Hope this Helps.

Regards!

3 Comments

I am trying to merge two. What is wrong with mine if I may ask?
You are passing a string, not a spannable object, when you concatenated them here "String strConcat = WordToSpan.toString() + WordToSpan1.toString();", since that's what you are using you lost the "spannable" object which is the one that changes color, so you are setting just a regular concatenated string, if you want to combine them you first concatenate, then add the spannable properties and make sure to pass the spannable on setText method, NOT the String.
TextUtils.concat(WordToSpan, WordToSpan1) worked with my code. I upvoted your answer. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.