Pages

Chapter 6.2: Colored Toast

Colored Toast in Android

In this tutorial we will learn how to create a Colored Toast with a simple example in Android Studio. Just we have to get a Textview and set the view of toast in that Textview and show it.

Example:
  • Create a new project in Android Studio or just design a Button in the previous project.
  • Design a Button in an activity_main.xml file, as follows.
 File: activity_main.xml:

<LinearLayout 
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:orientation="vertical"    
android:gravity="center"    
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.toastinandroid.MainActivity">

<Button       
android:id="@+id/button"       
android:text="Click to show Toast"       
android:layout_gravity="center_vertical"       
android:layout_width="match_parent"       
android:layout_height="wrap_content" />

<Button        
android:id="@+id/custom_button"        
android:text="Click to show custom Toast"        
android:layout_gravity="center_vertical"        
android:layout_width="match_parent"        
android:layout_height="wrap_content" />

<Button        
android:id="@+id/color_toast"        
android:text="Click to show color toast"        
android:layout_width="match_parent"        
android:layout_height="wrap_content" />

</LinearLayout>
  • Set on click listener to the button and show the Toast in onClick, as follows.
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.makeText(getApplicationContext(), "You clicked a Button", Toast.LENGTH_LONG).show();
            }
        });


        Button custom_button = (Button) findViewById(R.id.custom_button);
        custom_button.setOnClickListener(new View.OnClickListener() {
            @Override            

                public void onClick(View v) {
                String str = ("You have clicked a custom Button");
                //  inflating the layout                

                LayoutInflater inflater = getLayoutInflater();
                View layout = inflater.inflate(R.layout.custom_toast, (ViewGroup) findViewById(R.id.custom_layout));

                //  Display text in Textview                

                 TextView textView = (TextView) layout.findViewById(R.id.text_custom);
                textView.setText(str);

                //  Displaying the inflated layout in toast                Toast custom_toast = new Toast(getApplicationContext());
                custom_toast.setDuration(Toast.LENGTH_LONG);
                custom_toast.setView(layout);
                custom_toast.show();


            }
        });

        Button color_button = (Button) findViewById(R.id.color_toast);
        color_button.setOnClickListener(new View.OnClickListener() {
            @Override            

                public void onClick(View v) {
                Toast toast = Toast.makeText(getApplicationContext(), "You have clicked a color Button", Toast.LENGTH_LONG);
                TextView textView = (TextView) toast.getView().findViewById(android.R.id.message);
                textView.setTextColor(Color.RED);
                toast.show();
            }
        });
    }
}

  • Now run the Application.
Output:

Download full project here



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


Unknown

Author: Nouman Baig

Editor: Mohammad Jabir

No comments:

Post a Comment