Blogger Tips and TricksLatest Tips And TricksBlogger Tricks

Develop an application that uses GUI components, Font and Colours

 Simple application to change font size and color of textview

1)Open eclipse or android studio and select new android project
2)Give project name and select next
3) Choose the android version.Choose the lowest android version(Android 2.2) and select next
4) Enter the package name.package name must be two word seprated by comma and click finish
5)Go to package explorer in the left hand side.select our project.
6)Go to res folder and select layout.Double click the main.xml file
7)Now you can see the Graphics layout window.

8)Click the main.xml file and type the code below

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20sp"
        android:gravity="center"
        android:text="HELLO WORLD"
        android:textSize="20sp"
        android:textStyle="bold" />
    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Change font size"
        android:textSize="20sp" />
    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Change color"
        android:textSize="20sp" />
     <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Change font"
        android:textSize="20sp" />
</LinearLayout>
9)Again click the graphics layout tab and screen layout is look like below
 10)Go to project explorer and select src folder.Now select mainactivity.java file and type the following code

//import android.R;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class AndroidActivity extends Activity {
     float font =24;
     int i=1;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final TextView t1=(TextView) findViewById(R.id.textView1);
      Button  b1 = (Button) findViewById(R.id.button1);

        b1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                t1.setTextSize(font);
                font=font+4;
                if(font==40)
                    font=20;
            }
        });
        Button  b2 = (Button) findViewById(R.id.button2);
        b2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                switch(i)
                {
                case 1:
                    t1.setTextColor(Color.parseColor("#0000FF"));
                    break;
                case 2:
                    t1.setTextColor(Color.parseColor("#00FF00"));
                    break;
                case 3:
                    t1.setTextColor(Color.parseColor("#FF0000"));
                    break;
                case 4:
                    t1.setTextColor(Color.parseColor("#800000"));
                    break;
                }
                i++;
                if(i==5)
                    i=1;
                 
            }
              
        });
   
}
}
11)Now go to main.xml and right click .select run as option and select run configuration

12) Android output is present in the android emulator as shown in below.

6 comments:

  1. Android SDK allows you to create stunning mobile application loaded with more features and enhanced priority. With basis on Java coding language, you can create stunning mobile application with ease.

    Cado Magenge
    ”http://www.appdevelopmentcompany.com.au/custom-web-development.html”
    "http://appdevelopmentcompany.com.au/iphone-application-development.html"
    ”http://appdevelopmentcompany.com.au/ipad-application-development.html”

    ReplyDelete
  2. This community have a great motive and this will change the life of others.



    Android App Development Company Australia

    ReplyDelete
  3. Awesome blog its very informative and useful blog thanks for sharing.
    Looking to learn Android App Development Online Training
    Android App Development Trutorial

    ReplyDelete
  4. Really i am impressed from this post....the person who created this post is a genius and knows how to keep the readers connected..

    Mobile Application Development and Testing Training

    ReplyDelete
  5. I'm truly grateful for this article. This is exactly what I've been looking for. Turkey visa for Mexico is a visa for Mexican people. With this visa all the Mexicans visit Turkey .

    ReplyDelete

Flag Counter