Friday, 24 June 2016

Custom Toast Example


Custom toast is used to display image in your project. E.g. Welcome message or any other message in image
Firstly Create new project and Add any image to display welcome message in drawable folder.
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?> 
<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.database.safal.toast.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="hello_world" />
</RelativeLayout>
 

Create another xml file inside the layout directory. 
Here you can add ImageView and TextView in this xml file. 
Create new layout
 
 customtoast.xml

<?xml version="1.0" encoding="utf-8"?>

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical" android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:id="@+id/custom_toast_layout">

  

    <TextView

        android:id="@+id/custom_toast_message"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Toast" />
    
  <ImageView

        android:id="@+id/custom_toast_image"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:contentDescription="hello_world"

        android:src="@drawable/welcome"/>

</LinearLayout>
 


 
Go to MainActivity.java 




package com.database.safal.toast;

  

  import android.support.v7.app.AppCompatActivity;

  import android.os.Bundle;

  import android.view.Gravity;

  import android.view.LayoutInflater;

  import android.view.View;

  import android.view.ViewGroup;

  import android.widget.Toast;

  

  public class MainActivity extends AppCompatActivity {

  

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        //Creating the LayoutInflater instance

        LayoutInflater li = getLayoutInflater();

        //Getting the View object as defined in the customtoast.xml file

        View layout = li.inflate(R.layout.customtoast,

                (ViewGroup) findViewById(R.id.custom_toast_layout));

  

        //Creating the Toast object

        Toast toast = new Toast(getApplicationContext());

        toast.setDuration(Toast.LENGTH_SHORT);

        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);

        toast.setView(layout);//setting the view of custom toast layout

        toast.show();

    }

}
 
Output




 You also see my other posts this links
 

No comments:

Post a Comment