Monday, 31 August 2015

Android Programming Tutorial 45 : Toast In Android

OutPut Of the Tutorial:



Create Android Application:

  • File >> New >> Android Application
  • Enter Project name: ToastApp
  • 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.os.Bundle;
    imort android 
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;

    public class MainActivity extends  Activity {

                @Override
                protected void onCreate(Bundle savedInstanceState) {
                            super.onCreate(savedInstanceState);
                            setContentView(R.layout.activity_main);
                           
               Button toastBtn = (Button) findViewById(R.id.toastBtn);
               toastBtn.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
               // TODO Auto-generated method stub
    Toast.makeText(MainActivity.this, "Toating Here......", Toast.LENGTH_LONG).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: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/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Click The Button To See The Toast Message...." />

        <Button
            android:id="@+id/toastBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView1"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="42dp"
            android:text="Click To Toast" />

    </RelativeLayout>

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

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


    OutPut Of the Tutorial:




    OutPut:

    When our application launch Just Click on the button i.e.. Click To Toast check the output 

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






    Stay Tune For Next Tutorial...  Google Maps Guide In Android:

    No comments:

    Post a Comment