Monday, 4 September 2017

Custom Toast display in Android

About Example:- Here is the example that "How to Display Custom Toast   in Android Application By using Coding"

Basic Requirement:-
1. MainActivity.java
2. activity_main.xml

Steps are:-
Step 1: Add a new project and open activity_main.xml and write following code:-

<?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:orientation="vertical"
    android:gravity="center"
    tools:context="com.example.supriyabharti.launchgooglemapexmp.MainActivity"
>

   
    <Button
        android:id="@+id/btn_cus_toast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Custom Intent"
        android:padding="5dp"
        android:background="@drawable/btn_design"
        android:textColor="#fff"
/>

</LinearLayout>
Step 2:- Go to layout->  and a new layout with name "custom_toast.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:gravity="center"
>
    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"
/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="I Like AndroidHubb"
        android:textColor="#000"
        android:textSize="20sp"
        android:textStyle="bold"
/>

</LinearLayout>

Step 3:- Open MainActivity and write following code:-

package com.example.supriyabharti.launchgooglemapexmp;

import android.content.Intent;
import android.net.Uri;
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 {
    Button btnCusIntent;
    View viewLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn=(Button)findViewById(R.id.btn_map);
        btnFb=(Button)findViewById(R.id.btn_share_fb);
        btnCusIntent=(Button)findViewById(R.id.btn_cus_toast);
        LayoutInflater layoutInflater=getLayoutInflater();
        viewLayout=layoutInflater.inflate(R.layout.custom_toast,null);
        btnCusIntent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast t=Toast.makeText(getApplicationContext(),"Custom Toast",Toast.LENGTH_LONG);
                t.setGravity(Gravity.BOTTOM,0,0);
                t.setView(viewLayout);
                t.show();

            }
        });
    }
}

Output are like:-

No comments:

Post a Comment