Search code examples
androidlayoutoutline

Android: how to configure the textView outline black border in xml layout properly


How can I add a black and solid outline border to a textView. I tried this code:

 <TextView
    android:id="@+id/faqTitle"
    style="@style/viewParent.headerText.HomeCardTitle"
    android:layout_alignParentEnd="true"
    android:layout_centerVertical="true"
    android:shadowColor="@color/colorRed"
    android:shadowRadius="15"
    android:paddingBottom="@dimen/padding"
    android:paddingStart="@dimen/padding"
    android:paddingEnd="@dimen/padding"
    android:textColor="@color/colorWhite"
    android:text="TEST" />

The problem is that after updating showRadius, the outline is not solid. I don't want to use an external library. Only uses of XML attributes. Please take a look at the attached images.

Expected

Actual


Solution

  • For outline to characters you can use below code

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/test1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:text="TEST"
            android:textColor="#000000"
            android:textSize="25sp" />
        <TextView
            android:id="@+id/test2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:text="TEST"
            android:textColor="#FFC107"
            android:textSize="25sp" />
    </FrameLayout>
    

    In .java file put these lines to give border to TextView characters

    TextView textViewShadow = (TextView) view.findViewById(R.id.test1);
    textViewShadow.getPaint().setStrokeWidth(5);
    textViewShadow.getPaint().setStyle(Paint.Style.STROKE);