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 [Link] class is the
subclass of [Link] 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:
[Link](getApplicationContext(),"Hello Javatpoint",Toast.LENGTH_SHORT).show();
Toast toast=[Link](getApplicationContext(),"Hello Javatpoint",Toast.LENGTH_SHORT);
[Link](50,50);
[Link]();
Activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
xmlns:tools="[Link]
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>
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
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) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
// layout instances
firstname = (EditText)findViewById([Link]. etfname);
lastname = (EditText)findViewById([Link]. etlname);
password = (EditText)findViewById([Link]. etpassword);
email = (EditText)findViewById([Link]. etemail);
buttonSubmit = (Button) findViewById([Link]);
radioGroup = (RadioGroup) findViewById([Link]);
cb1 = (CheckBox) findViewById([Link].cb1);
cb2 = (CheckBox) findViewById([Link].cb2);
cb3 = (CheckBox) findViewById([Link].cb3);
cb4 = (CheckBox) findViewById([Link].cb4);
/*
Submit Button
*/
[Link](new [Link]() {
@Override
public void onClick(View v) {
String fname,lname, em, ps, txt, chk;
//get the value of firstname textbox
fname= [Link]().toString();
//get the value of lastname textbox
lname= [Link]().toString();
//get the value of password textbox
ps= [Link]().toString();
//get the value of email textbox
em= [Link]().toString();
// get the selected RadioButton of the group
selectedRadioButton = (RadioButton)findViewById([Link]());
//get RadioButton text
txt = [Link]().toString();
String checkBoxChoices = "";
if ([Link]()) {
checkBoxChoices += [Link]().toString();
}
if ([Link]()) {
checkBoxChoices += [Link]().toString();
}
if ([Link]()) {
checkBoxChoices += [Link]().toString();
}
if ([Link]()) {
checkBoxChoices += [Link]().toString();
}
// display it as Toast to the user
[Link]([Link], “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();
}
});
}
}