How to add drawable image inside textview - android

I need a drawable within the text, just like an emoji, so e.g.
"Please press the button looking like this 👥 and then proceed ..."
but with a custom drawable instead of the emoji in my example. (How) is this possible?

I need a drawable within the text
You can Use ImageSpan
Span that replaces the text it's attached to with a Drawable that can be aligned with the bottom or with the baseline of the surrounding text. The drawable can be constructed from varied sources:
but with a custom drawable instead of the emoji in my example. (How) is this possible?
Try this working example
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView=findViewById(R.id.tv);
Spannable span = new SpannableString("Please press the button looking like this and then proceed ..");
Drawable test = getResources().getDrawable(R.drawable.abc);
test.setBounds(0, 0, 32,32);
ImageSpan imageSpan = new ImageSpan(test, ImageSpan.ALIGN_BASELINE);
span.setSpan(imageSpan, 36, 37, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setText(span);
}
}
RESULT

You can try unicode:
int unicode = 0x1F60A;
And use it like this:
public String getEmojiByUnicode(int unicode){
return new String(Character.toChars(unicode));
}

Related

Android clickable span click activate all TextView not only spannable text

Good afternoon. Could you help me with a clickable span? The fact is that when I assign a clickable substring at the end of an origin String, not only the text becomes clickable, but also the entire TextView area to the right of the substring, which is bad for me (it is necessary that only a substring is clickable. Can you advise me something ? match parent for my textview is required. Unfortunately, programmatically adding " " to the original string is also unacceptable to me. Pastebin link with code: here. I created a video on my Dropbox:
bad work my programm:
dropbox
I want it to work like this
dropbox
My TextView needs the following layout
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.text_view);
SpannableString spanable = new SpannableString("my clickable span");
ClickableSpan clickableSpan = new ClickableSpan() {
#Override
public void onClick(View widget) {
Toast.makeText(MainActivity.this, "click span", Toast.LENGTH_SHORT).show();
}
};
spanable.setSpan(clickableSpan, 13, 17, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(spanable);
textView.setMovementMethod(LinkMovementMethod.getInstance());
}
set all text using length.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.text_view);
String strService = "my clickable span";
int serviceStart = strService.indexOf("my clickable span");
int serviceEnd = serviceStart + "my clickable span".length();
SpannableString spanable = new SpannableString(strService);
ClickableSpan clickableSpan = new ClickableSpan() {
#Override
public void onClick(View widget) {
Toast.makeText(MainActivity.this, "click span", Toast.LENGTH_SHORT).show();
}
};
spanable.setSpan(clickableSpan, serviceStart, serviceEnd, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(spanable);
textView.setClickable(true); //add clickable
textView.setMovementMethod(LinkMovementMethod.getInstance());
}

How to change a strings color?

