Android Alert Dialog will replace blue by default with a different color

I am using a one-choice warning dialog box in which I would like to replace the blue (by default) and the line with the switches with orange, which I use in the title bar. I managed to get to the title bar using setCustomTitle() , but I got lost trying to get rid of this damned blue.

For title bar

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@color/orange" > <TextView android:id="@+id/alertTitle" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="14dp" android:gravity="center" android:text="Alert Title" android:textColor="@color/white" android:textSize="18sp" /> </LinearLayout> 

Create alert

 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); View customTitle = View.inflate(MainActivity.this, R.layout.custom_alert_title, null); builder.setCustomTitle(customTitle); builder.setSingleChoiceItems(mAlertOptions, -1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // do stuff dialog.dismiss(); } }).create().show(); 

It looks like this:

enter image description here

I need to get rid of this blue! Help!

+4
source share
1 answer

The only way to change the color of the header separator is to use the Resources.getIdentifier in combination with Window.findViewById . The flag can be easily changed by calling AlertDialog.Builder.setSingleChoiceItems(ListAdapter, int, OnClickListener) .

Here is an example:

Single select item layout : I generated your_radio_button using Android Holo Colors.

 <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" android:layout_width="match_parent" android:layout_height="wrap_content" android:checkMark="@drawable/your_radio_button" android:ellipsize="marquee" android:gravity="center_vertical" android:minHeight="?android:attr/listPreferredItemHeightSmall" android:paddingEnd="16dip" android:paddingStart="16dip" android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="?android:attr/textColorAlertDialogListItem" /> 

Implementation

  final ListAdapter adapter = new ArrayAdapter<String>(this, R.layout.select_dialog_singlechoice, android.R.id.text1, new String[] { "Option 1", "Option 2" }); final AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setCustomTitle(getLayoutInflater().inflate(R.layout.custom_alert_title, null)); builder.setSingleChoiceItems(adapter, -1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // Do something } }); // Show the AlertDialog final AlertDialog dialog = builder.show(); // Change the title divider final Resources res = getResources(); final int titleDividerId = res.getIdentifier("titleDivider", "id", "android"); final View titleDivider = dialog.findViewById(titleDividerId); titleDivider.setBackgroundColor(res.getColor(android.R.color.holo_orange_dark)); 

results

Example

+11
source

Source: https://habr.com/ru/post/1214603/


All Articles