Wednesday, January 14, 2015

106-Programmr: Enhanced TextView


.
106-Programmr: Enhanced TextView
You have learned the basics of TextView attributes.
There are many more attributes that would enhance the look and functionality of TextView.
This tutorial will demonstrate some of their uses and it contains just TextView objects.

0) Starting Up

1) TextSize, Typeface and Drawables

<?xml version="1.0" encoding="utf-8"?>    
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"    
    android:orientation="vertical"    
    android:layout_width="fill_parent"    
    android:layout_height="fill_parent"    
    >    
    <TextView    
        android:layout_width="fill_parent"    
        android:layout_height="wrap_content"  
        android:text="About"
        android:gravity="center"
        android:textSize="30dp"
        android:typeface="serif"        
        android:drawableLeft="@drawable/smiley"
        android:drawableRight="@drawable/smiley"    
    />        
</LinearLayout>
OUTCOME.

2) Background, Text Color, Auto Links and String Resources

<?xml version="1.0" encoding="utf-8"?>    
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"    
    android:orientation="vertical"    
    android:layout_width="fill_parent"    
    android:layout_height="fill_parent"    
    >    
    <TextView    
        android:layout_width="fill_parent"    
        android:layout_height="wrap_content"  
        android:gravity="center"
        android:typeface="serif"        
        android:text="About"
        android:textSize="30dp"
        android:drawableLeft="@drawable/smiley"
        android:drawableRight="@drawable/smiley"    
    />    
    <TextView
        android:id="@+id/tvw_textview"    
        android:layout_width="fill_parent"    
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:padding="10dp"
        android:typeface="sans"        
        android:background="@drawable/bgcloud"
        android:textColor="#000000"
        android:autoLink="all"
        android:text="@string/about_texts"    
    />    
</LinearLayout>
Refer the last attribute of the second TextView object.
This TextView is using a String Resource named about_texts.
The resource must be added to strings.xml file.
Refer the codes below.
res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>  
<resources>  
<string name="about_texts">
    This app is developed by ABC Company Sdn Bhd.\n
    \n
    Visit our website www.abc.com.my\n
    \n
    Send enquiries to us at enquiry@abc.com.my\n
    \n
    Give us a call at +603-34567890\n
    \n
    </string>
</resources>  
\n means a new line.
OUTCOME.
Notice that Android has translated the url, email and phone number into clickable links.
If you click the url, Android will open the address using a web browser. (Note: add an Internet Permission statement to AndroidManifest to allow Internet Browsing).
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.MyAndroid"
      android:versionCode="1"
      android:versionName="1.0">
 <uses-permission android:name="android.permission.INTERNET" />     
    <application android:label="MyAndroid" android:icon="@drawable/ic_launcher">
        <activity android:name="MyAndroid"
                  android:label="MyAndroid">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="About"
                  android:label="About">        
        </activity>
        <activity android:name="Settings"
                  android:label="Settings">        
        </activity>        
    </application>
<uses-sdk android:minSdkVersion="8" />
</manifest>
At the time of this writing, the website for the url www.abc.com.my does not exist.
However, if you change it to www.abc.com you will be redirected to a live website.
If your device does not have any related apps, Android will display Unsupported action error message.
Sometimes, there may be a number of actions that could be related to the link.
Android will ask you to select your preferred action eg create new contact, add to a contact, send sms or call.

3) Further Exercise

.

No comments:

Post a Comment