Tuesday, October 24, 2017

Android Studio - Custom Toast Code

mainActivity.java - code of CustomToast

 package com.example.raghukm.custum;

 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 btn;
     View viewlyt;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         btn=(Button)findViewById(R.id.button);
         LayoutInflater layoutInflater = getLayoutInflater();
         viewlyt=layoutInflater.inflate(R.layout.custom_toast,(ViewGroup)findViewById(R.id.custom_toast_layout));
     }

     public void btnClick(View view) {
         Toast toast=Toast.makeText(this,"Toast:Gravity.TOP",Toast.LENGTH_LONG);
         toast.setGravity(Gravity.CENTER,0,0);
         toast.setView(viewlyt);
         toast.show();
     }
 }


activity_main.xml - code of CustomToast

 <?xml version="1.0" encoding="utf-8"?>
 <android.support.constraint.ConstraintLayout 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"
     tools:context="com.example.raghukm.custum.MainActivity">

     <Button
         android:id="@+id/button"
         android:layout_width="319dp"
         android:layout_height="56dp"
         android:text="Click here"
         app:layout_constraintLeft_toLeftOf="parent"
         app:layout_constraintRight_toRightOf="parent"
         app:layout_constraintTop_toTopOf="parent"
         android:layout_marginTop="28dp"
         android:onClick="btnClick"
         app:layout_constraintHorizontal_bias="0.507" />
 </android.support.constraint.ConstraintLayout>
  

custom_toast.xml - code of Custom Toast

 <?xml version="1.0" encoding="utf-8"?>
 <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:id="@+id/custom_toast_layout"
     xmlns:tools="http://schemas.android.com/tools"
     android:orientation="vertical" android:layout_width="match_parent"
     android:layout_height="match_parent">

    <ImageView
         android:id="@+id/imageView"
         android:layout_width="172dp"
         android:layout_height="146dp"
         app:layout_constraintLeft_toLeftOf="parent"
         app:layout_constraintRight_toRightOf="parent"
         app:srcCompat="@drawable/logo"
         app:layout_constraintTop_toTopOf="parent"
         android:layout_marginTop="124dp" />

    <TextView
         android:id="@+id/textView2"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Welcome to TECH-SITE"
         android:textAlignment="center"
         android:textColor="@android:color/black"
         android:textSize="28dp"
         app:layout_constraintLeft_toLeftOf="parent"
         app:layout_constraintRight_toRightOf="parent"
         app:layout_constraintTop_toBottomOf="@+id/imageView" />

     <TextView
         android:id="@+id/textView3"
         android:layout_width="263dp"
         android:layout_height="30dp"
         android:layout_marginTop="50dp"
         android:text="Technology solutions"
         android:textAlignment="center"
         android:textColor="@android:color/holo_blue_dark"
         android:textSize="26dp"
         app:layout_constraintHorizontal_bias="0.504"
         app:layout_constraintLeft_toLeftOf="parent"
         app:layout_constraintRight_toRightOf="parent"
         app:layout_constraintTop_toBottomOf="@+id/imageView" />

 </android.support.constraint.ConstraintLayout>




SHARE THIS

Author:

0 Post a Comment: