Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] java – get string length from resources before variable inserted

Posted on November 14, 2022

Solution 1 :

Instead of doing this, You can use bold tags in your string and display it in HTML form. See the code below

String text = itemView.getContext().getResources().getString( R.string.ActivityIShared_with, sharedTo.get( getAdapterPosition));

SpannableStringBuilder styledString;

if(SDK_INT > N) styledString = (SpannableStringBuilder) Html.fromHtml(,Html.FROM_HTML_MODE_COMPACT) else styledString = (SpannableStringBuilder) Html.fromHtml(result)

tv_Sharewith.setText(styledString.toString());

Solution 2 :

From what I can understand you are basically trying to concate string with some style. You can try this straight forward to solve your problem with little modification

Add this method

public static SpannedString buildString(String boldText, String regularText) {
    final SpannableStringBuilder builder = new SpannableStringBuilder();
    int start = builder.length();
    builder.append(boldText);
    builder.setSpan(new StyleSpan(BOLD), start, builder.length(), SPAN_INCLUSIVE_EXCLUSIVE);
    builder.append(regularText);
    return new SpannedString(builder);
}

and change

<string name="ActivityIShared_with">Shared with: %1$s</string>

to

<string name="ActivityIShared_with">Shared with:</string>

and use the method

buildString(context.getString(R.string.ActivityIShared_with), sharedTo.get(getAdapterPosition()));

This way you don’t have to keep count of text length referring ActivityIShared_with

Problem :

In one of my TextViews I had like to set some of the text in a bold font like this:

enter image description here

In order do so I’m using the following:

final SpannableStringBuilder sb = new SpannableStringBuilder(itemView.getContext().getResources().getString( R.string.ActivityIShared_with, sharedTo.get( getAdapterPosition() ) ));
final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD);
sb.setSpan(bss, 0, 11, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
tv_SharedWith.setText(sb);

Where

<string name="ActivityIShared_with">Shared with: %1$s</string>

As you can see this code works. However, I needed to count the length of the R.string.ActivityISharedwith manually until the part of %1$s which is 11 in order to make it bold.

Maybe in a few days, I will change that sentence and I would like to avoid counting its length every time.

How can I get the length of it but without the part of sharedTo.get( getAdapterPosition() ) ? the string without the variable.

Thank you

Comments

Comment posted by Marco

did you try regex? ` String mydata = “String with: 1s$s”; Pattern pattern = Pattern.compile(“^[A-Za-z, ]+:”); Matcher matcher = pattern.matcher(mydata);` Something like that you neet to make pattern as static variable because that consume a lot each compilation

READ  [ANSWERED] flutter - Dupplicate class com.google.android.gms.location.places.zza
Powered by Inline Related Posts

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress