Kaloyan Roussev
Kaloyan Roussev

Reputation: 14711

How can I transform this into a method/class so it can be reusable?

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

Upvotes: 0

Views: 128

Answers (5)

Kaloyan Roussev
Kaloyan Roussev

Reputation: 14711

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);

Upvotes: 0

Piyush
Piyush

Reputation: 18923

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);

Upvotes: 1

Biraj Zalavadia
Biraj Zalavadia

Reputation: 28484

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();
                }
            });

        }

Upvotes: 1

Darko Rodic
Darko Rodic

Reputation: 1020

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

Upvotes: 1

Ketan Ahir
Ketan Ahir

Reputation: 6738

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).

Upvotes: 1

Related Questions