How to adjust background color, background color and text color for Toast in android

I want to customize my toast without creating my own layout, changing the default Toast. I want red for the background of the toast and white for the text of the toast, and I want to make my toast larger than the default toast. when I launch my application, nothing changes from my toast, it is still displayed in the toast by default.

Here's how I set up my toast:

if (seriesSelection == null) { Toast toast = Toast.makeText(getApplicationContext(), "tidak ada chart yang dipilih", Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 50, 50); toast.getView().setPadding(10, 10, 10, 10); toast.getView().setBackgroundColor(Color.RED); TextView text = (TextView) toast.getView().findViewById(android.R.id.message); text.setTextColor(Color.WHITE); text.setTextSize(14); } else { Toast toast= Toast.makeText( getApplicationContext(), "Nilai " + listData.get(seriesSelection.getPointIndex()).getInuNilai()+ " tanggal " + listData.get(seriesSelection.getPointIndex()).getTanggal(), Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 50, 50); toast.getView().setPadding(10, 10, 10, 10); toast.getView().setBackgroundColor(Color.RED); text.setTextColor(Color.WHITE); text.setTextSize(14); toast.show(); } 
+6
source share
3 answers

You may have a custom view inflating a custom view and using toast.setView(layout) .

Example:

 LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.custom_toast, (ViewGroup) findViewById(R.id.toast_layout_root)); TextView text = (TextView) layout.findViewById(R.id.text); text.setText("This is a custom toast"); Toast toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); toast.setDuration(Toast.LENGTH_LONG); toast.setView(layout); toast.show(); 

And your xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toast_layout_root" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="8dp" android:background="#DAAA" > <ImageView android:src="@drawable/droid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="8dp" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFF" /> </LinearLayout> 

Additional Information @

http://developer.android.com/guide/topics/ui/notifiers/toasts.html

Edit your, if even part of the code (separately), it shows a toast with a red background and white text. I do not see any problems. But if you need to customize, you can use your own layout and inflate the layout and set the presentation to toast.

Edit:

Your text image

  TextView text = (TextView) toast.getView().findViewById(android.R.id.message); 

initialized in the if part, otherwise the textview part is not initialized.

Initialize text outside the external if and else code.

Check out this crouton library you might find useful

https://github.com/keyboardsurfer/Crouton

+9
source

Toast has a setView() method.

You can configure Toast to display any kind.

I would say, instead of trying to edit the view inside Toast, you simply create a View and put it in yourself.

+2
source

I have very simple and simple code for setting Toast accordingly, you can also change the background of the toast and the color of the text.

  Toast toast = Toast.makeText(MainActivity.this, "Added successfully", Toast.LENGTH_LONG); View view1 = toast.getView(); toast.getView().setPadding(20, 20, 20, 20); view1.setBackgroundResource(R.color.GREEN); view1.setTextColor(Color.RED); toast.show(); 
0
source

Source: https://habr.com/ru/post/950035/


All Articles