Saturday, December 24, 2016

Custom Toast In Android

What is Toast in android?

Andorid Toast can be used to display information for the short period of time. A toast contains message to be displayed quickly and disappears after sometime. 

The android.widget.Toast class is the subclass of java.lang.Object class.
You can also create custom toast as well for example toast displaying image.

Syntax for displaying simple toast.

Toast.makeText(getApplicationContext,"Your message comes here",2000).show();


This is about displaying the simple toast. this message will appear till the 2 second on screen.


Custom Toast

We can create custom toast in android. So, you can display some images like congratulations or some network status etc. It means you are able to customize the toast now.


Steps to create the custom toast in android
1) Create the customtoast.xml file. save it on res/drawable folder.
2) In MainActivity.java.  
    i) Create the LayoutInflater  instance.
   ii) Get the view object as defined in the customtoast.xml.
  iii)  Create the Toast class object.



activitymain.xml


  1. <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_centerHorizontal="true"  
  11.         android:layout_centerVertical="true"  
  12.         android:text="@string/hello_world" />  
  13.   
  14. </RelativeLayout>  




customtoast.xml


  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
  3.       android:id="@+id/custom_toast_layout"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical"  
  7.     android:background="#FF00FF"  
  8.      >  
  9.        
  10.     <ImageView  
  11.         android:id="@+id/custom_toast_image"  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:src="@drawable/congratulation"/>  
  15.       
  16. <TextView  
  17.         android:id="@+id/custom_toast_message"  
  18.         android:layout_width="wrap_content"  
  19.         android:layout_height="wrap_content"  
  20.         android:text="custom toast" />  
  21. </LinearLayout> 




Activity class

  1. package com.baba.customtoastexample;  
  2. import android.os.Bundle;  
  3. import android.app.Activity;  
  4. import android.view.Gravity;  
  5. import android.view.LayoutInflater;  
  6. import android.view.Menu;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.widget.Toast;  
  10.   
  11. public class MainActivity extends Activity {  
  12.      @Override  
  13.         public void onCreate(Bundle savedInstanceState) {  
  14.             super.onCreate(savedInstanceState);  
  15.             setContentView(R.layout.activity_main);  
  16.               
  17.         //Creating the LayoutInflater instance  
  18.             LayoutInflater li = getLayoutInflater();  
  19.         //Getting the View object as defined in the customtoast.xml file  
  20.             View layout = li.inflate(R.layout.customtoast,  
  21.               (ViewGroup) findViewById(R.id.custom_toast_layout));  
  22.            
  23.         //Creating the Toast object   
  24.             Toast toast = new Toast(getApplicationContext());  
  25.             toast.setDuration(Toast.LENGTH_SHORT);  
  26.             toast.setGravity(Gravity.CENTER_VERTICAL, 00);  
  27.             toast.setView(layout);//setting the view of custom toast layout  
  28.             toast.show();  
  29.         }  
  30.         @Override  
  31.         public boolean onCreateOptionsMenu(Menu menu) {  
  32.             getMenuInflater().inflate(R.menu.activity_main, menu);  
  33.             return true;  
  34.         }  
  35.   
  36. }  

Saturday, November 12, 2016

Intents in android

Android Intent is the message that is passed between components such as activities, content providers, broadcast receivers, services etc.
It is used with startActivity() method to invoke activity, broadcast receiver etc.
Android intents are mainly used to:
Start the service
Launch an activity
Display a web page
Display a list of contacts
Broadcast a message
Dial a phone call etc.
Types of Android Intents
There are two types of intents in android: implicit and explicit.
1) Implicit Intent

Implicit Intent doesn't specifiy the component. In such case, intent provides information of available components provided by the system that is to be invoked.
For example, you may write the following code to view the webpage.
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.javatpoint.com"));
startActivity(intent);

2) Explicit Intent

Explicit Intent specifies the component. In such case, intent provides the external class to be invoked.

Intent i = new Intent(getApplicationContext(), ActivityTwo.class);
startActivity(i);
To get the full code of explicit intent, visit the next page.