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,
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();
}
}
- appcompat_v7 errors after updates to API level 21 Material Theme
- adb: The Android Debug Bridge and Commands
- The Android Virtual Device myEmulator is currently running an emulator and cannot be deleted.
- How to make TextView Text Transparent [Android]
- Android is starting optimizing... app 1 of 1
- How to make Text in TextView bold and italic in Android
- Android Development - How to switch between two Activities
- Android : IOException: Unable to open sync connection!
- Android Toolbar example with appcompat_v7 21
- How to Change Android Title Bar Color?
- How to Add Padding to Android TextView
- JavaScript : redirect page to other url
- Programmatically Send an Email from Android App using Intent
- What is Android Toast.LENGTH_SHORT and Toast. LENGTH_LONG durations
- [Soluiton] You already have the latest version of Android Studio installed
- Detect swipes on Android Activity
- Add Buttons at the bottom of Android Layout xml file
- Android Lint app_name is not translated in af (Afrikaans) am (Amharic) ar (Arabic) bg (Bulgarian)
- Hide Navigation Bar from Android Screen Activity
- Android Disable EditText from Auto Focus on Activity load
- Toast not getting displayed Android App
- How to Enable Developers Option in Android Phones Settings
- Android : Duplicate registration for activity com.example.abc
- Android ListView turns Black or Flickers while Scrolling
- Android Studio Ctrl Shift o auto import not working
- Jupyter Notebook: 500 Internal Server Error - nbconvert failed: xelatex not found on PATH - Python
- Python Hello World! Program with code example (snippet) - Python
- How to check the size of a Docker Container - Docker
- Adding internet permission to Android Project - Android
- Program 9: Divide Two Numbers - 1000+ Python Programs - Python-Programs
- Create React App using npm command with TypeScript - JavaScript
- Read a File Line By Line using Java 8 Stream API - Java
- Android Error Generating Final Archive - Debug Certificate Expired - Android