TextView in Android
A TextView is a user interface element that displays text to the user. In this Example we going to see how to implement TextView in Advance Style like..TextView With Shadow,Images,With Multi Colors.
- TextView with Shadow
In Andorid we Can Acheive this with the help of Shadow attribute like shadowColor,shadowDx,shadowDy,shadowRadius.
| Attribute | Description |
Related methods: |
|---|---|---|
| android:shadowColor | Place a blurred shadow of text underneath the text, drawn with the specified color.The text shadow produced does not interact with properties on View that are responsible for real time shadows, elevation and translationZ.
May be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb". |
setShadowLayer(float,float,float,int) |
| android:shadowDx | Horizontal offset of the text shadow.May be a floating point value, such as "1.2". |
setShadowLayer(float,float,float,int) |
| android:shadowDy | Vertical offset of the text shadow.May be a floating point value, such as "1.2". |
setShadowLayer(float,float,float,int) |
| android:shadowRadius | Blur radius of the text shadow.May be a floating point value, such as "1.2". |
setShadowLayer(float,float,float,int) |
Example XML Snippet for TextView with Shadow
| Code | Output |
|---|---|
|
|
- TextView with Pattern
Working with Pattern is Quite Simple and Easy,to implement this we going to take help of Bitmap and BitmapShader Classes
| Class | Description |
|---|---|
| android.graphics.Bitmap |
A Bitmap Can be an Images/Drawable which may consits of solid colors or image. A Bitmap graphic composed into very small i.e., which are in the form of Pixels.
|
| android.graphics.BitmapShader | Shader used to draw a bitmap as a texture. The bitmap can be repeated or mirrored by setting the tiling mode. |
Steps to Create TextView with Pattern
-
Open ur activity_main.xml file in layout folder.
project/app/src/main/res/layout/activity_main.xml
- Inside the root layout declare a TextView with minimum attributes like width,height,text,id etc.,
-
Now open ur MainActivty Class.
project/app/src/main/java/ur package/MainActivity.java
- In MainActivity class first find the View of the TextView using findViewById(ur TextView Id Name) and same this TextView view in TextView Reference Object
- Now make ur image as Bitmap, we use BitmapFactory.decodeResource(ur Image)
- Now create a BitmapShader object with following paramters like Bitmap refernce object,TileMode of X,TileMode of Y
- Now we need to set all this properties to our TextView Reference by using getPaint() and setShader()
Note : if u dont have images in drawable folder you can download any image from internet and u should be paste that image in res/drawable folder
Example XML and Java Snippet for TextView with Pattern
XML Code
Java Code
//find the reference of TextView TextView textView = (TextView) findViewById(R.id.textView2); // making image as bitmap Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sdfsdf); // creating BitmapShader object with bitmap object,TileMode of X and TileMode of Y Shader shader = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT); // setting patter to TextView with getPaint() and setShader() methods textView.getPaint().setShader(shader);
Output
- TextView with MutliColor
Working with MutliColor is unique and interesing topic we can use n number of varitons in this we are going to do one of them,to implement this we going to take help of Shader and Matrix Classes
| Class | Description |
|---|---|
| android.graphics.Matrix |
The Matrix class holds a 3x3 matrix for transforming coordinates. |
| android.graphics.Shader | Shader is the based class for objects that return horizontal spans of colors during drawing. A subclass of Shader is installed in a Paint calling paint.setShader(shader). After that any object (other than a bitmap) that is drawn with that paint will get its color(s) from the shader. |
Steps to Create TextView with MutliColor
-
Open ur activity_main.xml file in layout folder.
project/app/src/main/res/layout/activity_main.xml
- Inside the root layout declare a TextView with minimum attributes like width,height,text,id etc., or simplily you can copy the below XML snippet for TextView With Multi Color
- Create a array file in Values folder with name array.xml and add colors with in items attribute or simplily you can copy the below XML snippet for array.xml file
-
after adding your favourite color, Now open ur MainActivty Class and the complete code from Java Snippet for TextView with MutliColor
project/app/src/main/java/ur package/MainActivity.java
project/app/src/main/res/values/array.xml
XML Snippet for TextView with MutliColor
Java Snippet for TextView with MutliColor
Output
