How to add angular radius and complement to multi-line stretchable text

How can I add a corner radius and an addition to the stretch text below?

public class CustomTextView extends TextView {
        public CustomTextView(Context context) {
            super(context);
            setFont();
        }
        public CustomTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
            setFont();
        }
        public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            setFont();
        }

        private void setFont() {
            Typeface font = Typeface.createFromAsset(getContext().getAssets(), "fonts/TEXT.ttf");
            setTypeface(font, Typeface.NORMAL);

 Spannable myspan = new SpannableString(getText());
    myspan.setSpan(new BackgroundColorSpan(0xFF757593), 0, myString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    txtview.setText(myspan);


        }
    }
+4
source share
2 answers

EDIT

Do you want something like that?

screenshot

If so, you can only create it with the following code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="#999999"
          android:gravity="center">

    <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="20dp"
            android:gravity="center"
            android:background="@drawable/rounded_corners"/>
</LinearLayout>

and here is the rounded_corners.xml file:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="10dp"/>
    <solid android:color="@android:color/white"/>
</shape>

And this is MainActivity:

public class MainActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Typeface font = Typeface.createFromAsset(getContext().getAssets(), "fonts/TEXT.ttf");

        TextView textView = (TextView) findViewById(R.id.textView);

        textView.setTypeface(font, Typeface.NORMAL);

        String text = "Text1\nText2\nText3";
        Spannable myspan = new SpannableString(text);
        myspan.setSpan(new BackgroundColorSpan(0xFF757593), 0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        textView.setText(myspan);
    }
}

I had to use different colors so you can see that these are rounded corners. You can just play around with the color values. And I do not have the TEXT.ttf file, so I can’t know how it will look. The above image, of course, without its own font.

+1
source
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="10dp"/>
    <solid android:color="@android:color/white"/>
</shape>

, . TextView, .

+1

Source: https://habr.com/ru/post/1605707/


All Articles