1

I did my toast like this:

Currently Showing Toast Like Below -

Showed toast

Toast toast  = Toast.makeText(MainActivity.this,"Row Inserted.",Toast.LENGTH_SHORT);
  View view = toast.getView();
  view.setBackgroundColor(COlor.GREEN);
  toast.show();

But i want to do something like this:

I want to do this:

Al-Amin
  • 1,369
  • 1
  • 9
  • 26
  • 1
    view.setBackgroundColor(COlor.GREEN);//set drawable xml [please check and create xml file inside drawable and set in backgound](https://stackoverflow.com/questions/18781902/rounded-corner-for-textview-in-android) – Raju Tukadiya Jan 08 '20 at 09:17

3 Answers3

2

create shape in drawble like this

<?xml version="1.0" encoding="UTF-8"?>

<gradient
    android:angle="90"
    android:endColor="#FFC107"
    android:startColor="#FFC107"/>
<corners
    android:radius="40dp"/>

and then access it like this like

Toast toast  = Toast.makeText(AppMenusActivity.this,"Row Inserted.",Toast.LENGTH_SHORT);
    View view = toast.getView();
    view.setBackground(getResources().getDrawable(R.drawable.btngradient));
    toast.show();
Black mamba
  • 462
  • 4
  • 15
0

You can make a custom Toast. Please refer bellow example code

This is java code.

LayoutInflater inflater = SelectCyclesActivity.this.getLayoutInflater();
        View layout = inflater.inflate(R.layout.layout_toast,
                (ViewGroup) SelectCyclesActivity.this.findViewById(R.id.custom_toast_container));

        TextView text = (TextView) layout.findViewById(R.id.text);
        text.setSimpleCustomText(sMessage, Constants.SECOND_FONT);

        Toast toast = new Toast(SelectCyclesActivity.this);
        toast.setGravity(Gravity.BOTTOM, 0, 60);

        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(layout);
        toast.show();

This is an XML file layout_toast.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_toast_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/toast_bg"
    android:orientation="vertical">


    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/_10sdp"
        android:layout_marginRight="@dimen/_10sdp"
        android:padding="@dimen/_12sdp"
        android:gravity="center"
        android:textColor="#FFF"
        android:textStyle="bold" />

</LinearLayout>

This is a rounded shape for toast background. toast_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid
        android:width="@dimen/_2sdp"
        android:color="@color/colordrakGray" />
    <corners android:radius="30dp" />
</shape>
MehulGohil
  • 114
  • 2
  • 10
0

Here is the easiest way and more precise -

First, call this method in your java code -

private void toastIconSuccess() {
        Toast toast = new Toast(getApplicationContext());
        toast.setDuration(Toast.LENGTH_LONG);

        //inflate view
        View custom_view = getLayoutInflater().inflate(R.layout.toast_icon_text, null);
        ((TextView) custom_view.findViewById(R.id.message)).setText("Success!");
        ((ImageView) custom_view.findViewById(R.id.icon)).setImageResource(R.drawable.ic_done);
        ((CardView) custom_view.findViewById(R.id.parent_view)).setCardBackgroundColor(getResources().getColor(R.color.green_500));

        toast.setView(custom_view);
        toast.show();
    }

And Then create this custom layout - with toast_icon_text name in your layout folder -

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/parent_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical"
    app:cardBackgroundColor="#263238"
    app:cardCornerRadius="23dp"
    app:cardElevation="0dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:paddingBottom="10dp"
        android:paddingLeft="18dp"
        android:paddingRight="18dp"
        android:paddingTop="10dp">

        <ImageView
            android:id="@+id/icon"
            android:layout_width="18dp"
            android:layout_height="18dp"
            android:tint="@android:color/white"
            app:srcCompat="@drawable/ic_close" />

        <View
            android:layout_width="10dp"
            android:layout_height="0dp" />

        <TextView
            android:id="@+id/message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:maxLines="1"
            android:singleLine="true"
            android:text="Error Toast"
            android:textColor="#f2f2f2" />

        <View
            android:layout_width="10dp"
            android:layout_height="0dp" />

    </LinearLayout>

</android.support.v7.widget.CardView>

Change colors and icons as your choice.

Al-Amin
  • 1,369
  • 1
  • 9
  • 26