Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the color of AlertDialog title and the color of the line under it

I changed the color of an AlertDialog title using this command

alert.setTitle( Html.fromHtml("<font color='#FF7F27'>Set IP Address</font>")); 

But I want to change the color of the line that appear under the title; how can I do that ?

Note: I don't want to use a custom layout

screenshot of the desired effect

like image 714
Mohammed Subhi Sheikh Quroush Avatar asked Jan 21 '13 13:01

Mohammed Subhi Sheikh Quroush


People also ask

How do I change the color of my AlertDialog button?

You can just create a style and apply that theme on the dialog box. So whenever you want to change the color of AlertDialog box, just change color in styles. xml and all the dialog boxes will be updated in the whole application.

How do I change the background color on AlertDialog builder?

Use setInverseBackgroundForced(true) on the alert dialog builder to invert the background.


2 Answers

Unfortunately, this is not a particularly simple task to accomplish. In my answer here, I detail how to adjust the color of a ListSeparator by just checking out the parent style used by Android, creating a new image, and creating a new style based on the original. Unfortunately, unlike with the ListSeparator's style, AlertDialog themes are internal, and therefore cannot be referenced as parent styles. There is no easy way to change that little blue line! Thus you need to resort to making custom dialogs.

If that just isn't your cup of tea... don't give up! I was very disturbed that there was no easy way to do this so I set up a little project on github for making quickly customized holo-style dialogs (assuming that the phone supports the Holo style). You can find the project here: https://github.com/danoz73/QustomDialog

It should easily enable going from boring blue to exciting orange!

enter image description here

The project is basically an example of using a custom dialog builder, and in the example I created a custom view that seemed to cater to the IP Address example you give in your original question.

With QustomDialog, in order to create a basic dialog (title, message) with a desired different color for the title or divider, you use the following code:

private String HALLOWEEN_ORANGE = "#FF7F27";  QustomDialogBuilder qustomDialogBuilder = new QustomDialogBuilder(v.getContext()).     setTitle("Set IP Address").     setTitleColor(HALLOWEEN_ORANGE).     setDividerColor(HALLOWEEN_ORANGE).     setMessage("You are now entering the 10th dimension.");  qustomDialogBuilder.show(); 

And in order to add a custom layout (say, to add the little IP address EditText), you add

setCustomView(R.layout.example_ip_address_layout, v.getContext()) 

to the builder with a layout that you have designed (the IP example can be found in the github). I hope this helps. Many thanks to Joseph Earl and his answer here.

like image 125
Daniel Smith Avatar answered Oct 20 '22 23:10

Daniel Smith


Divider color:

It is a hack a bit, but it works great for me and it works without any external library (at least on Android 4.4).

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle(R.string.dialog)        .setIcon(R.drawable.ic)        .setMessage(R.string.dialog_msg); //The tricky part Dialog d = builder.show(); int dividerId = d.getContext().getResources().getIdentifier("android:id/titleDivider", null, null); View divider = d.findViewById(dividerId); divider.setBackgroundColor(getResources().getColor(R.color.my_color)); 

You can find more dialog's ids in alert_dialog.xml file. Eg. android:id/alertTitle for changing title color...

UPDATE: Title color

Hack for changing title color:

int textViewId = d.getContext().getResources().getIdentifier("android:id/alertTitle", null, null); TextView tv = (TextView) d.findViewById(textViewId); tv.setTextColor(getResources().getColor(R.color.my_color)); 
like image 31
maresmar Avatar answered Oct 21 '22 01:10

maresmar