Wednesday, October 25, 2017

Android studio - Toast Position -Code

mainActivity.java - code of CustomToastLocation

 package com.example.raghukm.customtoastlocation;

 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.view.Gravity;
 import android.view.View;
 import android.widget.Button;
 import android.widget.Toast;

 public class MainActivity extends AppCompatActivity {
     Button btn1;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         btn1 = (Button)findViewById(R.id.button);
     }
     public void btnClick(View view){
         Toast toast1= Toast.makeText(this, "Gravity.CENTER",Toast.LENGTH_SHORT);
          toast1.setGravity(Gravity.CENTER,0,0);
         toast1.show();

         Toast toast2= Toast.makeText(this, "Gravity.BOTTOM",Toast.LENGTH_SHORT);
         toast2.setGravity(Gravity.BOTTOM,0,0);
         toast2.show();

         Toast toast3= Toast.makeText(this, "Gravity.TOP",Toast.LENGTH_SHORT);
         toast3.setGravity(Gravity.TOP,0,0);
         toast3.show();

         Toast toast4= Toast.makeText(this, "Gravity.CENTER AND LEFT",Toast.LENGTH_SHORT);
         toast4.setGravity(Gravity.CENTER |Gravity.LEFT,0,0);
         toast4.show();

         Toast toast5= Toast.makeText(this, "Gravity.CENTER AND BOTTOM",Toast.LENGTH_SHORT);
         toast5.setGravity(Gravity.CENTER |Gravity.BOTTOM,0,0);
         toast5.show();

         Toast toast6= Toast.makeText(this, "Gravity.CENTER AND RIGHT",Toast.LENGTH_SHORT);
         toast6.setGravity(Gravity.CENTER |Gravity.RIGHT,0,0);
         toast6.show();
     }
 }


activity_main.xml - code of CustomToastLoacation

  <?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.customtoastlocation.MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="333dp"
        android:layout_height="52dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="136dp"
        android:onClick="btnClick"
        android:text="CLICK HERE"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    </android.support.constraint.ConstraintLayout>




SHARE THIS

Author:

0 Post a Comment: