How can we increase the font size in toast? - android

Is there any way to increase the font size in toast without customizing?
I don't want to create a layout for increasing the text size.
Is there any way?
Thanks,
Niki

I believe it is achieveable by this:
ViewGroup group = (ViewGroup) toast.getView();
TextView messageTextView = (TextView) group.getChildAt(0);
messageTextView.setTextSize(25);

this is ...
Toast toast = Toast.makeText(context, R.string.yummyToast, Toast.LENGTH_SHORT);
//the default toast view group is a relativelayout
RelativeLayout toastLayout = (RelativeLayout) toast.getView();
TextView toastTV = (TextView) toastLayout.getChildAt(0);
toastTV.setTextSize(30);
toast.show();

Here is how to do that with spans:
SpannableStringBuilder biggerText = new SpannableStringBuilder(text);
biggerText.setSpan(new RelativeSizeSpan(1.35f), 0, text.length(), 0);
Toast.makeText(context, biggerText, Toast.LENGTH_LONG).show();

You can't increase the font size without creating a CustomToastView.
This is a related question.

Working from Ani's answer, another solution that allows you to set the text size to a dimension value would be something like:
public static void showToast(Context context, int resId) {
Toast toast = Toast.makeText(context, resId, Toast.LENGTH_LONG);
LinearLayout toastLayout = (LinearLayout) toast.getView();
TextView toastTV = (TextView) toastLayout.getChildAt(0);
toastTV.setTextSize(TypedValue.COMPLEX_UNIT_PX,
context.getResources().getDimension(R.dimen.TEXT_SIZE));
toast.show();
}
This lets you match the size you get your toasts to be the same size as specified in TextView and Button controls, for example.

Nikolay answered this beautifully. Heuristically, it's pretty a safe bet that, if an Android API method takes a CharSequence instead of a String it means you can pass it Spanned (i.e. formatted) text.
If you don't want to want to build it by hand, and are more comfortable with HTML you can do this:
Toast.makeText(context, Html.fromHtml("<big>Big text.</big>"), Toast.LENGTH_SHORT)
Or, if using a string resource, do this:
<string name="big_text"><big>Big text.</big></string>
And just use it like this:
Toast.makeText(context, R.string.big_text, Toast.LENGTH_SHORT)
The available markup is mostly documented here.

Toast toast = Toast.makeText(MyApplication.getContext(), "here", Toast.LENGTH_LONG);
ViewGroup group = (ViewGroup) toast.getView();
TextView messageTextView = (TextView) group.getChildAt(0);
messageTextView.setTextSize(30);
toast.show();

Try this:
SpreadsheetApp.getActive().toast(" ","The text you want here");
By keeping the first set of quotes empty, the second set will bold your text and make it easier to read.
Normally the line would be: .toast("Text you want here","The title of your toast popup");
By making your text as the Title, second set of quotes, and leaving the first quotes empty, your text is now easier to read.

You can try to put the following code into your Manifest:
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:resizeable="true"
android:smallScreens="true"/>
Put it above the <Application> element.

Related

Issue with custom Toast with color

