Working With Text Controls in Mono for Android

Introduction

 
In this article, we are going to see how we can create a number of different Text controls in your Android application. We can configure, style, and manipulate the controls in a variety of ways; we have so many useful attributes we can use within the application to change the style, position, etc. 

Learn from the basic text controls to your layout files, generally we have four types of Text Controls in Android:
  1. TextView
  2. EditText
  3. AutoCompleteTextView
  4. MultiCompleteTextView

TextView

 
TextView controls are usually included as part of your Activity's layout resource file. TextView has an interesting feature which is that it can automatically create a link based on the content of the Text; if the Text is an URL, an e-mail address or a phone number then when the user clicks on the textview the default intent launches whether it is the web browser or the dialer. 
 
The property which we use for the linking is android:autoLink=""
 
Let's create the application to understand TextView
  • First, create a new android application

    create android application

  • Go to Main.axml in Resources/Layout and do these changes

    main.axml page in android
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:orientation="vertical"  
    4.     android:layout_width="fill_parent"  
    5.     android:layout_height="fill_parent"  
    6.     >  
    7.   <TextView  
    8.       android:layout_width="fill_parent"  
    9.       android:layout_height="wrap_content"  
    10.       android:text="You can create a number of different Text controls in your Android application.  
    11.                          you can configure, style, and manipulate the controls in a variety of ways, we have so many useful attributes which  
    12.                          we can use within the application to change the style, position etc."  
    13.       android:textColor="#ff0fffff"  
    14.     />  
    15.   <TextView  
    16.       android:layout_width="fill_parent"  
    17.       android:layout_height="wrap_content"  
    18.       android:text="TextView controls are usually included as part of your Activity's layout resource file  
    19.                          TextView have an interesting feature which is automatically create the link based on the content of the Text if  
    20.                          its is an URL, an e-mail or a phone number so that when the user clicks on the textview the default intent whether it is the web browser or the dialer launches."  
    21.       android:textColor="#ff000fff"  
    22.     />  
    23. </LinearLayout>  
    In the above code, you see android:text property which is used to insert the text in normal textview and the other property android:textColor used to change the color of the text.

    Output

    textview in android

    As I said about android:autoLink the property we used with textview to detect whether it is a web, email, phone and address information. like if I write any website URL or any phone number in textview and android:autoLink="all" the property was not used with the textview than the URL shows as simple text and if I use android:autoLink="all" property then it will automatically show as a clickable link.

    Change the Main.axml like below to see the effect of android:autoLink="all" property:
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:orientation="vertical"  
    4.     android:layout_width="fill_parent"  
    5.     android:layout_height="fill_parent"  
    6.     >  
    7.   <TextView  
    8.       android:layout_width="fill_parent"  
    9.       android:layout_height="wrap_content"  
    10.       android:text="You can create a number of different Text controls in your Android application."  
    11.     />  
    12.   <TextView  
    13.       android:layout_width="fill_parent"  
    14.       android:layout_height="wrap_content"  
    15.       android:text="http://www.c-sharpcorner.com/"  
    16.       android:autoLink="all"  
    17.     />  
    18.   <TextView   
    19.       android:layout_width="fill_parent"  
    20.       android:layout_height="wrap_content"  
    21.       android:text="2 659-697-0000"  
    22.       android:autoLink="all"  
    23.     />  
    24. </LinearLayout> 
    Output

    textview autolink in android

EditText

 
EditText is a subclass of the TextView class, it is functionally rich and customizable and enables users to edit text, it is like the TextBox.
 
Let's create the application to understand EditText
  • First, create a new android application
     
  • Go to Main.axml in Resources/Layout and do these changes
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:orientation="vertical"  
    4.     android:layout_width="fill_parent"  
    5.     android:layout_height="fill_parent"  
    6.     >  
    7.   <EditText  
    8.      android:layout_width="fill_parent"  
    9.      android:layout_height="wrap_content"  
    10.      android:capitalize="characters"  
    11.      android:autoText="true"  
    12.   />  
    13.   <EditText  
    14.     android:layout_width="fill_parent"  
    15.     android:layout_height="wrap_content"  
    16.     android:capitalize="words"  
    17.     android:autoText="true"  
    18.   />  
    19.   <EditText  
    20.     android:layout_width="fill_parent"  
    21.     android:layout_height="wrap_content"  
    22.     android:capitalize="none"  
    23.     android:autoText="true"  
    24.   />  
    25.   <EditText  
    26.     android:layout_width="fill_parent"  
    27.     android:layout_height="wrap_content"  
    28.     android:password="true"  
    29.   />  
    30. </LinearLayout> 
In the above code, you see we use three types of properties autoText, capitalize and password same as android provides many other properties like singleLine etc. But these three are the main which is used with EditText:
  1. autoText : It is used to make the EditText in such a manner that it is able to correct the common spelling mistakes. you will see the effect of autoText property in the below images with the capitalize property of EditText, the possible correct matching spelling will come according to the text which you entered. 
     
  2. capitalize : This is used to specify the capitalization condition of the text. It further have three conditions:

    --characters : When you set capitalize property as characters, then all the characters or words which you enter will show in the upper case.
        
    characters property in android

    --words : When you set capitalize property as words, then the first character of each words will show in upper case and rest in lower case.
         
    words property in android

    --none : When you set capitalize property as none, then all the characters or words which you enter will show in lower case.
        
    none property in android
     
  3. password : password property is used to make a password type EditText or TextBox. Means the typed characters are displayed as bullets (on Windows) or asterisks.
        
    password property in android

AutoCompleteTextView

 
An autocompletetextview is an editable text view that shows completion suggestions automatically while the user is typing. We can show string items in autocompletetextview. So that items will display according to the characters we give.
 

MultiCompleteTextView

 
We can override the MultiAutoCompleteTextView and add the needed implementation of the default textview, it works the same way as the AutoCompleteTextView except you can add a Tokenizer that parses the text and allows you to suggest where to start suggesting words.

I will explain in deep about the last two categories of Text control, "AutoCompleteTextView and MultiCompleteTextView", in my next article. 

Summary 


Hope you like this article. Post your comments and feedback so that I can improve myself. 
Thank You........


Similar Articles