Wednesday, May 13, 2015

Simple Android program on Toast

In android toast is use to show the notification on the screen for some period of time. Showing period will define by the user. If we take short in toast then notification will be for the short period of time and if user take then notification will increase some time to show the notification. Time period will depend on users system.
In xml file we take Relative layout and a text view. In java file we take only toast and a message that will bi show in the notification. Here we take short length so notification will display on screen for short period of time. Here we can take long also then notification will display for long time. 

activity_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"
   
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>


MainActivity.java
package com.example.toast; 
    import android.os.Bundle; 
    import android.app.Activity; 
    import android.view.Menu; 
    import android.view.View; 
    import android.widget.Toast; 
     
    public class MainActivity extends Activity { 
         @Override 
            public void onCreate(Bundle savedInstanceState) { 
                super.onCreate(savedInstanceState); 
                setContentView(R.layout.activity_main); 
                 
               Toast.makeText(getApplicationContext(),"Hello Android Devoloper",Toast.LENGTH_SHORT).show();        
            } 
     
            @Override 
            public boolean onCreateOptionsMenu(Menu menu) { 
                getMenuInflater().inflate(R.menu.activity_main, menu); 
                return true; 
            } 
     
    }  


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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.toast.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>

No comments:

Post a Comment