I trying to make a custom toast with the background "Orange color", however, in the attempt I am able to get it but the field also becomes white and the text is not visible, I have attached the image
You can see that the text is not visible.
here is my code:
String g= "+";
Toast toast = Toast.makeText(getActivity(), "Click and hold on '"+g+"' icon",
Toast.LENGTH_SHORT);
View view= toast.getView();
view.setBackgroundColor(Color.parseColor("#FF791B"));
View t = toast.getView().findViewById(android.R.id.message);
t.setBackgroundColor(Color.parseColor("#FFFFFF"));
toast.show();
I just want the text to be in white.
Is something I am missing in the code, I am not able to get it right.
Also, is there a way to make the "+" sign bold g
Just Change this line
t.setBackgroundColor(Color.parseColor("#FFFFFF"));
To
t.setTextColor(Color.parseColor("#FFFFFF"));
I have mode some changes here:
TextView t = toast.getView().findViewById(android.R.id.message);
t.setTextColor(Color.WHITE);
I have saved it as textView and I was able to get the setTextColor method and I was able to change the color
Try this - customttoast.xml is your custom toast xml and custom_toast_layout can be your activity layout xml
//Creating the LayoutInflater instance
LayoutInflater li = getLayoutInflater();
//Getting the View object as defined in the customtoast.xml file
View layout = li.inflate(R.layout.customtoast,(ViewGroup) findViewById(R.id.custom_toast_layout));
//Creating the Toast object
Toast toast = new Toast(getApplicationContext());
toast.setDuration(Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setView(layout);//setting the view of custom toast layout
toast.show();

Create a Toast that Comes From Top of Screen

I am wondering how do we create a Custom toast in Android that shows on the top of screen like this?
I came from iOS background and now I have to create custom controls like this.
Any pointers please?
Thank you
Check this link: https://github.com/gfranks/GFMinimalNotifications, this is what you want i think and it is working fine for me.
output:
Or you can prepare custom toast like this:
View layout = getLayoutInflater().inflate(R.layout.customtoast,
(ViewGroup) findViewById(R.id.custom_toast_layout));
Toast toast = new Toast(getApplicationContext());
toast.setDuration(Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP, 0, 0);
toast.setView(layout);
toast.show();
you can use crouton for this purpose.
Description :
A Crouton will be displayed at the position the developer decides.
Standard will be the top of an application window. You can line up
multiple Croutons for display, that will be shown one after another
Create a Crouton for any CharSequence:
Crouton.makeText(Activity, CharSequence, Style).show();
more details on Crouton, a Context sensitive notifications for Android
there are many you can use for toast describe here
try this ::
Toast toast= Toast.makeText(getApplicationContext(),
"Your string here", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
add this line for position
toast.setGravity(Gravity.TOP, 0, 0);
creat customize toast layout, toast.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#color/yellow"
android:layout_width="match_parent"
android:padding="8dp"
android:gravity="center"
android:textSize="32sp"
android:layout_height="256dp"/>
create toast using the layout above:
static Toast t;
public static void show(Context c, String s) {
if (t == null) {
t = new Toast(c.getApplicationContext());
t.setGravity(Gravity.TOP | Gravity.FILL_HORIZONTAL, 0, 0);
LayoutInflater inflater = LayoutInflater.from(c);
TextView v = (TextView) inflater.inflate(R.layout.toast, null);
t.setView(v);
t.setDuration(Toast.LENGTH_LONG);
}
TextView v = (TextView) t.getView();
v.setText(s);
t.show();
}
As you can see, there are many solutions. But the easiest way is create a layout in your XML layout file, and make it invisible. When this view needs to be shown make it visible and start an animation.

Android compound drawable

I am using a custom toast, with a ImageView and a TextView using a background in drawable. But the Android Studio tells me to use a single TextView and a compound drawable instead. I have already used a drawable for background, but how can I "draw" a picture at a certain position relative to the TextView?
You can use a single TextView and a CompoundDrawable like this
Toast toast = Toast.makeText(this, "Custom Compount Drawable Toast", Toast.LENGTH_LONG);
View toastView = toast.getView(); //This'll return the default View of the Toast.
TextView toastMessage = (TextView) toastView.findViewById(android.R.id.textview);
toastMessage.setTextSize(25);
toastMessage.setCompoundDrawablesWithIntrinsicBounds(R.drawable.image_id, 0, 0, 0);
toastMessage.setGravity(Gravity.CENTER);
toastMessage.setCompoundDrawablePadding(16);
toastView.setBackgroundColor(Color.CYAN);
toast.show();

Android make Spanned containing link clickable in Toast

Through a webservice I get an Error message containing a link (e.g.
Click <a href='blablabla'>here</a>
). I use fromHtml to turn it into a spanned and then show it in a custom Toast.
Inside the toast the text is shown and the 'here' is underlined, just like a link. However when I click it, it doesn't do anything.
How should I solve this? Is there any way to deduct the link from the xml (e.g.
<ERROR>Click <a href='blabla'>here</a></ERROR>
) so I can create a button to push
which opens the link in a Webview?
Here's the relevant code:
My main activity
CommonCode.showToast(error, mContext, mViewGroup, true);
CommonCode
public static void showToast(Spanned toastString, Context context, View view, Boolean isLink) {
LayoutInflater inflater = LayoutInflater.from(context);
View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) view);
TextView text = (TextView) layout.findViewById(R.id.toastText);
if(isLink == true) {
text.setMovementMethod(LinkMovementMethod.getInstance());
}
text.setText(toastString);
Toast toast = new Toast(context);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
Toasts were designed to place a small amount of information in front of the user for a short time. If you need buttons pressed try a dialog instead of a toast.

Android: How to set the colour of a Toast's text

I am displaying a toast message as the result of an if statement using the following code:
Toast.makeText(getBaseContext(), "Please Enter Price", Toast.LENGTH_SHORT).show();
It is displayed as white text on a white background, as such it can not be read! My question is, how can I change the colour of the toast's text?
You can achieve this very easily, without creating a custom layout by modifying the default Toast :
Toast toast = Toast.makeText(this, resId, Toast.LENGTH_SHORT);
TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
v.setTextColor(Color.RED);
toast.show();
You can find the layout used by the default toast view in the Android SDK :
$ANDROID-SDK$/platforms/android-8/data/res/layout/transient_notification.xml
You can create a custom Toast view to suit your requirements. See the section titled "Creating a Custom Toast View" at http://developer.android.com/guide/topics/ui/notifiers/toasts.html
You may want to create a custom Toast
<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="10dp"
android:background="#DAAA"
>
<ImageView android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="10dp"
/>
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#FFF"
/>
</LinearLayout>
-
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout,
(ViewGroup) findViewById(R.id.toast_layout_root));
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.android);
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello! 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();
Source
The simplest way to change the background color of a toast and the background color of a toast's text:
View view;
TextView text;
Toast toast;
toast.makeText(this, resId, Toast.LENGTH_SHORT);
view = toast.getView();
text = (TextView) view.findViewById(android.R.id.message);
text.setTextColor(getResources().getColor(R.color.black));
text.setShadowLayer(0,0,0,0);
view.setBackgroundResource(R.color.white);
toast.show();
Try to use Toasty library. It is really easy to use it - https://github.com/GrenderG/Toasty
You can also use SpannableString. It can also color parts of the string.
SpannableString spannableString = new SpannableString("This is red text");
spannableString.setSpan(
new ForegroundColorSpan(getResources().getColor(android.R.color.holo_red_light)),
0,
spannableString.length(),
0);
Toast.makeText(this, spannableString, Toast.LENGTH_SHORT).show();
You can try this if you don't wish to use any custom libraries
Toast toast=Toast.makeText(getApplicationContext(),"This is advanced toast",Toast.LENGTH_LONG);
View view=toast.getView();
TextView view1=(TextView)view.findViewById(android.R.id.message);
view1.setTextColor(Color.YELLOW);
view.setBackgroundResource(R.color.colorPrimary);
toast.show();
Here is an example in Kotlin, showing how you can change the background color of a toast and it's text color :
val toast = Toast.makeText(context, text, Toast.LENGTH_SHORT)
toast.view.background.setColorFilter(ContextCompat.getColor(context,
R.color.white), PorterDuff.Mode.SRC_IN)
val textView = toast.view.findViewById(android.R.id.message) as TextView
textView.setTextColor(ContextCompat.getColor(context, R.color.black))
toast.show()
The solution with setting a custom view on Toast is deprecated for API 30 and forward.
Documentation says
apps
* targeting API level {#link Build.VERSION_CODES#R} or higher that are in the background
* will not have custom toast views displayed.
The alternative is
Toast.makeText(applicationContext,
HtmlCompat.fromHtml("<font color='red'>custom toast message</font>", HtmlCompat.FROM_HTML_MODE_LEGACY),
Toast.LENGTH_LONG).show()
Html color tag can also be <font color='#ff6347'>
For every modification that has to do with the text displayed the above solution would be enough. You can for example make the text bold by inserting <b>my text</b> or you maybe want to change the font-familywith <font font-family='...'> my text </font> For all those changes that solution will be enough.
If you want to modify the container though with properties like background-color the only alternative is to use Snackbar. View can not be modified for Toast anymore.
https://developer.android.com/guide/topics/ui/notifiers/toasts?hl=es-419#java
Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();

Categories

Resources