I want to change this string to the color red
String isFalse = False;
For some reason every tutorial seemed more complicated than I expected and I don't understand them. Is there a simple way to do this? Also, would this override the color of a textview? Because I would like it to.
String is not View, so it has no color at all.
Maybe what you want is to change the appearance color of it host TextView. To achieve this you can use:
TextView text;
//the initialize of this TextView
text.setText(isFalse);
text.setTextColor(Color.Red);
The parameter of the color could be resource from your color XML values file or android.R.color resource file, or from Color class, etc.
Please use this code.
SpannableStringBuilder builder = new SpannableStringBuilder();
String isFalse = "False";
SpannableString redSpannable= new SpannableString(isFalse);
redSpannable.setSpan(new ForegroundColorSpan(Color.RED), 0, Tru_score.length(), 0);
builder.append(redSpannable);
TextView text1 = (TextView)findViewById(R.id.textView1);
text1.setText(builder, BufferType.SPANNABLE);
It is not complicated or hard
Addressing both possibilities..
You want to change the color for your text at runtime.
that would be like TextView.setTextColor()
i.e.
falseTextView.setTextColor(getResources().getColor(R.color.red))
if you wanted a segment of the same textview to have a different color,by which I mean that the isFalse string is only apart of the content of your TextView ,you need to use as mentioned in the other answer.
Try this...
string.xml
<string name="isFalse"><![CDATA[<b><font color=#FF0000>False</b>]]></string>
MainActivity.java
TextView textView1 = (TextView)findViewById(
R.id.textView1);
textView1.setText(Html.fromHtml(isFalse));
And result, you might get like this...

Android: Make a part of TextView's text NOT-clickable at runtime?

I used Android.text.style.ClickableSpan to make a part (Black) of a string (Blue | Black) clickable:
SpannableString spannableString = new SpannableString("Blue | Black ");
ClickableSpan clickableSpan = new ClickableSpan() {
#Override
public void onClick(View textView) {
//...
}
};
ss.setSpan(clickableSpan, 7, 11, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView textView = (TextView) findViewById(R.id.secondActivity_textView4);
textView.setText(spannableString);
textView.setMovementMethod(LinkMovementMethod.getInstance());
So Black part of the string is clickable. What I want is that when the user clicks Black, it should make Black Not-clickable, and Blue (another part of the same string) clickable.
So to make Blue clickable, we can call setSpan() on the same spannableString another time. But how can I make Black not-clickable?
You can call removeSpan() to remove any previously added Spans. In this particular case it's very easy, as we hold a reference to the very Span we want to remove:
ClickableSpan clickableSpan = new ClickableSpan()
{
#Override
public void onClick(View view)
{
((SpannableString)textView.getText()).removeSpan(this);
}
};
Another option could be to iterate over all ClickableSpan instances and remove them all, such as:
SpannableString str = (SpannableString)textView.getText();
for (ClickableSpan span : str.getSpans(0, str.length(), ClickableSpan.class))
str.removeSpan(span);
For some reason that I cannot fathom, the documentation for spans is really poor... they are quite powerful!

Android listview item with multiple text creation dynamically?

I Want to create mulitple TextView dynamically in ListView item. suppose i use LinearLayout it will create textview horizontal or vertically. I want multiple textview with the wraping. How can i create like that please share your valuable ideas,
Below screen images.
Note :
Each textview have the click action
Mike voted 8 , lara voted 9 like that individual text with wraping conetxt.
I have a custom view (merge xml) that contains a text view (originally it's a more complicated view).
My custom view class like this
public class Example extends LinearLayout {
protected Context context;
protected TextView titleView;
public Example(Context context) {
super(context);
this.context = context;
LayoutInflater inflater = (LayoutInflater) `enter code here`context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.bloghu_title_layout, this, true);
this.setOrientation(LinearLayout.VERTICAL);
titleView = (TextView) getChildAt(0);
}
public void setBlogTitle(String blogTitle, final String blogUrl, String author, final String authorUrl) {
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
spannableStringBuilder.append(blogTitle.toUpperCase());
spannableStringBuilder.append(" / ");
spannableStringBuilder.setSpan(new RelativeSizeSpan(1.5f), 0, blogTitle.length() + 2, 0);
spannableStringBuilder.append(author);
spannableStringBuilder.setSpan(new RelativeSizeSpan(1.2f), spannableStringBuilder.length() - author.length(), spannableStringBuilder.length(), 0);
spannableStringBuilder.setSpan(new NonUnderlineClickableSpan() {
#Override
public void onClick(View widget) {
Log.d("span", blogUrl);
}
}, 0, blogTitle.length(), 0);
spannableStringBuilder.setSpan(new NonUnderlineClickableSpan() {
#Override
public void onClick(View widget) {
Toast.makeText(context, authorUrl, Toast.LENGTH_SHORT).show();
}
}, spannableStringBuilder.length() - author.length(), spannableStringBuilder.length(), 0);
spannableStringBuilder.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.index_orange)), 0, blogTitle.length(), 0);
spannableStringBuilder.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.black)),
spannableStringBuilder.length() - author.length(), spannableStringBuilder.length(), 0);
titleView.setMovementMethod(LinkMovementMethod.getInstance());
titleView.setText(spannableStringBuilder, BufferType.SPANNABLE);
}
}
The NonUnderlineClickableSpan() is an extended ClickAbleSpan(), it just because I don't want to underline the clickable text, end it has an empty onclick method that you have to override:
public class NonUnderlineClickableSpan extends ClickableSpan{
#Override
public void updateDrawState(TextPaint ds) {
ds.setColor(ds.linkColor);
ds.setUnderlineText(false); // set to false to remove underline
}
#Override
public void onClick(View widget) {
// TODO Auto-generated method stub
}
As you can see in Example class you can set a new NonUnderlineClickableSpan, in its' onClick() method you can set what to happen, than you have to set the first and the last character of the clickable span, and a flag (this is the last parameter, in this case 0).
Whit ForegroundSpan you can set font color, whith relative size span you can set different text sizes, and there are a lot of span to style your text and make it interactive, but it is a very under-documented part of android.
I haven't found a good tutorial about this topic yet, so if somebody know one, pls let me know :).
What is the problem, whit textviews in linearLayout? But I think, what you really looking for is spannable string,in this case you can set the formats (colour, font size, style and what ever you want, and onClick actions for every word, and you need just one text view.

Android: Statically underline dynamic text in TextView

I would like to make a TextView to be entirely underlined but I can't use a text resource and <u> tag because it is dynamic text.
Related: Can I underline text in an android layout?
So far the only way I know to do this is at runtime. Is this really the only way? Is there a way I could do it in the XML files?
The easiest solution is probably to create a custom UnderLineTextView component deriving from TextView, override setText() and set the entire text as underlined, something like this (underline code from the link you referred to above):
#Override
public void setText(CharSequence text, BufferType type) {
// code to check text for null omitted
SpannableString content = new SpannableString(text);
content.setSpan(new UnderlineSpan(), 0, text.length(), 0);
super.setText(content, BufferType.SPANNABLE);
}
It's then just a matter of using your new component in the layout and setting the text as usual. The rest is handled automatically.
More info on custom components:
http://developer.android.com/guide/topics/ui/custom-components.html
You can underline text over Html.fromHtml(String source)
Example:
textView.setText(Html.fromHtml("this is <u>underlined</u> text"));
You can also do this via the /res/values/string.xml file if you prefer: For example, in the /res/values/string.xml you could add an entry like:
<string name="createAccount"><u>Create Account</u></string>
And then in the onCreate(Bundle savedInstanceState) method of your activity you would add the following code to cause "Create Account"> to appear as underlined in the UI that you set for the createAccountText TextView that you defined in the xml file in /res/layout/ for your activity:
TextView createAccountText = (TextView) findViewById(R.id.createAccountText);
Resources res = getResources();
CharSequence styledText = res.getText(R.string.createAccount);
createAccountText.setText(styledText, TextView.BufferType.SPANNABLE);
peter3 wrote before to extend TextView class and override setText method.
This solution won't work as setText method is marked as FINAL.
http://developer.android.com/reference/android/widget/TextView.html#setText(java.lang.CharSequence)
Not sure if the code could work with some modifications.
SpannableString content = new SpannableString(name);
content.setSpan(new UnderlineSpan(), 0, name.length(), 0);
geometrical_textview.setText(content, TextView.BufferType.SPANNABLE);

Categories

Resources