Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8988481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:01:05+00:00 2026-06-15T22:01:05+00:00

I am trying to implement a hypertext-like application so I can use a tablet

  • 0

I am trying to implement a hypertext-like application so I can use a tablet via USB to talk to a PIC system. My concept is to have a fragment showing two EditText views, one to allow a command to be composed and edited on the tablet, the other to hold all the traffic in both directions.

I have got as far as getting the composed string sorted and added to the traffic view. However, I would like to be able to show the tablet-generated strings in a different color to that used for the PIC-generated replies. I have tried numerous combinations and permutations of setSpan but with no luck – any ideas?
I am also open to other ways of achieving the same end.

The Java code I am using is:

package durdle.bruce.fragment_trial;

import android.app.Fragment;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

public class PanelFragment extends Fragment implements OnKeyListener
{
    private final String TAG = "Panel Fragment:- ";
    EditText cmdLine;
    EditText traffic_panel;
    int lineCount = 0;
    CharSequence cs;
    CharSequence nextLine;
    SpannableStringBuilder cmdString;
    SpannableStringBuilder traffic;
    SpannableStringBuilder ss;
    ForegroundColorSpan cmdTextColor;
    ForegroundColorSpan trafTextColor;
    String cntString;
    char newLine = '\n';

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) 
    {
        View view = inflater.inflate(R.layout.textfragment,container, false);
        cmdLine = (EditText) view.findViewById(R.id.enterCmd);
        cmdLine.setOnKeyListener(this);
        cmdTextColor = new ForegroundColorSpan(0x0000FF);
        trafTextColor = new ForegroundColorSpan(0xFF0000);
        traffic = new SpannableStringBuilder();
        return view;
    }  

    public boolean onKey(View v, int keyCode, KeyEvent event) 
    {
        if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
            (keyCode == KeyEvent.KEYCODE_ENTER))
        {
            traffic_panel = (EditText) getView().findViewById(R.id.detailsText);

            cmdString = (SpannableStringBuilder) cmdLine.getText();
            Log.d(TAG,"ENTER pressed " + cmdString);

            cs = String.valueOf(lineCount) + "-" + '\n';
            ss = new SpannableStringBuilder(cs);
            ss.setSpan(trafTextColor,0,ss.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            traffic = traffic.append(ss);

            cmdString.setSpan(cmdTextColor,0,cmdString.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            nextLine = cmdString.append(newLine);
            traffic = traffic.append(nextLine);

            Log.d(TAG, "traffic string = " + traffic);
            ((TextView) traffic_panel).setText(traffic);
            cmdLine.setText("");
            lineCount++;
        }
        return false;
    }
} 

Working code with Html.fromHtml is:

package durdle.bruce.fragment_trial;

import android.app.Fragment;
import android.os.Bundle;
import android.text.Html;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

public class PanelFragment extends Fragment implements OnKeyListener
{
private final String TAG = "Panel Fragment:- ";
EditText cmdLine;
TextView traffic_panel;
int lineCount = 0;
int oldTrafficLength;
int newTrafficLength;
CharSequence cs;
CharSequence nextLine;
SpannableStringBuilder cmdString;
SpannableStringBuilder traffic;
SpannableStringBuilder ss;
ForegroundColorSpan cmdTextColor;
ForegroundColorSpan trafTextColor;
String cntString;
char newLine = '\n';

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
  Bundle savedInstanceState) 
{
    View view = inflater.inflate(R.layout.textfragment,container, false);
    cmdLine = (EditText) view.findViewById(R.id.enterCmd);
    cmdLine.setOnKeyListener(this);
    cmdTextColor = new ForegroundColorSpan(0xFF0000);
    trafTextColor = new ForegroundColorSpan(0x0000FF);
    traffic = new SpannableStringBuilder();
    return view;
}  

public boolean onKey(View v, int keyCode, KeyEvent event) 
{
    if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
            (keyCode == KeyEvent.KEYCODE_ENTER))
    {
        traffic_panel = (TextView) getView().findViewById(R.id.detailsText);

        cmdString = (SpannableStringBuilder) cmdLine.getText();
        Log.d(TAG,"ENTER pressed " + cmdString);
        cmdString = cmdString.append(newLine);

        lineCount++;
        cs = String.valueOf(lineCount) + "-" + '\n';
        traffic = traffic.append(Html.fromHtml("<font color=\"blue\">" + cs + "</font>"));
        traffic = traffic.append('\n');

        traffic = traffic.append(Html.fromHtml("<font color=\"red\">" + cmdString + "</font>"));
        traffic.append('\n');

        ((TextView) traffic_panel).setText(traffic);
        cmdLine.setText("");
    }
    return false;
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T22:01:05+00:00Added an answer on June 15, 2026 at 10:01 pm

    You can use HTML font markup and use Html.fromHtml.

    just do:

    TextView tv;
    tv.setText(Html.fromHtml("<font color=\"red\">" + string + "</font>"));
    

    Here is a link with the tags supported by Html.fromHtml

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to implement what I thought was a simple concept. I have a user
Trying to implement a simple notification system based on private pub ( something like
I'm trying implement a custom login page to use in my JSF 2.0 application.
Trying to implement the following behavior on an iPad. I have a map-centric application
Trying to implement a rating system of users and postings. What is the best
Trying to implement an autocomplete based on this It looks like it's very straight
Trying to implement the following structure from c to use NSArray in objective-c: In
im trying to implement a nice gui for organizing posts. i want to use
When trying to implement a simple OpenGL application, I was surprised that while it
I'm new in mule ESB and I'm trying implement a simple application flow: <mule

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.