You must have seen that by default we get a dark greyish kind of background color for a toast message that is displayed in Android Activity Screen (may vary depending upon what flavor of Android OS you are using). If you want to develop a custom Toast message with a color combination that suits your app design then we can do it programmatically using setBackgroundResource() provided by the Toast Class. Let's see an example, we will change the background color to Yellow and the toast text color to white,

Custom toast color
We need to create a drawable XML file to achieve this, we will create toast_drawable.xml and store it in the any of the drawable folders under res. We will add a solid tag to set the background color.
File : toast_drawable.xml<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#ffff00" />
</shape>
File : MainActivity.java
package com.code2care.toast;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void displayToast(View view) {
Toast toast = Toast.makeText(getApplicationContext(),
"Custom toast background color",
Toast.LENGTH_SHORT);
View toastView = toast.getView();
toastView.setBackgroundResource(R.drawable.toast_drawable);
toast.show();
}
}
More Posts related to Android,
- Android : Execute some code after back button is pressed
- Android is starting optimizing... app 1 of 1
- How to change Android Button Color using xml attribute and programatically using java
- SQLite with Android Easy to Understand Tutorial that covers Select, Insert, Update and Delete
- How to make Android EditText not editable
- Make Android TextView Clickable like Buttons
- Unable to establish connection to adb : Android Studio Error
- Android Constant and Resource Type Mismatches Lint
- Android Shared Preferences API tutorial
- Fixing Android unknown error 961 while downloading app
- DDMS files not found hprof-conv.exe
- How to reset eclipse layout
- Android Developers Bluetooth Tutorial
- Android Studio Native typeface cannot be made error
- 21 Useful Android Emulator Short-cut Keyboard Keys
- Multiline EditText in Android Example
- How to screenshot on Android?
- JavaScript : redirect page to other url
- Unable to load VM from snapshot. The snapshot has been saved for a different hardware configuration
- How To Disable Landscape Mode in Android Application
- Change Title text for Android Activity using java code
- Android : Class file collision: A resource exists with a different case
- Android Emulator Soft Back button action using Computer keyboard
- Device not compatible error Android Google Play Store
- ERROR x86 emulation currently requires hardware acceleration. Intel HAXM is not installed on this machine
- java.lang.NoClassDefFoundError android.support.v4.content.LocalBroadcastManager
- Disable Fading Edges Scroll Effect Android Views
- Android : Unable to load VM from snapshot : Mac OS X Error
- Change Android EditText Cursor Height
- What is Android Toast.LENGTH_SHORT and Toast. LENGTH_LONG durations
- How to add border to Android TextView
- Remove ActionBar from Activity that extends appcompat-v7
- How to send SMS on Android Emulator
- Android RatingBar Example
- This Toast was not created with Toast.makeText() : Android RuntimeException
- Android Studio Ctrl Shift o auto import not working
- Android : Duplicate registration for activity com.example.abc
- ADT Installation Error: requires plug-in org.eclipse.wst.sse.ui
- Running Android Lint has encountered a problem NullPointerException Error
- Android Emulator] ##KBD: Full queue, lose event Error Logs
- Export aborted because fatal lint error were found
- Android Parsing Data for android-L failed Unsupported major.minor version 51.0 Error
- Android : Accidental Octal Lint Warning
- Android ListView turns Black or Flickers while Scrolling
- How to make a dummy phone call from Android Emulator device
More Posts:
- Android : DeviceMonitor] Sending Tracking request failed! Error - Android
- Cut, Copy and Paste commands for mac - MacOS
- How to make Text in TextView bold and italic in Android - Android
- Android Parsing Data for android-L failed Unsupported major.minor version 51.0 Error - Android
- Setting Java_Home Environment variable on Windows Operating System - Java
- Share Image to WhatsApp with Caption from your Android App - WhatsApp
- AlertDialog with image using ImageView Example - Android
- SharePoint PowerShell - How to get list item by ID using GetItemById - SharePoint
- How to add border to Android TextView - Android
- Delete Android Studio Projects - Android-Studio
- Submit html form on dropdown menu value selection or change using javascript - JavaScript
- list of jars required for hibernate 4.x.x - Java
- How to Change Text Size for Android ActionBar - Android
- Eclipse : A java Runtime Environment (JRE) or Java Development kit (JDK) must be available - Java
- SharePoint List redirect user after submitting form NewForm.aspx - SharePoint