info@thistimebd.com

Tuesday 14th of May 03:57:26am

Custom Toast Android App Studio Source Code

package com.thistimebd.web.customtoast;


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.Button;

import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

    private Button loginButton;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        loginButton=(Button) findViewById(R.id.loginButtonId);


        loginButton.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                LayoutInflater inflater=getLayoutInflater();

                View customView = inflater.inflate(R.layout.customtoast_layout, (ViewGroup) findViewById(R.id.customtoast_id));

                Toast toast=new Toast(MainActivity.this);

                toast.setDuration(Toast.LENGTH_SHORT);

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

                toast.setView(customView);

                toast.show();


            }

        });

    }

}

=====================


<resources>

    <string name="app_name">CustomToast</string>

    <string name="button_text">Login</string>

    <string name="toast_text">You have clicked login button</string>

</resources>

===============================


acivity_main.xml

----------------


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

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

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:padding="20dp"

    tools:context="com.thistimebd.web.customtoast.MainActivity">


    <Button

        android:id="@+id/loginButtonId"

        android:layout_height="wrap_content"

        android:layout_width="wrap_content"

        android:text="@string/button_text"

        android:textSize="20sp"

        android:textStyle="bold"


        />




</LinearLayout>

=============================================================

customtoast_layout.xml

----------------------

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

<LinearLayout

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

    android:layout_width="match_parent"

    android:layout_height="match_parent"

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


    <ImageView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:src="@mipmap/ic_launcher_round"


        />

    <TextView

        android:layout_margin="10dp"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/toast_text"

        android:textSize="20sp"/>


</LinearLayout>