Advertisement
Guest User

clickable span

a guest
Jun 15th, 2020
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. package com.example.testclickablespan;
  2.  
  3. import android.graphics.Color;
  4. import android.os.Bundle;
  5. import android.text.SpannableString;
  6. import android.text.Spanned;
  7. import android.text.TextPaint;
  8. import android.text.method.LinkMovementMethod;
  9. import android.text.style.ClickableSpan;
  10. import android.view.View;
  11. import android.widget.TextView;
  12. import android.widget.Toast;
  13.  
  14. import androidx.annotation.NonNull;
  15. import androidx.appcompat.app.AppCompatActivity;
  16.  
  17. public class MainActivity extends AppCompatActivity {
  18.  
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_main);
  23.  
  24. TextView textView = findViewById(R.id.text_view);
  25.  
  26. textView.setBackgroundColor(Color.TRANSPARENT);
  27. SpannableString spanable = new SpannableString("my clickable span");
  28.  
  29. ClickableSpan clickableSpan = new ClickableSpan() {
  30. @Override
  31. public void onClick(View widget) {
  32. Toast.makeText(MainActivity.this, "click span", Toast.LENGTH_SHORT).show();
  33. }
  34.  
  35. @Override
  36. public void updateDrawState(@NonNull TextPaint ds) {
  37. ds.setUnderlineText(false);
  38. super.updateDrawState(ds);
  39. ds.setUnderlineText(false);
  40. }
  41. };
  42.  
  43. spanable.setSpan(clickableSpan, 13, 17, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  44.  
  45. textView.setText(spanable);
  46. textView.setMovementMethod(LinkMovementMethod.getInstance());
  47. }
  48. }
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56. <?xml version="1.0" encoding="utf-8"?>
  57. <FrameLayout
  58. xmlns:android="http://schemas.android.com/apk/res/android"
  59. android:layout_width="match_parent"
  60. android:layout_height="match_parent"
  61. android:background="@color/colorPrimary">
  62.  
  63. <TextView
  64. android:id="@+id/text_view"
  65. android:layout_width="match_parent"
  66. android:layout_height="match_parent"
  67. />
  68.  
  69. </FrameLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement