Android: Fixed width font in AlertDialog
Asked Answered
H

2

14

I am displaying a list of strings in an AlertDialog as shown below:

enter image description here

1.How can I set a fixed width font like the one shown below

enter image description here

2.The AlertDialog has vertical scrolling enabled by default. How can I enable horizontal scrolling so that the last line stays in the previous line?

Horsley answered 22/5, 2012 at 20:18 Comment(0)
D
18

Inflate your own view for the AlertDialog and set android:typeface="monospace" in that view. To achieve this:

enter image description here

Use this layout:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/dlgView"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <LinearLayout android:id="@+id/dlgLayout"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="5dp">
    <TextView android:id="@+id/dlgText"
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:textColor="#FFF"
      android:typeface="monospace"/>
  </LinearLayout>
</ScrollView>

This code for the main activity builds an AlertDialog (drop a button onto the default layout).

public class MonospacedAlertActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //attach an instance of HandleClick to the Button
        findViewById(R.id.button1).setOnClickListener(new HandleClick());
    }
    private class HandleClick implements OnClickListener{
        public void onClick(View arg0) {
            ShowAlert(MonospacedAlertActivity.this);
        }

        private void ShowAlert(Activity callingActivity) {
            //Generate views to pass to AlertDialog.Builder and to set the text
            View dlg;
            TextView tvText;
            try {
              //Inflate the custom view
              LayoutInflater inflater = callingActivity.getLayoutInflater();
              dlg = inflater.inflate(R.layout.alertmono, (ViewGroup) callingActivity.findViewById(R.id.dlgView));
              tvText = (TextView) dlg.findViewById(R.id.dlgText);
            } catch(InflateException e) {
              //Inflater can throw exception, unlikely but default to TextView if it occurs
              dlg = tvText = new TextView(callingActivity);
            }
            //Set the text
            tvText.setText( "22-05-2012 20:51:13 114 58 00:04:19\n"+
                            "22-05-2012 20:59:15  84 52 00:01:25\n"+
                            "22-05-2012 22:49:48  96 51 00:01:32\n"+
                            "23-05-2012 00:08:52  79 58 00:01:26");
            //Build and show the dialog
            new AlertDialog.Builder(callingActivity)
              .setTitle(callingActivity.getString(R.string.app_name))
              .setCancelable(true)
              .setIcon(R.drawable.ic_launcher)
              .setPositiveButton("OK", null)
              .setView(dlg)
              .show();    //Builder method returns allow for method chaining
        }
    }
}
Dripdry answered 28/5, 2012 at 14:10 Comment(2)
What is alertmono and/or R.layout?Prichard
R.layout is how Android references the compiled layouts, alertmono was the name of the saved dialog layout file.Dripdry
T
0

I know this is way old but... I just discovered this and thought someone else might be interested. As far back as API 19 there are several different typefaces defined in Typeface.java: one of which is MONOSPACE. My alert dialog in Kotlin is:

private fun showDialog(mess:String) : Boolean {
    val tv = TextView(requireActivity())
    tv.typeface = Typeface.MONOSPACE
    tv.text = mess
    AlertDialog.Builder(this.requireContext())
        .setTitle("Changes in deck")
        .setView(tv)
        .show()
    return true
}
Torrid answered 26/9, 2021 at 0:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.