Spannable Text does not work in dialog snippet

I have added a TextView to my Dialog snippet and I am showing Spannable data Dynamically in this TextView . But this does not affect the Spannable text.

Here is my code for creating Three Spannable String and added to 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 result:

enter image description here

Please give me a hint. Why doesn't it work in Dialog Fragment ?

+6
source share
1 answer

To achieve this, you must specify the type of Span . You can set it in the fourth parameter of the setSpan() method. In your case, the value should be SPAN_EXCLUSIVE_EXCLUSIVE :

Spaces of type SPAN_EXCLUSIVE_EXCLUSIVE are not expanded to include text inserted at their start or end point. They can never have a length of 0 and are automatically removed from the buffer if all the text that they cover is deleted.

Like this:

 VehicleCaptureSpan.setSpan( new ForegroundColorSpan(Color.RED), 0, VehicleCapture.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE ); 

But if you want to set the color or style of the text of the whole text, it’s easier to set them with the TextView options:

 textViewTicketSummary.setTextColor(Color.RED); textViewTicketSummary.setTextSize(TypedValue.COMPLEX_UNIT_SP, size); textViewTicketSummary.setTypeface(Typeface.DEFAULT_BOLD); 

UPD: There is a little funny bug in your code. You are trying to add String instead of SpannableString values ​​in the constructor. Just change this code to this:

 summary.append(VehicleCaptureSpan).append("\n\n") .append(TotalVehicleSpan).append("\n\n") .append(DurationTimeSpan).append("\n"); 
+4
source

Source: https://habr.com/ru/post/978623/


All Articles