Thursday, October 18, 2018

Make TRANSPARENT Toast

It's a simple way to make TRANSPARENT Toast.

Java
package com.blogspot.android_er.mycustomtoast;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        displayMyToast();
    }

    private void displayMyToast(){
        Toast myToast = Toast.makeText(MainActivity.this,
                "I'm a TRANSPARENT Toast",
                Toast.LENGTH_LONG);
        View view = myToast.getView();
        view.setBackgroundColor(Color.TRANSPARENT);
        myToast.show();
    }
}


Kotlin, generated in Android Studio by menu > Code > Convert Java file to Kotlin File.
package com.blogspot.android_er.mycustomtoast

import android.graphics.Color
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.Toast

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        displayMyToast()
    }

    private fun displayMyToast() {
        val myToast = Toast.makeText(this@MainActivity,
                "I'm a TRANSPARENT Toast",
                Toast.LENGTH_LONG)
        val view = myToast.view
        view.setBackgroundColor(Color.TRANSPARENT)
        myToast.show()
    }
}




Next:
- Create custom Toast with layout XML

1 comment:

Anonymous said...

perfect as usual. Thank you android-er.