Issue
I am having an issue of placing a imagebutton/imageview between a multiline textview in Android.
Eg.:
The text is dynamic. I am having many views like this in my application(nearly 50) with different texts and I want to achieve this with re-usability of code. So kindly suggest me some dynamic solution. On clicking the imagebutton/imageview, it should redirect to calling screen.
I am open to ideas of performing this with the help of WebViews unless and until they handle the dynamicity of text throughout the application(I don't want to make Html file for each and every view).
Looking for the sincere co-operation. Thank You.
Solution
Try this
SpannableString string1=new SpannableString(yourdynamictext);
//Get Drawable
Drawable d=getResources().getDrawable(R.drawable.ic_launcher);
d.setBounds(0,0,d.getIntrinsicWidth(),d.getIntrinsicHeight());
// image span
ImageSpan span2= new ImageSpan(d,ImageSpan.ALIGN_BASELINE);
string1.setSpan(span2, 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
yourtextview.setText(string1);
Update:
set this for your textview
yourtexview.setMovementMethod(LinkMovementMethod.getInstance());
Use ClickableSpan
Here is the example
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View textView) {
Toast.makeText(MainActivity.this, "Hello",Toast.LENGTH_LONG).show();
}
};
string1.setSpan(clickableSpan , 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
yourtextview.setText(string1);
Answered By - Pragnani
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.