Show a Toast Message | Android App Development Tutorial #1

Words
256
Reading
2 min
Listen
Play
6y
  • My GitHub Profile: https://github.com/uafappdevelopers

  • What will I Learn?

  • How to click on Button
  • Show a Toast message in Android

  • Requirements

  • Basic knowledge of JAVA and Android XML Layouts
  • Basic object oriented programming skills

  • Procedure

  • Open android studio and create a new Empty Activity project

  • empty Activity.png

    • Now open the activity_main.xml file as shown the tree structure here and click on split button to show the code and design at the same time.

    activity_main.png

    • Now replace the code as given here it include a Linear Layout and a Button inside it with the id message_btn
    <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"
        tools:context=".MainActivity"
        android:gravity="center">
    
      
        <Button
            android:id="@+id/message_btn"
        android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Show Message"/>
        
    
    </LinearLayout>
    
    
    • Open the MainActivity.java file as shown the RED line in this mockup

    MainActivity.png

    • Insert the Button click code after this line
    setContentView(R.layout.activity_main);
    

    here is the button click code

            //Its a code for a button click that we declare in activity_main.xml file
            
            findViewById(R.id.message_btn).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    
                    //Its a code to show the message on the screen
                    Toast.makeText(MainActivity.this, "Hello Hive", Toast.LENGTH_SHORT).show();
                }
            });
    
    
    • Picture of all code setup in MainActivity.java

    code.png

    • Now connect you mobile device and lets test the app make sure the Developer Option is on and the USB Debugging is enabled.
    • Click on the play button to run the app

    mobile_lg.png

    Final Result

    App on MobileButton Clicked
    Capture_20200701195341.pngCapture_20200701195401.png



    Follow | Upvote | Share

    About me

    Hello Friends I am Muhammad Faisal Amin. My country is Pakistan. nI live in a city of Pakistan named Faisalabad. I love my country.
    I am 21 years old in 2020.
    I study in Software Engineering in University of Agriculture Faisalabad Main Campus it also knows as UAF.
    Its a very beautiful University and the biggest University in Asia.
    I am also having experiences in MObile App Development using Java Android and Flutter also.
    Its my passion to develop quality app for both iPhone and Android Mobiles.
    IMG_20200423_180747.jpg


    My%20Video2.gif

    Show a Toast Message | Android App Development Tutorial #1 | Ecency