Create Basic Toast in Android Studio

1. Create a Blank Activity.
2. Naming the activity “HomeActivity.java”.
3. User Interface (drag and drop component are there) Named as “activity_home.xml”.
4. Drag and drop the “NEW BUTTON” on the screen and double click the button to set the button title and @id.
5. Declare the button variable and set the Event listener before we need to implements the View.OnClickListener.

import android.content.Context;
import android.widget.Button;
import android.widget.Toast;


public class HomeActivity extends AppCompatActivity implements View.OnClickListener{
Button btnClickMe;  // after apply the short to import the "widget.Button"

protected void onCreate(Bundle savedInstanceState) {  // system call to intimate the next lifecycle call
super.onCreate(savedInstanceState);  //The savedInstanceState is a reference to a Bundle object that is passed into the onCreate method of every Android Activity.
setContentView(R.layout.activity_home);  //call the particular activity

btnClickMe = (Button) findViewById(R.id.button);  // @id using to identify the button
btnClickMe.setOnClickListener(HomeActivity.this);  // add the event Listener to the button

}

//Here can you apply the shortcut to import the method automatically
@Override
    public void onClick(View view) {

   //Create Simple toast 
   Context context = getApplicationContext();
   CharSequence message = "First Toast and button click action";
   int duration = Toast.LENGTH_SHORT;
   final Toast toastBasic = Toast.makeText(context,message,duration);
   toastBasic.show();
    }

}


6. “Alt + Enter” to import the required class.

device-2016-07-13-010930

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s