Android Toast
With code
// create instance
Toast toast = Toast.makeText(context, text, duration);
// set message color
TextView textView= (TextView) toast.getView().findViewById(android.R.id.message);
textView.setTextColor(Color.YELLOW);
// set background color
toast.getView().setBackgroundColor(getResources().getColor(R.color.indigo));
With custom view
I. Declare your custom view inside of any
layout.xml
file.<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/txtMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableStart="@drawable/ic_report_problem"
android:drawablePadding="8dp"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:gravity="center"
android:textColor="@android:color/white"
android:textSize="16dp"
android:text="No connection."
android:background="@color/indigo"/>
II. Set your custom view to
Toast
via setView()
method.// create instance
Toast toast = new Toast(getApplicationContext());
// inflate custom view
View view = getLayoutInflater().inflate(R.layout.toast_view, null);
// set custom view
toast.setView(view);
// set duration
toast.setDuration(Toast.LENGTH_LONG);
// set position
int margin = getResources().getDimensionPixelSize(R.dimen.toast_vertical_margin);
toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_VERTICAL, 0, margin);
// show toast
toast.show();
Comments
Post a Comment