Working with Custom Toast notification


To understand this you need to know about the Toast  and how it is used.Now, you are ready to create Custom Toast notification to show simple text/image notification.

Starting new Project

1. Create a new project in Eclipse from File ⇒ New ⇒ Android Application Project.

2.Create a new project File -> New -> Android Project 2. In Package Explorer right click on res/layout folder and create a new Android XML File and name it as you wish.

create one XML layout file and give a name cust_toast_layout.xml and define the below layout.
cust_toast_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/relativeLayout1"
  android:background="@android:color/white">

 <TextView
     android:textAppearance="?android:attr/textAppearanceLarge"
     android:id="@+id/textView1" android:layout_height="wrap_content"
     android:layout_width="fill_parent"
     android:text="Paresh N. Mayani"
     android:gravity="center"
     android:textColor="@android:color/black">
    </TextView>

    <ImageView
     android:layout_height="wrap_content"
     android:layout_width="fill_parent"
     android:src="@drawable/new_logo"
     android:layout_below="@+id/textView1"
     android:layout_margin="5dip"
     android:id="@+id/imageView1">
    </ImageView>

    <TextView
     android:id="@+id/textView2"
     android:layout_height="wrap_content"
     android:layout_width="fill_parent"
     android:text="This is the demo of Custom Toast Notification"
     android:gravity="center"
     android:layout_below="@+id/imageView1"
     android:textColor="@android:color/black">
    </TextView>

</RelativeLayout>

         
And Now inflate the above layout to create/show custom Toast notification  in mainactivity

What does it mean to inflate a view from an xml file?

Inflating a view means taking the layout XML, creating the views specified within and then adding those views to the parent ViewGroup. When you call setContentView(), it attaches the views it creates from reading the XML to the activity. You can also use LayoutInflater to add views to another ViewGroup, which can be a useful tool in a lot of circumstances

When you write an XML layout, it will be inflated by the Android OS which basically means that it will be rendered. Let's call that implicit inflation (the OS will inflate the view for you). For instance:


class Name extends Activity{
    public void onCreate(){
         // the OS will inflate the your_layout.xml
         // file and use it for this activity
         setContentView(R.layout.your_layout);
    }
}
You can also inflate views explicitly by using the LayoutInflater. In that case you have to:

  1. Get an instance of the LayoutInflater
  2. Specify the XML to inflate
  3. Use the returned View
For instance:

LayoutInflater inflater = LayoutInflater.from(YourActivity.this); // 1
View theInflatedView = inflater.inflate(R.layout.your_layout, null); // 2 and 3
CustomToastDemoActivity.java
        LayoutInflater inflater = getLayoutInflater();
        View view = inflater.inflate(R.layout.cust_toast_layout,
                                       (ViewGroup) findViewById(R.id.relativeLayout1));

        Toast toast = new Toast(this);
        toast.setView(view);
        toast.show();   

Alll the world is a laboratory to the inquiring mind! Join Us

Comments

Popular posts from this blog

The Guide to DeepLearning with Tensorflow and Keras - The Beginning

Android Navigation Drawer sliding menu with fragments

Conditional Statements in Python - A Comprehensive Beginners Guide