0% found this document useful (0 votes)
47 views6 pages

Toast

The document describes Toast in Android, which displays brief notifications or messages on screen for a short period of time. It discusses the Toast class, its constants like LENGTH_LONG and LENGTH_SHORT, methods like makeText() and show(), and provides an example of creating and displaying a Toast notification.

Uploaded by

Kami
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views6 pages

Toast

The document describes Toast in Android, which displays brief notifications or messages on screen for a short period of time. It discusses the Toast class, its constants like LENGTH_LONG and LENGTH_SHORT, methods like makeText() and show(), and provides an example of creating and displaying a Toast notification.

Uploaded by

Kami
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Toast

Andorid Toast can be used to display information for the short period of time. A toast contains
message to be displayed quickly and disappears after sometime. The android.widget.Toast class is the
subclass of java.lang.Object class.

Toast class

Toast class is used to show notification for a particular interval of time. After sometime it disappears.
It doesn't block the user interaction.

Constants of Toast class


There are only 2 constants of Toast class which are given below.

Constant Description

public static final int LENGTH_LONG displays view for the long duration of time.

public static final int LENGTH_SHORT displays view for the short duration of time.

Methods of Toast class


The widely used methods of Toast class are given below.

Method Description

public static Toast makeText(Context context, makes the toast containing text and duration.
CharSequence text, int duration)

public void show() displays toast.

public void setMargin (float horizontalMargin, changes the horizontal and vertical margin
float verticalMargin) difference.

Toast Example
Example1:
Toast.makeText(getApplicationContext(),"Hello Javatpoint",Toast.LENGTH_SHORT).show();

Toast toast=Toast.makeText(getApplicationContext(),"Hello Javatpoint",Toast.LENGTH_SHORT);


toast.setMargin(50,50);
toast.show();
Activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<TextView
android:id="@+id/txtview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Name" />

<EditText
android:id="@+id/etfname"
android:layout_width="40dp"
android:layout_height="60dp"
/>

<TextView
android:id="@+id/txtview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Last Name" />

<EditText
android:id="@+id/etlname"
android:layout_width="40dp"
android:layout_height="60dp"
/>

<TextView
android:id="@+id/txtview3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password" />

<EditText
android:id="@+id/etpassword"
android:layout_width="40dp"
android:layout_height="60dp"
/>
<TextView
android:id="@+id/txtview4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email" />
<EditText
android:id="@+id/etemail"
android:layout_width="40dp"
android:layout_height="60dp"
/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/radioGroup" >

<RadioButton
android:id="@+id/rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EXCELLENT"
android:checked="false" />

<RadioButton
android:id="@+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GOOD"
android:checked="true" />

<RadioButton
android:id="@+id/rb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OKAY"
android:checked="false" />

<RadioButton
android:id="@+id/rb4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="POOR"
android:checked="false" />

</RadioGroup>

<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" I really enjoy this lesson."
android:id="@+id/cb1"
android:checked="false"/>

<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="I will prefer this lesson over any other."
android:id="@+id/cb2"
android:checked="false"/>

<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="I would like to hear more from you."
android:id="@+id/cb3"
android:checked="false"/>

<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="I am satisfied with the content and full description."
android:id="@+id/cb4"
android:checked="false"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SUBMIT"
android:id="@+id/btnSubmit" />

</RelativeLayout>
MainActivity.java
package com.example.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.EditText;
import android.widget.CheckBox;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

// These are the global variables


RadioGroup radioGroup;
RadioButton selectedRadioButton;
Button buttonSubmit;
EditText firstname;
EditText lastname;
EditText email;
EditText password;
CheckBox cb1, cb2, cb3, cb4;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// layout instances

firstname = (EditText)findViewById(R.id. etfname);


lastname = (EditText)findViewById(R.id. etlname);
password = (EditText)findViewById(R.id. etpassword);
email = (EditText)findViewById(R.id. etemail);
buttonSubmit = (Button) findViewById(R.id.buttonSubmit);
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
cb1 = (CheckBox) findViewById(R.id.cb1);
cb2 = (CheckBox) findViewById(R.id.cb2);
cb3 = (CheckBox) findViewById(R.id.cb3);
cb4 = (CheckBox) findViewById(R.id.cb4);

/*
Submit Button
*/
buttonSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

String fname,lname, em, ps, txt, chk;

//get the value of firstname textbox


fname= firstname.getText().toString();
//get the value of lastname textbox
lname= lastname.getText().toString();
//get the value of password textbox
ps= password.getText().toString();
//get the value of email textbox
em= email.getText().toString();

// get the selected RadioButton of the group


selectedRadioButton = (RadioButton)findViewById(radioGroup.getCheckedRadioButtonId());
//get RadioButton text
txt = selectedRadioButton.getText().toString();
String checkBoxChoices = "";

if (cb1.isChecked()) {
checkBoxChoices += cb1.getText().toString();
}
if (cb2.isChecked()) {
checkBoxChoices += cb2.getText().toString();
}
if (cb3.isChecked()) {
checkBoxChoices += cb3.getText().toString();
}
if (cb4.isChecked()) {
checkBoxChoices += cb4.getText().toString();
}

// display it as Toast to the user


Toast.makeText(MainActivity.this, “First Name is: ”+fname +”\n Last Name is:” + laname +”\n
Password is:” +ps + “\n Email is :” + em + "\n Selected Radio Button is: " + txt + "\n CheckBox Choices: "+
checkBoxChoices , Toast.LENGTH_LONG).show();
}
});
}
}

You might also like