Android - Custom Toast Class - java

I am trying to create a class that it´s supossed to show a toast every time that an object of this class is instaciated.
I want to do this so that I don´t have the same toast code repeated in every activity.
public class Toast extends android.widget.Toast {
String toast_text;
Context toast_context;
public Toast(String toast_text, Context toast_context) {
this.toast_text = toast_text;
this.toast_context = toast_context;
Toast toast = android.widget.Toast.makeText(this.toast_context.this, this.toast_text, Toast.LENGTH_LONG);
ViewGroup view = (ViewGroup) toast.getView();
view.setBackgroundResource(R.drawable.background_global);
TextView messageTextView = (TextView) view.getChildAt(0);
messageTextView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
messageTextView.setTextSize(35);
Typeface face_font = Typeface.createFromAsset(getAssets(), "res/font/aldrich.ttf");
messageTextView.setTypeface(face_font);
messageTextView.setTextColor(Color.CYAN);
toast.show();
}
}
These are the following erros:
On the constructor: "There is no default constructor available in "android.widget.toast";
On the first error: " ')' expected";
On the second error: "Cannot resolve method "getAssets" ".

Use toast_context that you receive through constructor to create toast and access asset
Toast toast = android.widget.Toast.makeText(toast_context, toast_text, Toast.LENGTH_LONG);
Typeface face_font = Typeface.createFromAsset(toast_context.getAssets(), "res/font/aldrich.ttf");
Beside this, You should use other name than Toast to create custom toast
class MyToast extends android.widget.Toast {
public MyToast(String toast_text, Context toast_context) {
super(toast_context);
...
}
}

Related

Method not running in Android - Java

I have a problem, whenever I make a method myself and I want to use it, I run into problems. The program does not give an error, but it does not run. If anyone knows what the problem is, please help me.
public class MainActivity extends AppCompatActivity {
List item
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomToast1(" show text");
}
public void CustomToast1(String text) {
Toast toast = Toast.makeText(this, text, Toast.LENGTH_LONG);
View view1 = toast.getView();
TextView txt1 = view1.findViewById(android.R.id.message);
view1.setBackgroundColor(Color.argb(50, 0, 0, 0));
txt1.setTextSize(24);
txt1.setTextColor(Color.BLUE);
txt1.setWidth(700);
txt1.setGravity(Gravity.CENTER_VERTICAL);
txt1.setCompoundDrawablePadding(40);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.show();
}
You can't access the toast view from the toast, to protect the users.
if you want to do this you should use this code:
private void CustomToast1(String text) {
Toast toast = Toast.makeText(this, text, Toast.LENGTH_LONG);
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
View view1 = toast.getView();
TextView txt1 = (TextView) view1.findViewById(androidx.appcompat.R.id.message);
view1.setBackgroundColor(Color.argb(50, 90, 90, 0));
txt1.setTextSize(24);
txt1.setTextColor(Color.BLUE);
txt1.setWidth(700);
txt1.setGravity(Gravity.CENTER_VERTICAL);
txt1.setCompoundDrawablePadding(40);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
}
toast.show();
}
if the statement is true then it will display it with it
but for you probably it will display as default but it won't crash.

Ho to call own toast message from another activity

I definied my own method to display Toast and I want to call it from another Activity. When I do that, my app crash. Do you have some Attempt to invoke virtual method.... on a null object reference.
The Toast method:
public void showToastDown(Context context, String message) {
context = getApplicationContext();
inflater = getLayoutInflater();
View v = inflater.inflate(R.layout.toast_down, (ViewGroup) findViewById(R.id.toast_down_root));
TextView tvToastDown = v.findViewById(R.id.tvToastDown);
tvToastDown.setText(message);
Toast toast = new Toast(context);
toast.setGravity(Gravity.BOTTOM|Gravity.FILL_HORIZONTAL, 0,0);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(v);
toast.show();
}
and the code from 2nd activity:
switch (item.getItemId()){
case R.id.btnAddActionBar:
MainActivity mainActivity= new MainActivity();
mainActivity.showToastDown(this, "TEXT");
break;
}
return super.onOptionsItemSelected(item);
}
Remove this line from your code, as you are already passing value of context so no need to initialize again with Application Context.
context = getApplicationContext();
EDIT: Change Your Method like this:
public void showToastDown(Context context, String message) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
View v = inflater.inflate(R.layout.toast_down, (ViewGroup) ((Activity)context).findViewById(R.id.toast_down_root));
TextView tvToastDown = v.findViewById(R.id.tvToastDown);
tvToastDown.setText(message);
Toast toast = new Toast(context);
toast.setGravity(Gravity.BOTTOM|Gravity.FILL_HORIZONTAL, 0,0);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(v);
toast.show();
}
There's a couple of problems with your code:
Never instantiate activities yourself (mainActivity = new MainActivity). This doesn't work, only the Android OS can create activities. I suggest taking some (free) beginner android tutorials online to understand how activities work and what their role is.
The toast function you've written is a utility function that doesn't appear to have anything to do with MainActivity. If you put it as a static function of a different class (whose purpose is only to provide useful utility functions like this) e.g. ActivityTools.java, then you can call it from anywhere and pass it a Context like you are doing.
In your toast function, you're receiving a context as the first parameter but then immediately overwriting it. This doesn't make any sense, there's no reason to overwrite it like this.

