Can we create custom TextView in Android

27 Views Asked by At

I just started learning Android Development and wanted to know that is there a way to make TextViews more creative. Like can we apply gradient filter or add shadows to it.

I tried using some background color and other properties.

1

There are 1 best solutions below

0
Aryan On

Here's an example of how to use shadows in TextView:

android:shadowColor="#636363"
android:shadowDx="2"
android:shadowDy="2"
android:shadowRadius="10"
android:background="@drawable/temp"
  • The shadowColor property allows you to change the color of the shadow.
  • The shadowDx property allows you to change the vertical placement of the shadow.
  • The shadowDy property allows you to change the horizental placement of the shadow.
  • The shadowRadius property allows you to increase the amount of area where the shadow is shown.

And as for the background, we first need to create a shape that has the appearance we want and we can apply that shape appearance onto the TextView.

To do so, we first make a new xml file (Drawable Resource File) in the drawable folder which is found under the "res" folder.

You can find more information about android xml shapes online but here's a simple example of a rectangular shape that has a linear gradient background:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <gradient
        android:type="linear"
        android:angle="0"
        android:startColor="#f6ee19"
        android:endColor="#115ede" />
    <corners
        android:radius="6dp"/>
</shape>

You can apply this shape by adding the android:background="@drawable/custom_shape" attribute to your TextView. (You need to rename custom_shape to the name you chose for the xml file)