Android AlertDialog Header Background Color

Is there a way to change the background color of the AlertDialog header (android.support.v7.app.AlertDialog)? I am currently in my topic

<item name="alertDialogTheme">@style/AppCompatAlertDialogStyle</item> <style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert"> <item name="colorAccent">@color/colorAccent</item> </style> 

and I get it like this:

enter image description here

How can I do it this way

enter image description here

Using

 <style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert"> <item name="colorAccent">@color/colorAccent</item> <item name="android:windowTitleStyle">@style/DialogTitle</item> </style> <style name="DialogTitle"> <item name="android:background">@color/colorPrimary</item> </style> 

gives

enter image description here

Any ideas on how to do this?

+5
source share
2 answers

You can just customize a custom header like this

 LayoutInflater inflater = this.getLayoutInflater(); View titleView = inflater.inflate(R.layout.custom_title, null); new AlertDialog.Builder(SubCategoryActivity.this) .setCustomTitle(titleView); 

and in custom_title layout you can create a custom header like this

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:paddingLeft="10dp" android:paddingRight="10dp" android:id="@+id/llsubhead" android:background="@color/colorPrimary"> <TextView android:id="@+id/exemptionSubHeading4" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:layout_weight="1" android:text="Exemption Sub Head" android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium" android:textColor="@color/white" /> </LinearLayout> </LinearLayout> 
+6
source

Use custom Alerbox, use this code when clicked. I created a custom layout "alert_input" and the "OK" and "Cancel" options will appear

  LayoutInflater layoutInflater = LayoutInflater.from(Login.this); View promptView = layoutInflater.inflate(R.layout.alert_input, null); final EditText editText = (EditText) promptView.findViewById(R.id.alertEdit2); final EditText editText2 = (EditText) promptView.findViewById(R.id.alertEdit3); final TextView at=(TextView)findViewById(R.id.alertText); AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Login.this,AlertDialog.THEME_HOLO_LIGHT); alertDialogBuilder.setView(promptView); alertDialogBuilder.setCancelable(false) .setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { } }) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); // create an alert dialog AlertDialog alert = alertDialogBuilder.create(); alert.show(); 
0
source

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


All Articles