Android Custom Toast Message not working in a separate class

I have created the following method for custom toast.
public void customToastMessage(String message){
LayoutInflater inf = (LayoutInflater)con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inf.inflate(R.layout.custom_toast_layout,(ViewGroup)findViewById(R.id.myCustomToast));
TextView toastMessage = layout.findViewById(R.id.myCustomToastText);
toastMessage.setText(message);
Toast warningMessage = Toast.makeText(con, message, Toast.LENGTH_LONG);
warningMessage.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 10);
warningMessage.setView(layout);
warningMessage.show();
}
As long as this method exists in MainActivity, it works fine but when I move it to a separate class I get:
"java.lang.IllegalStateException: Could not execute method for android:onClick at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:389)".
What do I need to change in my class below?
public class MyCustomUI extends AppCompatActivity {
private static Context con;
public MyCustomUI(Context con){
this.con = con;
}
public void customToastMessage(String message){
LayoutInflater inf = (LayoutInflater)con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inf.inflate(R.layout.custom_toast_layout,(ViewGroup)findViewById(R.id.myCustomToast));
TextView toastMessage = layout.findViewById(R.id.myCustomToastText);
toastMessage.setText(message);
Toast warningMessage = Toast.makeText(con, message, Toast.LENGTH_LONG);
warningMessage.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 10);
warningMessage.setView(layout);
warningMessage.show();
}
}
I'm guessing your problem is when you inflate your layout:
View layout = inf.inflate(R.layout.custom_toast_layout,(ViewGroup)findViewById(R.id.myCustomToast));
I'm also guessing the problem is (ViewGroup)findViewById(R.id.myCustomToast). You are trying to look for a View/ViewGroup that does not exist on that class but on your MainActivity.
Pass it as an argument to your method (just the relevant part):
public void customToastMessage(String message, ViewGroup customToast){
LayoutInflater inf = (LayoutInflater)con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inf.inflate(R.layout.custom_toast_layout, viewgroup);

how to create toast in java to show value of a computation

I am trying to create a calculator that shows the result using a toast.
my code is
Button addBtn=(Button)findViewById(R.id.additionBtn);
addBtn.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
EditText inputFirst = (EditText)findViewById (R.id.inputTextFirst);
String message= inputFirst.getText().toString();
EditText inputSecond = (EditText)findViewById (R.id.inputTextSecond);
String message2= inputSecond.getText().toString();
int first = Integer.parseInt(message);
int second = Integer.parseInt(message2);
int sum = first + second;
TextView resTxt = (TextView)findViewById(R.id.viewResult);
resTxt.setText("Result is " +sum);
Toast.makeText(this,message, Toast.LENGTH_LONG).show();
TextView ResTxt=(TextView)findViewbyId(R.id.viewResult);
resTxt.setText("answer is" + sum);
} });
}
it manages to show the result in a label. but with the toast, it is giving me notification that makeText not applicable to arguments.
Change
Toast.makeText(this,message, Toast.LENGTH_LONG).show();
to
Toast.makeText(ActivityName.this ,message, Toast.LENGTH_LONG).show();
makeText takes as first parameter a Context object, but this, in your case, refers to the object of the inner anonymous class new View.OnClickListener
if it inside your activity then you should write this:
Toast.makeText(ActivityName.this ,message, Toast.LENGTH_LONG).show();
if it's outside your class, you'll need to get your activity context

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

Categories

Resources