Dialog box title text size in Android

Dialog box title text size in Android

Séafra Orvar Author: Séafra Orvar Date: 2022-10-05
Dialog box title text size in Android

All you need to know about Dialog box title text size in Android , in addintion to android-How to set the text size of list item in alert dialog , Change title size of alertdialog in android? , Adjusting size of custom dialog box in android , android - How to customize text color of the CheckBoxPreference title

  1. Dialog box title text size in Android
  2. Question:

    I am working on an Android application in which I want to set the text size of my dialog box. I have used the following code an XML for textview but it is only setting the size of my fields. I want to set the text size and color of my title as well.

    final CharSequence[] items = { "Fake Request", "Abusive Language","Indecent Approach","Unacceptable Attitude","Dangerous Behavior",};
    
               ArrayAdapter<CharSequence> itemsAdapter = new
               ArrayAdapter<CharSequence> (this,
                        R.layout.menu_items, items);
               AlertDialog.Builder   builder = new AlertDialog.Builder(this);
               builder.setTitle("My Title");
               builder.setIcon(R.drawable.icon);
               builder.setAdapter(itemsAdapter, new DialogInterface.OnClickListener()
               {
                   public void onClick(DialogInterface dialog, int item) {
                              switch(item) {
                                      case 0:
                                  //Mark as Beautiful
                                      break;
                                   case 1:
                                  //Mark as Beautiful
                                      break;
                                case 2: 
                                    //Mark as Not a Portrait
                                      break;
                                case 3:
                                  //Mark as Offensive
                                      break;
                               case 4:
                                  //Mark as Spam
                                      break;
                               case 5:
                                  //cancel
                               break;
                               }
                   }
               }
    
                   );
               builder.show();
    
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
           android:id="@android:id/text1"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:padding="10dip"
           android:layout_margin="5dip"
           android:gravity="center_vertical"
           android:textSize="5dip"
           android:textColor="@color/red_theme"
           android:typeface="normal"
           android:lineSpacingExtra="0dip"/>
    


    Solution 1:

    If you want to do it all using styles:

    <style name="MyDialog" parent="Base.Theme.AppCompat.Light.Dialog">
        <item name="android:windowTitleStyle">@style/DialogTitle</item>
    </style>
    
    <style name="DialogTitle" parent="TextAppearance.AppCompat.Large">
        <item name="android:textSize">18sp</item>
    </style>
    

    And create your dialog as such

    new AlertDialog.Builder(this, R.style.MyDialog);
    

    Solution 2:

    //Use this code
    TextView myMsg = new TextView(this);
      myMsg.setText("Succesfully send!");
      myMsg.setGravity(Gravity.CENTER_HORIZONTAL);
      myMsg.setTextSize(20); 
      myMsg.setTextColor(Color.WHITE);
      //set custom title
            builder.setCustomTitle(myMsg);
    

    Solution 3:

    Use custom title using builder.setCustomTitle(textView) to set size and color of title.

    AlertDialog.Builder builder = new AlertDialog.Builder(context);
        TextView title = new TextView(context);
        title.setText("Title");
        title.setBackgroundResource(R.drawable.gradient);
        title.setPadding(10, 10, 10, 10);
        title.setGravity(Gravity.CENTER);
        title.setTextColor(getResources().getColor(R.color.green));
        title.setTextSize(22);
        builder.setCustomTitle(title);
        builder.setMessage("Message");
        builder.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
    
        }
        });
        builder.setNegativeButton(R.string.no, null);
        AlertDialog dialog = builder.show();
        messageView = (TextView)dialog.findViewById(android.R.id.message);
        messageView.setGravity(Gravity.CENTER);
        dialog.show();
    

  3. android-How to set the text size of list item in alert dialog
  4. Question:

    In My application one button is there when you click on that one alert dialog will be appear. that alert dialog consists of single choice list items. Here i want to set the text size of single choice list item. is it possible? if yes how to do it.

    The following is my code

    sclist.java

    package com.examples.scl;
    
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.app.Dialog;
    import android.content.DialogInterface;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    public class sclist extends Activity {
    
     private static final int DIALOG_SINGLE_CHOICE = 1;
    
    
      @Override
        protected Dialog onCreateDialog(int id) {
            switch (id) {
            case DIALOG_SINGLE_CHOICE:
                return new AlertDialog.Builder(sclist.this)
                    .setIcon(R.drawable.alert_dialog_icon)
                    .setTitle("Single choice list")
                    .setSingleChoiceItems(R.array.select_dialog_items2, 0, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
    
                            /* User clicked on a radio button do some stuff */
                        }
                    })
                    .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
    
                            /* User clicked Yes so do some stuff */
                        }
                    })
                    .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
    
                            /* User clicked No so do some stuff */
                        }
                    })
                   .create();
            }
            return null;
            }
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
    
        /* Display a radio button group */
        Button radioButton = (Button) findViewById(R.id.radio_button);
        radioButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                showDialog(DIALOG_SINGLE_CHOICE);
            }
        });
    }
    }
    


    Solution 1:

    I just encountered this problem myself on a matching game I'm working on. My solution isn't simple but I wanted to use a custom font, and I didn't see an easy way to do it with the 2.2 Android interface (which is what I'm targeting). The trick is to attach an OnShowListener to the alert dialog before you show it. In that listener, get ListAdapter out of the ListView and wrap it with a proxy object that forwards all the calls except the getView. In that function, cast the View to a TextView, set the typeface and size, and return the view. Here's my code:

                        // Add your list with builder up here
            AlertDialog alert = builder.create();
            alert.setOnShowListener(new OnShowListener() {
    
                @Override
                public void onShow(DialogInterface alert) {
                    ListView listView = ((AlertDialog)alert).getListView();
                    final ListAdapter originalAdapter = listView.getAdapter();
    
                    listView.setAdapter(new ListAdapter()
                    {
    
                        @Override
                        public int getCount() {
                            return originalAdapter.getCount();
                        }
    
                        @Override
                        public Object getItem(int id) {
                            return originalAdapter.getItem(id);
                        }
    
                        @Override
                        public long getItemId(int id) {
                            return originalAdapter.getItemId(id);
                        }
    
                        @Override
                        public int getItemViewType(int id) {
                            return originalAdapter.getItemViewType(id);
                        }
    
                        @Override
                        public View getView(int position, View convertView, ViewGroup parent) {
                            View view = originalAdapter.getView(position, convertView, parent);
                            TextView textView = (TextView)view;
                            textView.setTypeface(MyFontUtil.getTypeface(MyActivity,MY_DEFAULT_FONT));
                            textView.setTextColor(Color.BLACK);
                            textView.setTextSize(25); // FIXIT - absolute size 
                            return view;
                        }
    
                        @Override
                        public int getViewTypeCount() {
                            return originalAdapter.getViewTypeCount();
                        }
    
                        @Override
                        public boolean hasStableIds() {
                            return originalAdapter.hasStableIds();
                        }
    
                        @Override
                        public boolean isEmpty() {
                            return originalAdapter.isEmpty();
                        }
    
                        @Override
                        public void registerDataSetObserver(DataSetObserver observer) {
                            originalAdapter.registerDataSetObserver(observer);
    
                        }
    
                        @Override
                        public void unregisterDataSetObserver(DataSetObserver observer) {
                            originalAdapter.unregisterDataSetObserver(observer);
    
                        }
    
                        @Override
                        public boolean areAllItemsEnabled() {
                            return originalAdapter.areAllItemsEnabled();
                        }
    
                        @Override
                        public boolean isEnabled(int position) {
                            return originalAdapter.isEnabled(position);
                        }
    
                    });
    
                }
    
            });
    
    
            alert.show();
    

    If you want to see it in action look on the Android Market in a few weeks. Search for metaphyze (my publisher id). I haven't decided what to call it yet. (It's not "FlashMatch Chinese I Free". That was my first game. This is a kid's matching game. Play the game and tap the picture at the end. You'll see the AlterDialog with the style list.).

    Solution 2:

    Good question. I believe you'd have to use the AlertDialog.Builder constructor that also takes a theme AlertDialog.Builder(Context context, int theme), see if you can see anything useful in that description, I've never tried it myself.

  5. Change title size of alertdialog in android?
  6. Question:

    about text in the message there is not problem. I can't change (personalize) color, font, style of Title of alertdialog?

    What's the way?

    thanks!


    Solution 1:

    About font size you can use this:

    SpannableStringBuilder ssBuilser = new SpannableStringBuilder("Sample");
    StyleSpan span = new StyleSpan(Typeface.ITALIC);
    ScaleXSpan span1 = new ScaleXSpan(1);
    ssBuilser.setSpan(span, 0, 5, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
    ssBuilser.setSpan(span1, 0, 5, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
    
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
       builder.setTitle(ssBuilser);
       builder.show();
    

    or

    AlertDialog dialog = new AlertDialog.Builder(this).setMessage("Hello world").show();
    TextView textView = (TextView) dialog.findViewById(android.R.id.message);
    textView.setTextSize(40);
    

    Solution 2:

    Good question. I believe you'd have to use the AlertDialog.Builder constructor that also takes a theme AlertDialog.Builder(Context context, int theme), see if you can see anything useful in that description, I've never tried it myself.

    Solution 3:

    Here is an full example how i would do it. See the last line of code.

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(cntx);
    alertDialogBuilder
    .setMessage(Html.fromHtml(cntx.getString(R.string.UltimateDemoText)))
    .setCancelable(false)
    .setPositiveButton(cntx.getString(R.string.Ok), new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int id) { 
        dialog.cancel(); 
        }
     });
    AlertDialog alertDialog = alertDialogBuilder.create();    
    alertDialog.show();
    
    ((TextView)alertDialog.findViewById(android.R.id.title)).setTextSize(TypedValue.COMPLEX_UNIT_SP, 9);
    

  7. Adjusting size of custom dialog box in android
  8. Question:

    I am building an custom alert dialog box for my application i have written the code and its working but the only problem is its size it is not adjusting according to my content in dialog box. below is my code:

    public class CustomDialogBoxActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            Button button = (Button)findViewById(R.id.Button01main);
    
            button.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    final Dialog myDialog = new Dialog(CustomDialogBoxActivity.this);
                    myDialog.setContentView(R.layout.dialog_sms_layout);
                    myDialog.setTitle("Select any one");
    
    
                    Button ok = (Button)myDialog.findViewById(R.id.buttonOK);
                    final RadioGroup radioGrp = (RadioGroup)myDialog.findViewById(R.id.radioGroupSmsDialog);
    
                    final CheckBox mob1 = (CheckBox)myDialog.findViewById(R.id.checkBoxMob1);
                    final CheckBox mob2 = (CheckBox)myDialog.findViewById(R.id.checkBoxMob2);
                    mob1.setText("9029691986");
                    mob2.setText("99263911766");
    
                    ok.setOnClickListener(this);
    
                    ok.setOnClickListener(new OnClickListener() {
    
                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            if(mob1.isChecked() && mob2.isChecked()) {
                                if(radioGrp.getCheckedRadioButtonId() == R.id.radioSms) {
                                    String[] phoneArray = {mob1.getText().toString(),mob2.getText().toString()};
    
                                    Log.v("MOB1",mob1.getText().toString());
                                    Log.v("MOB2",mob2.getText().toString());
                                    Log.v("Mobile No", ""+phoneArray.toString());
                                    sendSms("SMS Message",phoneArray);
    
                                }else if(radioGrp.getCheckedRadioButtonId() == R.id.radioBusinessCard) {
                                    String[] phoneArray = {mob1.getText().toString(),mob2.getText().toString()};
    
                                    Log.v("MOB1",mob1.getText().toString());
                                    Log.v("MOB2",mob2.getText().toString());
                                    Log.v("Mobile No", ""+phoneArray.toString());
                                    sendSms("SMS Business Card",phoneArray);
                                }
                            }else if(mob1.isChecked() && !mob2.isChecked()) {
                                if(radioGrp.getCheckedRadioButtonId() == R.id.radioSms) {
                                    String[] phoneArray = {mob1.getText().toString()};
    
                                    Log.v("MOB1",mob1.getText().toString());                                
                                    Log.v("Mobile No", ""+phoneArray.toString());
                                    sendSms("SMS Message",phoneArray);
    
                                }else if(radioGrp.getCheckedRadioButtonId() == R.id.radioBusinessCard) {
                                    String[] phoneArray = {mob1.getText().toString()};
    
                                    Log.v("MOB1",mob1.getText().toString());                                
                                    Log.v("Mobile No", ""+phoneArray.toString());
                                    sendSms("SMS Business Card",phoneArray);
                                }
    
                            }else if(!mob1.isChecked() && mob2.isChecked()) {
                                if(radioGrp.getCheckedRadioButtonId() == R.id.radioSms) {
                                    String[] phoneArray = {mob2.getText().toString()};
    
                                    Log.v("MOB2",mob2.getText().toString());
                                    Log.v("Mobile No", ""+phoneArray.toString());
                                    sendSms("SMS Message",phoneArray);
    
                                }else if(radioGrp.getCheckedRadioButtonId() == R.id.radioBusinessCard) {
                                    String[] phoneArray = {mob2.getText().toString()};
    
                                    Log.v("MOB2",mob2.getText().toString());
                                    Log.v("Mobile No", ""+phoneArray.toString());
                                    sendSms("SMS Business Card",phoneArray);
                                }
    
                            }else if(!mob1.isChecked() && !mob2.isChecked()) {
                                Toast.makeText(getApplicationContext(), "Select atleast one number", Toast.LENGTH_SHORT).show();
                            }                       
                        }
                    });
                    myDialog.show();
                }
    
            });
        }
    
        public void sendSms(String message,String[] phoneNumber) {
    
            Intent smsIntent = new Intent(Intent.ACTION_VIEW);
            smsIntent.setData(Uri.parse("sms:"));
            smsIntent.putExtra("sms_body", message);
            smsIntent.putExtra("address", phoneNumber);
            startActivity(smsIntent);       
        }
    }
    

    This is the output that i'm getting on button click

    This is my main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="This is my main activity, from here, I want to display a dialog, 
                            after the user clicked the button below this text." />
    
        <Button
            android:id="@+id/Button01main"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Show Dialog" />
    
    </LinearLayout>
    

    and this is my dialog_sms_layout

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    
    <RadioGroup
            android:id="@+id/radioGroupSmsDialog"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
             >
    
            <RadioButton
                android:id="@+id/radioSms"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="SMS" />
    
            <RadioButton
                android:id="@+id/radioBusinessCard"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Card" />
        </RadioGroup>
    
    <CheckBox
        android:id="@+id/checkBoxMob1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="Primary" />
    
    <CheckBox
        android:id="@+id/checkBoxMob2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Secondary" />
    
    <Button
        android:id="@+id/buttonOK"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="OK" />
    
    </LinearLayout>
    

    I want to adjust the dialog box size please help. thanks in advance


    Solution 1:

    I'm not sure if this will work for your situation, but if you want to set it to full width or something similar, you need to get the current Window for the activity and set it's layout params like so:

    myDialog.show();
    Window window = myDialog.getWindow();
    window.setLayout(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
    

    Edit

    FILL_PARENT has been deprecated, use instead

    window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    

    Solution 2:

    You just need to add is style "Theme_AppCompat_Light_Dialog_Alert" to your dialog like this:

    Dialog dialog = new Dialog(context, R.style.Theme_AppCompat_Light_Dialog_Alert);
    

    and your dialog is looks like normal alert dialog

    Solution 3:

    I solved this by removing the title bar with this code

        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    

    and adding padding to elements in my layout to stretch the Dialog box to my liking.

  9. android - How to customize text color of the CheckBoxPreference title
  10. Question:

    How to change the color of Title text in CheckBoxPreference?


    Solution 1:

    As zenob said this can be solved by a custom layout. Preference layout defined in the framework is used in the application:

    custom_preferece_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <!-- Copyright (C) 2006 The Android Open Source Project Licensed under the 
        Apache License, Version 2.0 (the "License"); you may not use this file except 
        in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
        Unless required by applicable law or agreed to in writing, software distributed 
        under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES 
        OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
        the specific language governing permissions and limitations under the License. -->
    
    <!-- Layout for a Preference in a PreferenceActivity. The Preference is able 
        to place a specific widget for its particular type in the "widget_frame" 
        layout. -->
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent" android:layout_height="wrap_content"
        android:minHeight="?android:attr/listPreferredItemHeight"
        android:gravity="center_vertical" android:paddingRight="?android:attr/scrollbarSize">
    
        <RelativeLayout android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:layout_marginLeft="15dip"
            android:layout_marginRight="6dip" android:layout_marginTop="6dip"
            android:layout_marginBottom="6dip" android:layout_weight="1">
    
            <TextView android:id="@+android:id/title"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:singleLine="true" android:textAppearance="?android:attr/textAppearanceLarge"
                android:ellipsize="marquee" android:fadingEdge="horizontal"
                android:textColor="#FF0000" />
    
            <TextView android:id="@+android:id/summary"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:layout_below="@android:id/title" android:layout_alignLeft="@android:id/title"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:maxLines="4" />
    
        </RelativeLayout>
    
        <!-- Preference should place its actual preference widget here. -->
        <LinearLayout android:id="@+android:id/widget_frame"
            android:layout_width="wrap_content" android:layout_height="match_parent"
            android:gravity="center_vertical" android:orientation="vertical" />
    
    </LinearLayout>
    

    textColor attribute is defined for title TextView in the above layout. Use this layout with the checkbox preference in preference xml.

    mypreference.xml

    <?xml version="1.0" encoding="utf-8"?>
    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
        <CheckBoxPreference android:key="testcheckbox"
        android:title="Checkbox Title"
        android:summary="Checkbox Summary..."
        android:layout="@layout/custom_preference_layout"></CheckBoxPreference>
    </PreferenceScreen>
    

    Checkbox preference title will have the color defined in the custom layout.

    Solution 2:

    One easy solution that i found is to add this to your style

        <item name="android:textColorSecondary">#000000</item>
        <item name="android:textColorPrimary">#000000</item>
    

    textColorPrimary will change the color of title of checkBoxPreference and textColorSecondary will change the color of summary.

    Solution 3:

    You open file color.xml and change to Light Blue #4096ff:

    <color name="colorAccent">#4096ff</color> 
    

    Here is the link of Screenshot enter image description here