Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6330049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:47:39+00:00 2026-05-24T17:47:39+00:00

in my application i use a custom toast in almost all the activity. To

  • 0

in my application i use a custom toast in almost all the activity. To create the custom toast i have the following method :

private void getCustomToast(String message)
{
    LayoutInflater li   = getLayoutInflater();
    View toastlayout    = li.inflate(R.layout.toast_error, (ViewGroup)findViewById(R.id.toast_layout));
    TextView text       = (TextView) toastlayout.findViewById(R.id.toast_text);
    text.setText(message);

    Toast toast = new Toast(this);
    toast.setDuration(Toast.LENGTH_LONG);
    toast.setView(toastlayout);
    toast.show();
}

It works fine but for each activity I need to duplicate this method, not really respectfull of the DRY principle …

How can i make a static class (for example) in which i have a method which gonna fire the custom toast on the current activity ?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T17:47:41+00:00Added an answer on May 24, 2026 at 5:47 pm

    You should make a custom abstract Activity that contains the toast method, and then extend that for your application’s activities:

    public abstract class ToastActivity extends Activity {
    
        protected void getCustomToast(String message)
        {
            LayoutInflater li = getLayoutInflater();
            View toastlayout  = li.inflate(
                    R.layout.toast_error, 
                    (ViewGroup) findViewById(R.id.toast_layout));
    
            TextView text = (TextView) toastlayout.findViewById(R.id.toast_text);
            text.setText(message);
    
            Toast toast = new Toast(this);
            toast.setDuration(Toast.LENGTH_LONG);
            toast.setView(toastlayout);
            toast.show();
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to use custom functions / objects in my web application based on
I have a c# application that I use a custom FTP library for. Right
I have a RESTful WCF application that makes use of custom classes as service
I'm trying to have my custom java application use our Active Directory Server for
i want to use custom fonts in y application. i have declared the Fonts
I want to use Custom URL Scheme to transfer data from application to other
I'm trying to use JRuby in a custom application, and I don't seem to
I'm using Visual Studio to create my installation package. My application use some bitmap
I am required to use custom fonts in my application. Problem: For ListViews that
I'm trying to use custom notifications in my Android application using Google's sample from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.