Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing font in AlertDialog

Could someone suggest a way to change font in a dynamically created AlertDialog (in title and body)? I tried in loads of ways, but none of them worked. The code is:

public void onClick(View v) {

    new AlertDialog.Builder( c )
    .setTitle( data.eqselect )
    //  .set 
    .setIcon(R.drawable.icon)
    .setMessage(Threads.myData[0] )
    .setNegativeButton( "Close", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            Log.d( "AlertDialog", "Negative" );
        }
    } )
    .show();
}
like image 596
Laurynas G Avatar asked May 10 '12 16:05

Laurynas G


People also ask

How do I change the font in AlertDialog?

If you only want to change text format, you can just override alertDialogTheme attribute to change the theme for the AlertDialog . <style name="MyTheme" parent="Theme. AppCompat. Light.

How do I use AlertDialog?

Alert Dialog code has three methods:setTitle() method for displaying the Alert Dialog box Title. setMessage() method for displaying the message. setIcon() method is used to set the icon on the Alert dialog box.

How many button options can you use in creating AlertDialog?

You can only create a Alertdialog with 3 buttons if you dont make the view by yourself. You can either make your own custom view in xml. but i'd suggest you just make a List.


1 Answers

Instead of setting the text of the alertdialog, you should set a custom view form your layouts. And before you do so, modify your view's font.

TextView mycontent = (TextView) findViewById(R.id.yourid);
         mycontent.setTypeface(Typeface.createFromAsset(getAssets(), "font.ttf")

And to set the view of your alert dialog, call .setView(mycontent) instead of setMessage() Although this doesn't change your title as far as I know.

Update I'm seeing you're unable to get what I'm saying, so here's a full example

TextView content = new TextView(this);
         content.setText("on another font");
         content.setTypeface(Typeface.SANS_SERIF);

//Use the first example, if your using a xml generated view

     AlertDialog.Builder myalert = new AlertDialog.Builder(this);
         myalert.setTitle("Your title");
         myalert.setView(content);
         myalert.setNeutralButton("Close dialog", null);
         myalert.setCancelable(true);
         myalert.show();

This is using a TextView that we've just created, and it won't have margins or such. If you use a readily created one form your layout files, you'd be able to customize all those settings, although you can do that for this example to in code.

Of course, replace the font with the one you want.. A sample TextView from xml could be:

<TextView
        android:id="@+id/yourid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:text="your content" />
like image 120
Juan Cortés Avatar answered Sep 25 '22 14:09

Juan Cortés