Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the background of Android Alert Dialog Title

I want to change the background of the Alert dialog. I have tried following code piece to do so:

 <style name="Theme.AlertDialog" parent="@android:style/Theme.Holo.Light.Dialog">
        <item name="android:background">@android:color/transparent</item>
        <item name="android:textColor">#659F26</item>
        <item name="android:windowTitleStyle">@style/Theme.AlertDialog.Title</item>
    </style>

    <style name="Theme.AlertDialog.Title" parent="@android:style/TextAppearance.DialogWindowTitle">
        <item name="android:background">@drawable/cab_background_top_example</item>
    </style>

Java Code:

ContextThemeWrapper ctw = new ContextThemeWrapper( this, R.style.Theme_AlertDialog);
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ctw);

alertDialogBuilder.setTitle(getResources().getString(R.string.confirmation));   
alertDialogBuilder.setMessage(getResources().getString(R.string.msg));       
alertDialogBuilder.show();

My app is showing dialog like this:

enter image description here

while i want it to look like:

enter image description here

Please suggest, what i am doing wrong.

like image 332
mudit Avatar asked Jul 23 '13 07:07

mudit


People also ask

How do I change the background color of alert in dialog?

To change the background color of all dialogs and pop-ups in your app, use colorBackgroundFloating attribute. Save this answer. Show activity on this post. I was looking at the material.io/components/dialogs/android#full-screen-dialog where the container color element has no attribute.

How do I change my dialog alert font?

To do this you use alert builder to build your alert. You then get the TextView from this alert and then you set the typeface for the alert. That's good, remember to approve and upvote once you get the privileges so other people can get this help too.


1 Answers

Use this code when creating a dialog:

 dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);   

create your own layout As many customise done with title then set your layout

  dialog.setContentView(R.layout.yourlayout);

NOTE: use

dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); before     
dialog.setContentView(R.layout.yourlayout);

otherwise it give error.

like image 112
Dixit Patel Avatar answered Sep 20 '22 08:09

Dixit Patel