Toasts in Android

Yugandhar
Android App Development for beginners
3 min readJul 21, 2016
  • A toast provides simple feedback about an operation in a small popup.
  • A toast is a view containing a quick little message for the user
  • It only fills the amount of space required for the message and the current activity remains visible and interactive.

Creating a Toast

  • First, instantiate a Toast object with one of the makeText() methods
  • This method takes three parameters: Context, text message, duration for the toast.
Toast.makeText(context, text, duration).show();Example
Toast.makeText(getApplicationContext(),"Hello Toast",Toast.SHORT_LENGTH).show();

Simple code snippet for creating a toast

layout.xml<LinearLayout
android:orientation="vertical"
tools:context="com.customizationtoastapp.CustomizationToastActivity">
<Button
android:id="@+id/bt_customizationToast"
android:text="Toast Customization"/>
</LinearLayout>
Activity.xmlpublic class CustomizationToastActivity extends AppCompatActivity implements View.OnClickListener {
private Button btCustomizationToast;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customization_toast);
setUpViews();
}
private void setUpViews() {
btCustomizationToast = (Button) findViewById(R.id.bt_customizationToast);

btCustomizationToast.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.bt_customizationToast:
Toast toast= Toast.makeText(getApplicationContext(),"Hello",Toast.LENGTH_SHORT);
toast.show();
break;
}
}

####OUTPUT

Positioning your Toast

  • A standard toast notification appears near the bottom of the screen, centered horizontally.
  • You can change this position with the setGravity(int, int, int) method.

Use same layout we defined previously

Activity.java
public class CustomizationToastActivity extends AppCompatActivity implements View.OnClickListener {
private Button btCustomizationToast;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customization_toast);
setUpViews();
}
private void setUpViews() {
btCustomizationToast = (Button) findViewById(R.id.bt_customizationToast);
btCustomizationToast.setOnClickListener(this);

}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.bt_customizationToast:
Toast toast= Toast.makeText(getApplicationContext(),"Hello",Toast.LENGTH_SHORT);
toast.setGravity(Gravity.START|Gravity.AXIS_CLIP, 0, 500);
toast.show();
break;

}
}
}

###OUTPUT

Creating a Custom Toast View

  • Create a separate layout file for custom toast view
  • Use LayoutInflater for initializing the layout
  • set layout to Toast with the method setView(View)
layout_custom_toast.xml<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CustomToast"
android:textSize="30sp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_account_circle_black_24dp"
/>
</LinearLayout>
actvity_layout.xml<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.customizationtoastapp.CustomizationToastActivity">
<Button
android:id="@+id/bt_customizationToast_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toast Customization layout"/>
</LinearLayout>
Activity.xmlpublic class CustomizationToastActivity extends AppCompatActivity implements View.OnClickListener {

private Button btCustomizationToastLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customization_toast);
setUpViews();
}
private void setUpViews() {
btCustomizationToastLayout = (Button) findViewById(R.id.bt_customizationToast_layout);
btCustomizationToastLayout.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {

case R.id.bt_customizationToast_layout:
setToastLayoutout();
break;
}
}
private void setToastLayoutout() {
LayoutInflater layoutInflater = getLayoutInflater();
View toastLayout = layoutInflater.inflate(R.layout.layout_toast, null);
Toast toast = new Toast(getApplicationContext());
toast.setView(toastLayout);
toast.show();
}
}

####output

--

--