I have added TextView in my Dialog Fragment and i show Spannable data Dynamically in this TextView. But it's not apply Spannable effect to the text.
Here is my code to generate Three Spannable String and added into TextView.
TextView textViewTicketSummary = (TextView) dialog.findViewById(R.id.ticketSummaryTextView);
SpannableStringBuilder summary = new SpannableStringBuilder();
SpannableString VehicleCaptureSpan, TotalVehicleSpan, DurationTimeSpan;
String VehicleCapture = "Total Vehicles Captured:9";
VehicleCaptureSpan = new SpannableString(VehicleCapture);
VehicleCaptureSpan.setSpan(new StyleSpan(Typeface.BOLD), 0, VehicleCapture.length(), 0);
VehicleCaptureSpan.setSpan(new ForegroundColorSpan(Color.RED), 0, VehicleCapture.length(), 0);
VehicleCaptureSpan.setSpan(new RelativeSizeSpan(1.5f), 0, VehicleCapture.length(), 0);
String TotalVehicle = "Total Car Park Capacity:10 ";
TotalVehicleSpan = new SpannableString(TotalVehicle);
TotalVehicleSpan.setSpan(new StyleSpan(Typeface.BOLD), 0, TotalVehicle.length(), 0);
TotalVehicleSpan.setSpan(new ForegroundColorSpan(Color.RED), 0, TotalVehicle.length(), 0);
TotalVehicleSpan.setSpan(new RelativeSizeSpan(1.5f), 0, TotalVehicle.length(), 0);
String DurationTime = "Total Duration: 0 Min 3 Secs ";
DurationTimeSpan = new SpannableString(DurationTime);
DurationTimeSpan.setSpan(new StyleSpan(Typeface.BOLD), 0, DurationTime.length(), 0);
DurationTimeSpan.setSpan(new ForegroundColorSpan(Color.RED), 0, DurationTime.length(), 0);
DurationTimeSpan.setSpan(new RelativeSizeSpan(1.5f), 0, DurationTime.length(), 0);
summary.append(VehicleCapture).append("\n\n")
.append(TotalVehicle).append("\n\n")
.append(DurationTime).append("\n");
textViewTicketSummary.setText(summary, TextView.BufferType.SPANNABLE);
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@drawable/shape_dialog_patrol"
android:orientation="vertical"
android:paddingBottom="8dp" >
<TextView
android:id="@+id/dialogOneButtonMessage"
style="@style/OneButtonDialogBody"
android:text="Dialog Body" />
<TextView
android:id="@+id/ticketSummaryTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@drawable/shape_textview_rounded_lightblack"
android:lineSpacingExtra="4dp"
android:padding="12dp"
android:text="Ticket Details"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal|top"
android:orientation="horizontal" >
<Button
android:id="@+id/dialogOneButtonText"
style="@style/OneButtonDialogButton2"
android:layout_alignParentRight="true"
android:layout_gravity="center_horizontal"
android:text="@string/ok" />
</RelativeLayout>
</LinearLayout>

Please give me some hint..Why it's not working in Dialog Fragment?
To achieve this you should specify the type of Span. You can set it in fourth parameter of setSpan() method. In your case the value should be SPAN_EXCLUSIVE_EXCLUSIVE:
Spans of type SPAN_EXCLUSIVE_EXCLUSIVE do not expand to include text inserted at either their starting or ending point. They can never have a length of 0 and are automatically removed from the buffer if all the text they cover is removed.
Like this:
VehicleCaptureSpan.setSpan(
new ForegroundColorSpan(Color.RED),
0,
VehicleCapture.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
);
But if you want to set color or text style of whole text it is easier to set them with TextView parameters:
textViewTicketSummary.setTextColor(Color.RED);
textViewTicketSummary.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
textViewTicketSummary.setTypeface(Typeface.DEFAULT_BOLD);
UPD:
There is a small funny bug in your code. You are trying to append Strings instead of SpannableString values to builder. Just change that code to this one:
summary.append(VehicleCaptureSpan).append("\n\n")
.append(TotalVehicleSpan).append("\n\n")
.append(DurationTimeSpan).append("\n");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With