Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make custom toast in android.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 147
    Comment on it

    In the below example I have created a custom toast Function in Android, the toast will open in center of the screen. Here I have used a image, image shown below contains a toast at the bottom of the screen. For more see the below code, in which I have described how to make custom toast in android.

    Step(1)-MainActivity-

    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button click = (Button)findViewById(R.id.button1);
            click.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    LayoutInflater inflater = getLayoutInflater();
                    View layout = inflater.inflate(R.layout.toast_layout,
                                                   (ViewGroup) findViewById(R.id.custom_toast_layout));
    
                        text.setText("Welcome In Evon ");
    
                    Toast toast = new Toast(getApplicationContext());
                    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                    toast.setDuration(Toast.LENGTH_LONG);
                    toast.setView(layout);
                    toast.show();
                }
            });
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
    }
    

    Step(2)-Main.xml.Layout-

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/custom_toast_layout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#DAAA"
        android:orientation="horizontal"
        android:padding="8dp" >
    
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="8dp"
            android:src="@drawable/ic_launcher" />
    
        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/black" />
    
    </LinearLayout>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: