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);
Here is my Layout:
<?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>
Here is the Output:

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 ofsetSpan()method. In your case the value should be SPAN_EXCLUSIVE_EXCLUSIVE:Like this:
But if you want to set color or text style of whole text it is easier to set them with
TextViewparameters:UPD: There is a small funny bug in your code. You are trying to append
Strings instead ofSpannableStringvalues to builder. Just change that code to this one: