Monday, 31 August 2015

Android Programming Tutorial 44 : CustomToast In Android

Toast In Android:

Toast is a type of pop-up message which is display for the developer screeen and disappear automatically in a short span of time.

Why Toast ?

One of the main reason to have Toast in android, is to debug our application,
and see does our application working properly or not.

Custom Toast ?

Custom Toast is to display our own Toast message.

Tutorial For Toast :

Create Android Application:

  • File >> New >> Android Application
  • Enter Project name: CustomToastApp
  • 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.view.Gravity;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Toast;

    public class MainActivity extends Activity {

                @Override
                protected void onCreate(Bundle savedInstanceState) {
                            super.onCreate(savedInstanceState);
                            setContentView(R.layout.activity_main);
                            LayoutInflater li = getLayoutInflater();
                           
                            View v = li.inflate(R.layout.toast, (ViewGroup)findViewById(R.id.customToast));
                           
                            Toast t = new Toast(getApplicationContext());
                            t.setDuration(Toast.LENGTH_LONG);
                            t.setGravity(Gravity.CENTER_VERTICAL,0, 0);
                            t.setView(v);
                            t.show();
                }                     
               
      }

    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="#000000">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="195dp"
            android:text="See Here Its Toasting...."
            android:textColor="#FFFFFF"
            android:textSize="18dp"
            android:textStyle="bold" />

    </RelativeLayout>

    3.Create another xml file and past the below code    toast.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:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.customtoast.MainActivity" >

        <TextView
            android:id="@+id/customeToasttv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="172dp"
            android:textSize="18dp"
            android:textStyle="italic"
            android:textColor="#0B610B"
            android:text="CustomToast Megs Is Here...." />

        <ImageView
            android:id="@+id/customToast"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/customeToasttv"
            android:layout_alignLeft="@+id/customeToasttv"
            android:layout_marginBottom="41dp"
            android:layout_marginLeft="34dp"
            android:src="@drawable/andiii" />
    </RelativeLayout> 

    NOTE: Place image of your choose in any drawable folder.

    4.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="18"
            android:targetSdkVersion="18" />


        <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>

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


    OutPut:

    When our application launch a custom toast wiil appear on the screen for a while and disappear. 

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






    Stay Tune For Next Tutorial... Toast In Android:

    No comments:

    Post a Comment