Close
Topic : User Interface » Xamarin.Android » TextView.



TextView:

TextView is one of the most fundamental Android User Interface widget, which is used to display text on Android Screen and optionally allows user to edit it Programmatically.



Creating TextView:

We can use Android Designer to create the textview in Android.

TextView : infobrother

Using XML, we can create text view using the following code.


<TextView
        android:text="Text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView1"
/>

TextView Attributes:

Plenty of textView Attributes are provided by android to work with text. follow the Official TextView Documentation To Learn more about these attributes.





Creating Shadow Effect Using TextView:
TextView Shadow Effect: infobrother
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:gravity="center_horizontal">
  
    <TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TEXTVIEW SHADOW"
        android:textSize="40dp"
        android:textStyle="bold"
        android:textColor="#FF0066"
        android:shadowColor="#303030"
        android:shadowRadius="10"
        android:shadowDy="-10" />
  
    <TextView
        android:id="@+id/textview2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TEXTVIEW SHADOW"
        android:textSize="40dp"
        android:textStyle="bold"
        android:textColor="#99FF00"
        android:shadowColor="#303030"
        android:shadowRadius="10"
        android:shadowDy="15"
        android:shadowDx="15" />
</LinearLayout>

In Above Example, we Apply shadow effect on TextView using XML Layout. We use the following properties of text view to apply display shadow.


PropertiesDescription:
ShadowDxSpecifies the X-axis of shadow. We can assign positive (+) values to draw shadow on the right side of text, and Negative value (-) to draw shadow on the left side of text.
ShadowDySpecifies the Y-axis of shadow. We can assign positive (+) values to draw shadow below the text, and Negative value (-) to draw shadow above the text.
ShadowRadiusSpecifies how much the shadow should be blurred at the edges.
ShadowColorSpecifies the shadow color.


Creating AutoLink using TextView:
TextView AutoLink: infobrother
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:gravity="center_horizontal">
  
    <TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="www.infobrother.com"
        android:textSize="25dp"
        android:textStyle="bold"
        android:textColor="#184757"
        android:autoLink="web"/>
</LinearLayout>

In Above code, we create clickable Link using the AutoLink property provided by TextView. This Link will highlighted in the text, and when user click on it, the link will open in the browser.



Using External Fonts:
TextView Custom Fonts: infobrother
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:gravity="center_horizontal"
    android:padding="10dp">
  
    <TextView
        android:text="USING CUSTOM FONT"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView1"
        android:textSize="40dp"
        android:textColor="#008080" />
  
    <TextView
        android:text="Hi There, I am using custom font"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView2"
        android:textSize="35dp"
        android:textColor="#184757" />
</LinearLayout>
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Graphics;

namespace Text_View
{
    [Activity(Label = "Text_View", MainLauncher = true)]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            //TextView # 1
            var textview1 = FindViewById<TextView>(Resource.Id.textView1);
            Typeface tf1 = Typeface.CreateFromAsset(Assets, "Insomnia .ttf");
            textview1.SetTypeface(tf1, TypefaceStyle.Bold);

            //TextView # 2
            var textview2 = FindViewById<TextView>(Resource.Id.textView2);
            Typeface tf2 = Typeface.CreateFromAsset(Assets, "chops .ttf");
            textview2.SetTypeface(tf2, TypefaceStyle.Bold);
        }
    }
}

In above Example, We Use the External fonts to customize the text view. In order to use custom fonts, Follow the given Steps:



TextView add fonts in project: infobrother
  • 1 » First of All, you need to download your required font. And note that android supports .ttf extension.
    2 » Add downloaded .ttf font file in the Assets folder of your project.
    3 » Create the Text view in Main.axml file.
    4 » Open your MainActivity.cs file and write the given code to use the font in your app.


















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement