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 android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Shadow TextView" android:textSize="80sp" android:padding="12dp" android:textColor="@android:color/white" android:shadowColor="@android:color/holo_red_dark" android:shadowDx="12" android:shadowDy="12" android:shadowRadius="3" android:textStyle="bold"/>
  • 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

  1. Open ur activity_main.xml file in layout folder.

    project/app/src/main/res/layout/activity_main.xml

  2. Inside the root layout declare a TextView with minimum attributes like width,height,text,id etc.,
  3. Now open ur MainActivty Class.

    project/app/src/main/java/ur package/MainActivity.java

  4. 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
  5. Now make ur image as Bitmap, we use BitmapFactory.decodeResource(ur Image)
  6. 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

  7. Now create a BitmapShader object with following paramters like Bitmap refernce object,TileMode of X,TileMode of Y
  8. Now we need to set all this properties to our TextView Reference by using getPaint() and setShader()

Example XML and Java Snippet for TextView with Pattern

XML Code

<TextView android:id="@+id/textView2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Patters TextView" android:textSize="80sp" android:gravity="center" android:textStyle="bold" />

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

  1. Open ur activity_main.xml file in layout folder.

    project/app/src/main/res/layout/activity_main.xml

  2. 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
  3. 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
  4. project/app/src/main/res/values/array.xml

  5. 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

XML Snippet for TextView with MutliColor

<TextView android:id="@+id/textView3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Patters TextView" android:textSize="80sp" android:gravity="center" android:textStyle="bold" />

Java Snippet for TextView with MutliColor

Output



By end of this we are able to implement TextView with Shadow,Patters,MultiColor.
Hope we get back to u soon Learning cure never ends keep visiting this site for new updates.

No comments:

Post a Comment