I have a problem with striking through a part of text that is displayed in the button.
I'm trying to achieve the effect using SpannableString and StrikethroughSpan. I have the following method:
public static SpannableString strikethrough(SpannableString text) {
StrikethroughSpan span = new StrikethroughSpan();
text.setSpan(span, 0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return text;
}
Then I do something like this
SpannableString strikethoughPart = new SpannableString("striked_part");
TextStyleHelper.strikethrough(strikethoughPart);
final CharSequence concatted = TextUtils.concat("not striked ", strikethoughPart);
justButton.setText(concatted);
justText.setText(concatted);
The TextView is displayed correctly, with one part seen as striked. However, the button is displayed not correctly, there is no strikethrough effect (the same on all apis from 21 to 25)
api 21 image.
API 26+ works as expected api 26 image.
How can I strikethrough only part of the button text on lower apis (21-25)? I can achieve the effect of striking through with setPaintFlags method but that will strike through all text and I don't need that.
Tried classes of button: usual Button and MaterialButton.
Related
I have seen all around stackoverflow to use
setText(html.fromHtml("<font color=\"#c5c5c5\">Hello</font>"))
However none of these work for android < 23
here is a sample code:
TextView mTextView = new TextView(context);
mTextView.setText(Html.fromHtml("<p style=\"color:red;\">Test</p>"));
This sets font color in > 23. Is there any alternatives that work for < 23? or is this a bug with the following dependency?
implementation 'androidx.appcompat:appcompat:1.1.0'
Some more info: I am testing on Lolipop x86 image
No need for HTML, just use spans.
Spannable text = new SpannableString("hello");
text.setSpan(new ForegroundColorSpan(Color.RED), 0, text.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
mTextView.setText(text);
If you're using appcompat, you probably want to new AppCompatTextView(context) rather than just new TextView(context).
use following
setText(Html.fromHtml("<![CDATA[<font color='#c5c5c5'>Hello</font>]]>"));
for xml layout remember to add
android:textAllCaps="true"
Excuse me, I have a question how can l write the code when i click on image button send it on edit view with the same pic inside the button
like the emojicon chat
note :
the same idea of emojicons but with my special icons like this picture
like this picture :
Are you looking for Spannable's? They can be used to insert images into a field that contains plain text in Android. You can view the Google documentation for Spannable's here.
I wrote a code example below that might give you a head start:
public CharSequence addImageSpan(CharSequence text){
SpannableStringBuilder builder=new SpannableStringBuilder(text);
int resId = R.drawable.emoticonImgResource;
builder.setSpan(new ImageSpan(mContext,resId),
text.start(), text.length()-1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return builder;
}
I have a url:
https://<site name>/pallavi/[Songs.PK]%2002%20.mp3
I have a text view, with property: android:autoLink="all"
If I simply set the text to the text view, my text view simply highlights the portion preceding the [. It looks something like this:
https://< site name >/pallavi/[Songs.PK]%2002%20.mp3
What i want is, the whole link should be highlighted like:
https://< site name >/pallavi/[Songs.PK]%2002%20.mp3
What I have tried till now:
Used the < pre > tag and Html.fromHtml, but it doesn't seem to work! (I don't even know if the < pre > is supported in android though.)
Used Jsoup.parser. But that too doesn't seem to work for me.
UPDATE
I have tried this answer too: https://stackoverflow.com/a/12376115/1320263
Please let me know if the issue is with android that the text view's linkAll property itself does not consider parenthesis as a valid character or not? If it is supported, how do i hyperlink that too?
Also NOTE:
The text(or link) I have written in the question is just a sample text. In reality, I am getting a block of text, from where it would be very difficult to identify where exactly the hyper link starts and where it ends. Also, the number of links present in the block would be un-known. Hence I cannot use the < a href = "" > thing...
If some one else happens to have the same issue, following is the solution which worked for me:
Pattern pattern = Pattern.compile("(?i)\\b((?:[a-z][\\w-]+:(?:/{1,3}|[a-z0-9%])|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\".,<>?«»“”‘’]))");
SpannableString spannable = new SpannableString(html);
Matcher matcher = pattern.matcher(spannable);
// Create ActivitySpans for each match
while (matcher.find())
spannable.setSpan(new ActivitySpan(matcher.group()), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// Create a new TextView with these spans and enable the clickable links
mTxtEventDescription.setText(spannable);
You can try this:
((TextView) findViewById(R.id.your_text_view)).setMovementMethod(LinkMovementMethod.getInstance());
((TextView) findViewById(R.id.your_text_view)).setText(Html.fromHtml(getResources().getString(R.string.string_with_links)));`
I wanted to try out this funny title bar coloring, but it doesn't work for me as
getWindow().findViewById(android.R.id.title);
returns null. So I had a look at it with Hierarchy Viewer and found out that the view is called id/action_bar instead. But there's no R.id.action_bar (autocomplete doesn't offer it and there's nothing like this is R.java).
So now I'm doubly confused:
Is android.R.id.title sort of obsolete now (I'm using version 16 in my emulator)?
Where does id/action_bar come from?
What's the recommended and simple practice w.r.t. compatibility?
Should I get ActionBarSherlock? I originally just wanted to change the title bar color... not fool around with it a lot.
Use the code below to get the ActionBar's id:
val actionBarId = resources.getIdentifier("action_bar", "id", packageName)
And use findViewById you can find the action bar.
Then find the title from actionbar's children (in normal cases):
val actionbar = findViewById<ViewGroup>(actionBarId)
for (view in actionbar.children) {
if (view is TextView) {
// this is the titleView
}
}
However, if you just want to change the title view's text, just use getSupportActionBar:
supportActionBar?.apply {
// set title text
title = "Hello"
// set colored title text
val coloredTitle = SpannableString("Hello")
coloredTitle.setSpan(ForegroundColorSpan(Color.RED), 0, coloredTitle.length, 0)
title = coloredTitle
}
I would recommend using ActionBarSherlock if you're looking for compatibility with Android versions before API level 14 / Android 4.0.
Changing the background of the ActionBar is straightforward and is most easily done via styles. See the "Background" section of http://android-developers.blogspot.com/2011/04/customizing-action-bar.html
You can also change it via code. Put this in your onCreate():
GradientDrawable gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[] {Color.RED, Color.GREEN});
getActionBar().setBackgroundDrawable(gradientDrawable);
Here is a screenshot of this code in action:
I want to have a portion of my TextView invisible. In detail, I present a math problem to the user:
9 * 9 = 81
and I want 81 to be invisible. As user types correct answer, I make digit "8" and then "1" visible.
I tried to use TextAppearanceSpan and ForegroundColorSpan with transparent color (both #00000000 and Color.TRANSPARENT) and they work perfectly for every device and emulator except KindleFire device and emulator.
On Kindle Fire, "81" appears colored in sort-of-dark-grey. It seems that rasterizer tries to make it invisible against some "sort-of-dark-grey" background, not against background (which looks like grid paper image) that I've set in the root LinearLayout of my Activity.
My question is: how can I make a portion of text in TextView invisible using neither TextAppearanceSpan nor ForegroundColorSpan? Does there exist another "span" that measures text correctly, but does not paint it?
Update
I've discovered that there exist some mysterous RasterizerSpan and MetricAffectingSpan, which could (judging by the title) help.
As I can see, the only meaning of RasterizerSpan is to set a Rasterizer to the TextPaint object:
public void updateDrawState(TextPaint ds) {
ds.setRasterizer(mRasterizer);
}
I found it promising: if I were able to set a kind of "void" rasterizer (that just does nothing), it would make this text invisible. Unfortunately
new RasterizerSpan(null); // (set mRasterizer to null)
did not help.
My another idea is to implement a custom ShaderSpan that would set a "void" shader:
public void updateDrawState(TextPaint ds) {
ds.setShader(myCustomVoidShader);
}
but I do not understand how to create a shader that produces "nothing".
See if overriding updateDrawState() on your ForegroundColorSpan and calling setXferMode() will work:
public class TransparentSpan extends ForegroundColorSpan {
private final PorterDuffXfermode MODE = new PorterDuffXfermode(PorterDuff.Mode.DST);
public TransparentSpan(int color) {
super(color);
}
#Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setXfermode(MODE);
}
}
Sorry, I didn't have a Kindle Fire to test with :)
You can use AbsoluteSizeSpan, set the size to 0. So it will invisible
span = new AbsoluteSizeSpan(0, true);
spannableString.setSpan(span, wordPosition.start, wordPosition.end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Notice:
The next word of the invisible word will take place the invisible word position.
Why not using the android visibility (true or false). It will do the same job you need (or there is a specificreason you would lie to use Span) ?