Monday, 24 August 2015

Android Programming Tutorials 3 : Text Play(Left, Right,Center)

In This Tutorial we are going to see to display the text to left side, Right side, center, changing the color has you enter and the text can be visible randomly through out the screen in various colors.Let Start....



OutPut Of the Tutorial:




Create Android Application:

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


2.Copy and past the following code in the  MainAcivity.java

package com.ambilpursunil.newapp;

import java.util.Random;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ToggleButton;

public class TextPlay extends Activity implements View.OnClickListener {
            /*
             * when we implements any class then we must(compulsory) implement all the
             * method of that class. if we entends then we can r cant implement all the
             * methods of that class
             */
            Button chkCmd;
            ToggleButton passTog;
            EditText input;
            TextView display;

            @Override
            protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.text);
            baconAndEggs();
            passTog.setOnClickListener(this);
           chkCmd.setOnClickListener(this);
            }
            private void baconAndEggs() {
           // TODO Auto-generated method stub
           chkCmd = (Button) findViewById(R.id.bResults);
           passTog = (ToggleButton) findViewById(R.id.tbPassword);
           input = (EditText) findViewById(R.id.etCommand);
           display = (TextView) findViewById(R.id.tvResults);
           }
           @Override
           public void onClick(View view) {
           // TODO Auto-generated method stub
           switch (view.getId()) {
           case R.id.bResults:
           String check = input.getText().toString();
           display.setText(check);
           if (check.contentEquals("left")) {
           display.setGravity(Gravity.LEFT);
           }else if (check.contentEquals("center")) {
           display.setGravity(Gravity.CENTER);
           } else if (check.contentEquals("right")) {
           display.setGravity(Gravity.RIGHT);
           } else if (check.contentEquals("red")) {
           display.setTextColor(Color.RED);
          } else if (check.contains("WTF")) {
           Random crazy = new Random();
          display.setText("WTF!!!!");
          display.setTextSize(crazy.nextInt(75));
          display.setTextColor(Color.rgb(crazy.nextInt(265),
          crazy.nextInt(265), crazy.nextInt(265)));
          switch (crazy.nextInt(3)) {
          case 0:
          display.setGravity(Gravity.LEFT);
           break;
          case 1:
          display.setGravity(Gravity.CENTER);
          break;
          case 2:
          display.setGravity(Gravity.RIGHT);
          break;
          }
         } else {
          display.setText("invalid");
          display.setGravity(Gravity.CENTER);
          display.setTextColor(Color.BLACK);
          }
          break;
          case R.id.tbPassword:
          if (passTog.isChecked()) {
          input.setInputType(InputType.TYPE_CLASS_TEXT
          | InputType.TYPE_TEXT_VARIATION_PASSWORD);
          } else {
          input.setInputType(InputType.TYPE_CLASS_TEXT);
           }
           break;
          }
      }
}

2.Change the  activity_main.xml  file as the below

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="25dp" >

    <EditText
        android:id="@+id/etCommand"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Hello Come On"
        android:password="true" />

    <!--
    Difference Between android:hint and android:text is hint cant be visible by clicking on the box we can type
    what ever we want like username,pwd. but in case of text it will appear in field and if we want to type we have
    to erase the text in the field and we need to type.
    android:password used to display text in term of password like ****** in the field
    -->

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="100" >

        <Button
            android:id="@+id/bResults"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="20"
            android:text="Button" />

        <ToggleButton
            android:id="@+id/tbPassword"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="80"
            android:checked="true"
            android:gravity="center"
            android:paddingBottom="10dp"
            android:text="ToggleButton" />
    </LinearLayout>

    <TextView
        android:id="@+id/tvResults"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="TextView" />

</LinearLayout>

3.Copy and past the code into the  manifest.xml file as the below

<?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="17" />

    <application
        android:allowBackup="true" 
        android:icon="@drawable/ic_launcher" 
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

    <activity 
       android:name="com.ambilpursunil.newapp.TextPlay"
       android:configChanges="keyboard|keyboardHidden|orientation>

   <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 Choose --->Run As-->Android Application as observer the OutPut:



OutPut Of the Tutorial:

                                   


There is one edittext, one button, toggle button and a textview are there on the screen Just type right or left or center in the edit text and click on the toggle button ON the you will the right in the edittext if you click the toggle button as OFF the text in the edit text will be change to password format, after entering the text as Right in the edittext click on the button the that text will appear on right same it will work for Left and center also.
If you enter Red in the Edit text the  text will appear as red.
If You ente WTF in cap in the edit text and click continously on the button then you will observe The text WTF will appear in randomly through the screen in different color.

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

Stay Tune For Next Tutorial...How Sent Mail From Android:

No comments:

Post a Comment