Saturday, 29 August 2015

Android Programming Tutorial 34 : Use Font and Colors For TextView Text In Android:

Create Android Application:

  • File >> New >> Android Application
  • Enter Project name: FontNColorChangeTextApp
  • Package: com.ambilpursunil.newapp
  • Keep other default selections, click Next until you reach Finish

                                   

     1.Simply Copy and Past the Code which is display below in the  MainActivity.java 

    package com.ambilpursunil.newapp;

    import android.app.Activity;
    import android.os.Bundle;
    import android.text.Html;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.TextView;

    public class MainActivity extends Activity {

                @Override
                protected void onCreate(Bundle savedInstanceState) {
                            super.onCreate(savedInstanceState);
                            setContentView(R.layout.activity_main);
                           
     //to get Italic and paragraph and break uncomment this piece of code below
                           
                             // fig(i)..

                            TextView foo = (TextView)findViewById(R.id.tvv);
                            foo.setText(Html.fromHtml(getString(R.string.hello_world)));
                           
       //to get Text Color In the Activity class Uncomment this Code below

                            /*fig(ii)
                           
                            TextView redColorTextView = (TextView)findViewById(R.id.tvv);
                            String redString = getResources().getString(R.string.hello_world);
                            redColorTextView.setText(Html.fromHtml(redString));*/ 
                            }

                @Override
                public boolean onCreateOptionsMenu(Menu menu) {
         // Inflate the menu; this adds items to the action bar if it is present.
                            getMenuInflater().inflate(R.menu.main, menu);
                            return true;
                }

                /*@Override
                public boolean onOptionsItemSelected(MenuItem item) {
                            // Handle action bar item clicks here. The action bar will
             // automatically handle clicks on the Home/Up button, so long
                            // as you specify a parent activity in AndroidManifest.xml.
                            int id = item.getItemId();
                            if (id == R.id.action_settings) {
                                        return true;
                            }
                            return super.onOptionsItemSelected(item);
                }*/
    }

    2.Simple Copy and Past the below code:  actvitiy_main.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFF66"
        tools:context="com.ambilpursunil.newapp.MainActivity" >

        <TextView
            android:layout_width="wrap_content"
            android:id="@+id/tvv"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />

    </RelativeLayout>

    4.Change the code of    string.xml to below code.

    <?xml version="1.0" encoding="utf-8"?>
    <resources>

        <string name="app_name">SampleDemoOne</string>
        <string name="action_settings">Settings</string>       
        <!--
         fig(ii) goto fig(ii) in Activity class
         <string name="hello_world"><![CDATA[<b><font color=#FF0000>Hello world!        </b>]]></string> -->
         <!--
         fig (i) goto fig(i) in Activityclass -->
        <string name="hello_world">
        <![CDATA[
        <p>This is a html-formatted string with <b>bold</b> and <i>italic</i>                   text</p> 
        <p>This is another paragraph of the same string.</p>
         ]]>
    </string>
        
    </resources>

     3.Copy and past the code for manifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.ambilpursunil.newapp"
        android:versionCode="1"
        android:versionName="1.0" >

        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="21" />

        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>

    </manifest>

    4.Right click on the project and Run As-->Android Application


    OutPut:

    When our application launch just observe the text. 


    Please Send Your Comments To ambilpura.sunil@gmail.com



    Stay Tune For Next Tutorial...  EditText Validations In Android:

    No comments:

    Post a Comment