info@thistimebd.com

Thursday 21st of November 04:17:51pm

How to create android app Toast ?

MainActivity.java

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


package com.thistimebd.web.myapplication2;


import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

import android.widget.Toast;


public class MainActivity extends AppCompatActivity {



    private Button loginButton, logoutButton;

    private TextView textView;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

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

        logoutButton = (Button) findViewById(R.id.logoutButtonId);

        textView = (TextView) findViewById(R.id.texViewId);




    }


    public void showMessage(View v){

        if(v.getId()==R.id.loginButtonId){

            Toast.makeText(MainActivity.this,"Login Button is clicked", Toast.LENGTH_SHORT).show();



        }



        if(v.getId()==R.id.logoutButtonId){


            Toast.makeText(MainActivity.this,"Logout Button is clicked", Toast.LENGTH_SHORT).show();



        }



    }


}

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


package com.thistimebd.web.myapplication2;


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

import android.widget.Toast;


public class MainActivity extends AppCompatActivity {



    private Button loginButton, logoutButton;

    private TextView textView;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

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

        logoutButton = (Button) findViewById(R.id.logoutButtonId);

        textView = (TextView) findViewById(R.id.texViewId);




    }


    public void showMessage(View v){

        if(v.getId()==R.id.loginButtonId){

            Toast toast=Toast.makeText(MainActivity.this,"Login Button is clicked",Toast.LENGTH_SHORT);

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

            toast.show();




        }



        if(v.getId()==R.id.logoutButtonId){

            Toast toast=Toast.makeText(MainActivity.this,"Logout Button is clicked",Toast.LENGTH_SHORT);

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

            toast.show();





        }



    }


}

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

Login Output in android

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

 }


    public void showMessage(View v){

        if(v.getId()==R.id.loginButtonId){

            Log.d("tag","Login button is clicked");





        }



        if(v.getId()==R.id.logoutButtonId){

            Log.d("tag","Logout button is clicked");





        }