Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I build up a display using an EditText with different colors for command and response strings?
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>The Java code I am using is:</p> <pre><code>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) &amp;&amp; (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; } } </code></pre> <p>Working code with Html.fromHtml is:</p> <pre><code>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) &amp;&amp; (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("&lt;font color=\"blue\"&gt;" + cs + "&lt;/font&gt;")); traffic = traffic.append('\n'); traffic = traffic.append(Html.fromHtml("&lt;font color=\"red\"&gt;" + cmdString + "&lt;/font&gt;")); traffic.append('\n'); ((TextView) traffic_panel).setText(traffic); cmdLine.setText(""); } return false; } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload