0

I want to use this in lots of places in my code and there will be a lot of repetition, but my knowledge in java is not sufficient enough to make this work.

        Toast myToast = Toast.makeText(net.asdqwe.activities.Signup.this, configurationz.ERROR_MESSAGES_SIGNUP_USER_NAME_MIN_LENGTH_PROBLEM, Toast.LENGTH_SHORT);
        myToast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0);
        TextView tv = (TextView) myToast.getView().findViewById(android.R.id.message);
        tv.setTextColor(Color.parseColor(configurationz.COLORS_TOAST_TEXT_COLOR));
        tv.setTextSize(20);
        myToast.getView().setBackgroundColor(Color.parseColor(configurationz.COLORS_TOAST_BACKGROUND));
        myToast.show();

I wanna be able to use it this way:

ToastMaker(short duration (//or long), configurationz.ERROR_MESSAGE (//of my choice), configurationz.COLORS_TOAST_TEXT_COLOR(//or some other variable), configurationz.COLORS_TOAST_BACKGROUND_COLOR(//or some other variable), 30(//text size), gravity)

something like this
ToastMaker(length, errorMessage, textColor, backgroundColor, textSize, gravity)

the one thing that concerns me the most is that the following piece of code is going to change for every class, and I do not know how to obtain that dynamically

net.asdqwe.activities.Signup.this

Actually I can make the text color, size and background a general setting for the entire app (which makes sense), so we are left with this:

ToastMaker(length, errorMessage, gravity)

as the final desired result

EDIT: I've answered my question with the working code, that I generated after reading all the answers

Kaloyan Roussev
  • 14,515
  • 21
  • 98
  • 180
  • replace `net.asdqwe.activities.Signup.this` with a parameter of type `Context` and let each place that calls the method put in e.g. `net.asdqwe.activities.Signup.this` – zapl Sep 27 '13 at 10:14
  • Re-factor the code such that all the common repeating code could go into methods, and invoke the methods via parameters. – Gyanendra Dwivedi Sep 27 '13 at 10:18
  • Im sorry guys, these explanations are very abstract to me :( – Kaloyan Roussev Sep 27 '13 at 10:22

5 Answers5

1

if want to declare it in saperet class the

class YourClass{

  public void showToast(Context context){

    Toast myToast = Toast.makeText(context, configurationz.ERROR_MESSAGES_SIGNUP_USER_NAME_MIN_LENGTH_PROBLEM, Toast.LENGTH_SHORT);
    myToast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0);
    TextView tv = (TextView) myToast.getView().findViewById(android.R.id.message);
    tv.setTextColor(Color.parseColor(configurationz.COLORS_TOAST_TEXT_COLOR));
    tv.setTextSize(20);
    myToast.getView().setBackgroundColor(Color.parseColor(configurationz.COLORS_TOAST_BACKGROUND));
    myToast.show();
  }
}

use in other class like this

YourClass myClass=new YourClass();
myClass.showToast(mContext);

You can also pass other parameter with context (e.g. message).

Ketan Ahir
  • 6,678
  • 1
  • 23
  • 45
1

How about make one static method like:

public static void ToastMaker(length, errorMessage, textColor, backgroundColor, textSize, gravity) 

Need to add context as parameter though. Just put your code in that method and that about it. You may even use custom layout

refer to this link: Custom toast message in all screens?

Hope this makes it bit more clear. Regards

Community
  • 1
  • 1
Darko Rodic
  • 1,010
  • 3
  • 10
  • 27
1

Do this way

I putted in runOnUi() method so you can call it from Asynctask/background thread

For long time

 public void tong(Context mContext, final String msg) {
            ((Activity)mContext).runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    Toast myToast = Toast.makeText(mContext, msg, Toast.LENGTH_LONG);
                    myToast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0);
                    TextView tv = (TextView) myToast.getView().findViewById(android.R.id.message);
                    tv.setTextColor(Color.parseColor(configurationz.COLORS_TOAST_TEXT_COLOR));
                    tv.setTextSize(20);
                    myToast.getView().setBackgroundColor(Color.parseColor(configurationz.COLORS_TOAST_BACKGROUND));
                    myToast.show();
                }
            });
        }

For short time

public void ting(final Context mContext, final String msg) {

            ((Activity)mContext).runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    Toast myToast = Toast.makeText(mContext, msg, Toast.LENGTH_SHORT);
                    myToast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0);
                    TextView tv = (TextView) myToast.getView().findViewById(android.R.id.message);
                    tv.setTextColor(Color.parseColor(configurationz.COLORS_TOAST_TEXT_COLOR));
                    tv.setTextSize(20);
                    myToast.getView().setBackgroundColor(Color.parseColor(configurationz.COLORS_TOAST_BACKGROUND));
                    myToast.show();
                }
            });

        }
Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77
1

you can simple use this:

1) First make a Common class named DisplayToast.

and in this class make method like 


public void showToast(Context context){

 Toast myToast = Toast.makeText(context, configurationz.ERROR_MESSAGES_SIGNUP_USER_NAME_MIN_LENGTH_PROBLEM, Toast.LENGTH_SHORT);
 myToast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0);
 TextView tv = (TextView) myToast.getView().findViewById(android.R.id.message);
 tv.setTextColor(Color.parseColor(configurationz.COLORS_TOAST_TEXT_COLOR));
 tv.setTextSize(20);
myToast.getView().setBackgroundColor(Color.parseColor(configurationz.COLORS_TOAST_BACKGROUND));
 myToast.show();
}

Now whenever you want to access this method in any class then you should make object of this class like:

 DisplayToast dt = new DisplayToast();

now call that method

 dt.showToast(context);

2) You can also make static method for that like:

 public static void showToast(Context context){

 Toast myToast = Toast.makeText(context, configurationz.ERROR_MESSAGES_SIGNUP_USER_NAME_MIN_LENGTH_PROBLEM, Toast.LENGTH_SHORT);
 myToast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0);
 TextView tv = (TextView) myToast.getView().findViewById(android.R.id.message);
 tv.setTextColor(Color.parseColor(configurationz.COLORS_TOAST_TEXT_COLOR));
 tv.setTextSize(20);
myToast.getView().setBackgroundColor(Color.parseColor(configurationz.COLORS_TOAST_BACKGROUND));
 myToast.show();
}

And you can use this in your class like

 DisplayToast.showToast(context);
Piyush
  • 18,895
  • 5
  • 32
  • 63
0

Thank you everyone, thanks to your help, this is what I created and it works perfectly:

public class ToastMaker extends Activity {

    public void toast(Context context, final String message, Configurationz configurationz, int duration) {

                Toast myToast = Toast.makeText(context, message , duration);
                myToast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0);
                TextView tv = (TextView) myToast.getView().findViewById(android.R.id.message);
                tv.setTextColor(Color.parseColor(configurationz.COLORS_TOAST_TEXT_COLOR));
                tv.setTextSize(configurationz.TOAST_TEXT_SIZE);
                myToast.getView().setBackgroundColor(Color.parseColor(configurationz.COLORS_TOAST_BACKGROUND));
                myToast.show();
    }
}

Im using it this way:

ToastMaker toastMaker = new ToastMaker();

toastMaker.toast(net.asdqwe.activities.Signup.this, configurationz.ERROR_MESSAGES_SIGNUP_USER_NAME_MIN_LENGTH_PROBLEM, configurationz, Toast.LENGTH_SHORT);
Kaloyan Roussev
  • 14,515
  • 21
  • 98
  • 180