Toast
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.
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.
Method Description
public static Toast makeText(Context context, makes the toast containing text and duration.
CharSequence text, int duration)
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();
<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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// layout instances
/*
Submit Button
*/
buttonSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
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();
}