Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Change text size in toast

Hello,

After much googling, I remained stumped on how to change the default text size of a toast. I found the following code:
ViewGroup group = (ViewGroup) toast.getView(); TextView messageTextView = (TextView) group.getChildAt(0); messageTextView.setTextSize(25);

Apparently this is all I need to do. However, in Xamarin when I type this code, I immediately get an error that says "the name 'toast' does not exist in the current context".

I imagine what I am trying to do is relatively simple, but I am (pathetically) quite stuck!

Any help would be greatly appreciated!

Dustin

Tagged:

Posts

  • CortezCortez USMember ✭✭✭

    Did you try with capital letter? If you want to show a toast you use a code like this:

    Toast.MakeText(this, "Sample message",ToastLength.Long).Show();

    ViewGroup group = (ViewGroup) Toast.GetView();
    also doesn't work?

  • TwoChainTwoChain CAMember ✭✭

    Hi Cortez,

    Thanks for your reply. Unfortunately, when I try what you have written, I still get a "toast does not contain a definition for GetView()" error. Any idea why?

    Thanks,

    Dustin

  • CharwakaCharwaka INMember ✭✭✭✭✭

    Hi @TwoChain

    Use the following code

      Toast toast = Toast.MakeText(this, "Sample message", ToastLength.Long);
                TextView v = (TextView)toast.View.FindViewById<android.R.id.message>();
                v.SetTextSize(Android.Util.ComplexUnitType.Sp, 25);
                toast.Show();
    

    Please Mark as Answer if this works for you

  • TwoChainTwoChain CAMember ✭✭

    Hello @Charwaka

    Thank you for your reply. The line that seems to cause me trouble is:

    TextView v = (TextView)toast.View.FindViewById<android.R.id.message>();

    I have seen similar code before, but it never seems to work for me. I tried capitalizing 'Android', but this still doesn't work. I also tried replacing 'android.R' with 'Android.Resources.Id.Message' but this still did not work.

    Is 'android.R' supported by Xamarin, or is this Java syntax?

    Either way, I still get the following error:
    There is no argument given that corresponds to the required formal parameter 'id' of 'View.FindViewById(int)'

    Any thoughts?

    Thanks,

    Dustin

  • CharwakaCharwaka INMember ✭✭✭✭✭

    Hi @TwoChain
    first u need create UI like this

            <TextView android:id="@+id/message"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:textColor="#FFF"
                          />
    

    TextView v = (TextView)toast.View.FindViewById<Android.Resource.Id.message>();

  • TwoChainTwoChain CAMember ✭✭

    Hi @Charwaka,

    Thank you for your patience. Unfortunately this code still does not work. I am including my entire code for you to try.

    When I try running this code, I get the following error:

    There is no argument given that corresponds to the required formal parameter 'id' of 'View.FindViewById(int)'
    The pesky line :
    ``TextView v = (TextView)toast.View.FindViewById<Android.Resource.Id.message>();' is where this error keeps coming from.

    Grateful for any additional help!

    p.s. I had to delete the xmlns:android namespace in the axml file below before the Xamarin Forums site would let me post my question.

    Main.axml
    <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:text="Text" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textView1" /> <Button android:text="Button" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/button1" /> <TextView android:id="@+id/message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFF" /> </LinearLayout>

    '
    MainActivity.cs
    using Android.App;
    using Android.Widget;
    using Android.OS;

    namespace simpleButton
    {
    [Activity(Label = "simpleButton", MainLauncher = true)]
    public class MainActivity : Activity
    {
    protected override void OnCreate(Bundle savedInstanceState)
    {
    base.OnCreate(savedInstanceState);

            int count =1;
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
    
            Button button = FindViewById<Button>(Resource.Id.button1);
            TextView textView = FindViewById<TextView>(Resource.Id.textView1);
    
            Toast toast = Toast.MakeText(this, "Sample message", ToastLength.Long);
            TextView v = (TextView)toast.View.FindViewById<Android.Resource.Id.message>();
            v.SetTextSize(Android.Util.ComplexUnitType.Sp, 25);
            toast.Show();
    
    
            button.Click += delegate {
                button.Text = string.Format("{0} clicks!", count++);
                toast.Show();
            };
    
    
        }
    }
    

    }
    `

  • TwoChainTwoChain CAMember ✭✭

    My saga continues...

    I have now found some code that I have successfully used to make an icon appear instead of my toast:

    `using Android.App;
    using Android.Widget;
    using Android.OS;
    using Android.Views;

    namespace simpleButton
    {

    [Activity(Label = "simpleButton", MainLauncher = true)]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
    
    
    
            int count =1;
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
    
            Button button = FindViewById<Button>(Resource.Id.button1);
    
    
            button.Click += delegate {
                button.Text = string.Format("{0} clicks!", count++);
    
    
    
    
                using (var toast = new Toast(ApplicationContext))
                using (var image = new ImageView(this))
                {
                    image.SetImageResource(Resource.Drawable.icon);
                    toast.Duration = ToastLength.Short;
                    toast.View = image;
                    toast.Show();
                }
    
    
    
            };
    
    
        }
    
    }
    

    }

    `
    By putting an icon in the 'drawable' folder, and naming it 'icon', I can make an icon appear! Great news... except that this wasn't my goal at all. Sadly, I am still unable to simply change the font size in a normal toast.

    Any more ideas?

  • TwoChainTwoChain CAMember ✭✭
    edited December 2017

    The final chapter of this epic tale is upon us....

    After traversing the dark corners of the internet and coding forums, our protagonist had tired of chasing false prophets, whose ability to spin wild yarns about custom toasts was riveled only by their ability to slither back into the aether from whence them came when confronted by the fact that their C# code was actually thinly veiled Java code which will not compile when using Xamarin.

    Crushed, our hero collapsed under the weight of his own despair, resigned to the fact that his toasts would always be rather pedestrian, never having the trademark flair his customers (currently consisting only of himself) had come to expect.

    Rejoice! All was not lost. While performing a last desperate google search, our hero uncovered the one true prophet who could lead him to glory. Telugu Velugu, the youtube master, removed the custom toast code from behind the shroud of mystery, revealing the most mindbogglingly confusing, unintelligible, yet still copy/paste-able, code our hero could have ever dreamt of.

    His spirits invigorated, he rose like a Phoenix from the ashes of his many burnt toasts, and in his endless generosity, has reentered the haunted graveyard of unanswered questions, known as the Xamarin Forums, to help guide others in their noble pursuit of customizing the seemingly uncustomizable; the toast.

    Now let us all raise our glasses, and join in a ‘toast’ to the completely befuddling yet somehow compilable code that follows!

    `using Android.App;
    using Android.Widget;
    using Android.OS;
    using Android.Views;
    using Android.Content;

    namespace simpleButton
    {

    [Activity(Label = "simpleButton", MainLauncher = true)]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
    
    
    
            int count =1;
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
    
    
            ActionBar.SetDisplayShowCustomEnabled(false);
            ActionBar.Hide();
    
    
            Button button = FindViewById<Button>(Resource.Id.button1);
    
    
            button.Click += delegate {
                button.Text = string.Format("{0} clicks!", count++);
    
                LayoutInflater inflater = (LayoutInflater)this.GetSystemService
                (Context.LayoutInflaterService);
    
                View popup = inflater.Inflate(Resource.Layout.custom_toast, (ViewGroup)FindViewById(Resource.Layout.custom_toast), true);
                TextView text = popup.FindViewById<TextView>(Resource.Id.text);
                text.Text = "Better Work!";
    
                Toast toast = new Toast(this);
    
                toast.Duration = ToastLength.Short;
                toast.View = popup;
                toast.Show();
            };
    
    
        }
    
    }
    

    }

    `

    XML file placed lovingly into the layout folder, and named custom_toast:

    `

    <TextView android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text ="Testing"
    android:textColor="#000000"
    android:textSize="35dp"

              />
    

    `

    Text inside Main xml file:

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:text="Text" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textView1" /> <Button android:text="Button" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/button1" /> <TextView android:id="@+id/message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFF" /> </LinearLayout>

    And with that, let us all say a silent thank you to the Xamarin King, Telugu Velugu. Telugu, you will always be in our hearts and our minds, but sadly never our computer screens due to the perfectly logical Xamarin Forums rule which prevents new members from posting links (including the namespaces that are a requisite part of XML code).

    March forward fellow coders, and never forget, even in your darkest moments there will always be a Telugu out there waiting to help... he just won't be found on this message board.

  • NunoSantosXNunoSantosX Member
    edited March 2019

    To avoid creating a custom layout you can just access the TextView like this:

    Toast toast = Toast.MakeText(this, "Sample message", ToastLength.Long);
    var toastView = (ViewGroup)toast.View;
    
    if (toastView.ChildCount > 0 && toastView.GetChildAt(0) is TextView)
    {
        TextView tv = (TextView)toastView.GetChildAt(0);
        tv.SetTextSize(Android.Util.ComplexUnitType.Sp, 25);
    }
    
    toast.Show();
    
Sign In or Register to comment.