Recently a spannable string i was using in my application, stopped being displayed, on some devices.
It was working in the past, but then i'm not sure when it stopped working (probably after recently updated the sdk).
Anyway what is happening is that the textView with the spannable is correctly displayed on some devices (i.e. tablet, or big screen devices, not all anyway, i.e. a one plus one is having this problem), but on other devices is not displayed at all (looks more not updated, since i tried to put a placeholder string, and this string is displayed even if i'm sure the setText method is being called).
So i'm not sure what can be the problem. This is the textView i'm using:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="40dp"
android:gravity="center"
android:id="@+id/edition_name"
android:shadowDx="3"
android:shadowDy="3"
android:shadowRadius="1.5"
android:shadowColor="@color/transparentGrey"
android:layout_below="@+id/space"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
/>
And this is how i set the spannableString:
private void updateText(TextView textView, String title, String value){
SpannableString styledString;
if(title != null) {
styledString = new SpannableString(title + ": " + value);
styledString.setSpan(new StyleSpan(Typeface.BOLD), title.length() + 2, styledString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} else {
styledString = new SpannableString(value);
styledString.setSpan(new StyleSpan(Typeface.BOLD), 0, styledString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
Log.i(TAG, "Spannable String: " + styledString.toString());
textView.setText(styledString);
textView.invalidate();
}
I'm in the else case. Anyway all the other spannable strings composed in the if part are working properly.
The spannable string i've checked is not empty. And when in landscape mode it display the text. I thought tha can be related to a sizing issue, so i tried to make the string smalle, but without success.
Any idea of why it is happening?
I checked this question: How come my spannable isn't shown? Not sure if the case in the answer about the height applyo to my case. At the moment the height set for textView is wrap-content.
--UPDATE
Another interesting thing i found is that if i rotate the screen and it goes in landscape mode, the string appears (it use another layout file), and when i rotate it back to vertical position the string appears (not aligned anyway).
Second Update
Ok this is interesting, i tried to comment the use of spannable string, and use just the text as it is, i'm facing the same problem. Probably is wort showing the onActivityCreated method where i'm populating the components:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if(savedInstanceState != null){
edition = (Edition) savedInstanceState.getSerializable(EDITION_DETAIL);
}
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
boolean useAppStorage = preferences.getBoolean("network_symbols", false);
String imagePath;
Activity currentActivity = getActivity();
TextView editionCodeView = (TextView) currentActivity.findViewById(R.id.edition_code);
TextView editionLaunchView = (TextView) currentActivity.findViewById(R.id.edition_launch);
TextView editionNameView = (TextView) currentActivity.findViewById(R.id.edition_name);
TextView editionSizeView = (TextView) currentActivity.findViewById(R.id.edition_size);
ImageView editionImageView = (ImageView) currentActivity.findViewById(R.id.edition_detail_image);
TextView otherAttributes = (TextView) currentActivity.findViewById(R.id.otherAttributes);
TextView cardsComposition = (TextView) currentActivity.findViewById(R.id.cards_composition);
Button gathererButton = (Button) currentActivity.findViewById(R.id.gatherer_button);
gathererButton.setOnClickListener(this);
if(edition != null) {
updateText(editionCodeView, currentActivity.getString(R.string.edition_code), edition.getCode());
showLaunchText(editionLaunchView, currentActivity, edition.getLaunchYear(), edition.getLaunchMonth());
//updateText(editionNameView, null, edition.getName());
if(edition.getTotalCards() != 0) {
updateText(editionSizeView, currentActivity.getString(R.string.total_cards), String.valueOf(edition.getTotalCards()));
}
updateText(cardsComposition, edition);
String extraProperties = buildExtraPropertiesString();
if(extraProperties != null && !extraProperties.equals("")){
updateText(otherAttributes, currentActivity.getString(R.string.other_attributes_label), extraProperties);
}
if(edition.getImage() == null) {
editionImageView.setImageResource(R.drawable.ic_block_black_48dp);
} else {
if(!useAppStorage){
imagePath = SYMBOL_FOLDER + edition.getImage();
} else {
imagePath = getActivity().getFilesDir() + "/" + SYMBOL_FOLDER + edition.getImage();
}
Drawable symbolDrawable = ImageUtils.loadImage(imagePath, getActivity().getAssets(), getResources(), SCALE_SIZE, useAppStorage);
if(symbolDrawable != null){
editionImageView.setImageDrawable(symbolDrawable);
} else {
editionImageView.setImageResource(R.drawable.ic_block_black_48dp);
}
}
editionNameView.setText(edition.getName());
}
}
Third update
This is very odd, i tried to use directly the string as it is, recreate a new textview from scratch, and for some reason this is the only one TextView that is not being updated.
There should be a reason.
And another odd thing, is that even if i try to set to it a costant string it doesn't display it.
So this is what i think was the problem In order to fix it i have to delete the item, and change the id of the item.
But digging in other layouts, probably the problem was that since i was using findViewById from the activity instead of the view, there was another item called edition_name in another view, so i suppose that the activity returned that reference instead of the one related to the current view.
I used the activity, since in my code i needed to access other components, not only related to the view, so i assumed to avoid to sue two different object to use only one to access everything.