Custom Toast

Custom Toast in Android

In this example we will learn how to set a custom Toast.

Creating Custom Toast in Android:

Sometimes a simple Toast may not be satisfactory, and then we can go for customizing the Toast. To create a custom layout, define a View layout, in XML and pass the root View object to the setView(View) method.

In this example I have created a extra xml layout as custom_toast.xml.

File:custom_toast.xml:

<LinearLayout    
xmlns:android="http://schemas.android.com/apk/res/android"    
android:id="@+id/custom_layout"    
android:layout_width="match_parent"    
android:layout_height="match_parent"    
android:orientation="vertical">
<LinearLayout        
android:layout_width="match_parent"        
android:layout_height="50dp"        
android:padding="10dp"        
android:gravity="center_vertical"        
android:background="#3F51B5"        
android:orientation="horizontal">


<ImageView            
android:id="@+id/image"           
android:layout_width="wrap_content"            
android:layout_height="match_parent"            
android:src="@drawable/mess"/>

<TextView           
android:id="@+id/text"            
android:textColor="#FFFFFF"            
android:layout_width="wrap_content"           
android:layout_height="wrap_content" />


</LinearLayout>
</LinearLayout>

File: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"    
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.hss_24.toast.MainActivity">

<Button        
android:id="@+id/button"        
android:layout_width="match_parent"        
android:layout_height="wrap_content"        
android:layout_alignParentEnd="true"        
android:layout_alignParentRight="true"        
android:layout_alignParentTop="true"        
android:layout_marginTop="158dp"        
android:text="Click Me " />

<Button        
android:id="@+id/button_custom"        
android:layout_width="match_parent"        
android:layout_height="wrap_content"        
android:layout_alignParentEnd="true"       
android:layout_alignParentRight="true"        
android:layout_alignParentTop="true"       
android:layout_marginTop="200dp"        
android:text="Custom Toast" />

</RelativeLayout>

File:MainActivity.java:


import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override           
               public void onClick(View v) {

               Toast toast = Toast.makeText(getApplicationContext(), "You Clicked A Button", 
               Toast.LENGTH_LONG);
               TextView textView = (TextView) toast.getView().findViewById(android.R.id.message);
                textView.setTextColor(Color.RED);
                toast.show();
            }
        });


        Button custom_button = (Button) findViewById(R.id.button_custom);
        custom_button.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                //  inflating the layout               
                LayoutInflater inflater = getLayoutInflater();
                View custom_layout = inflater.inflate(R.layout.custom_toast, 
                (ViewGroup) findViewById(R.id.custom_layout));

                //  get the textview and set_text                
                TextView textView = (TextView) custom_layout.findViewById(R.id.text);
                textView.setText("You Clicked On Custom Toast Button");

                //  get toast and set toast                
                Toast custom_toast = new Toast(getApplicationContext());
                custom_toast.setDuration(Toast.LENGTH_LONG);
                custom_toast.setView(custom_layout);
                custom_toast.show();

            }
        });

    }
}

Output:





Download project here 



Thank you!!!
Please like and share...


Comments