Android - changing dialog box title background

I am creating a custom dialog and I want to know how to change the background of the title bar.

I tried two approaches:

1 - I tried the AlertDialog.Builder 'setCustomTitle' method. I created a simple layout containing a text representation with the width of the layout and the height of match_parent and the background color. When I launch the application, only the upper half of the title bar displays the background color. The bottom half still shows the default background color of the theme. Does anyone know why?

2 - I created my own dialogue topic. Ive created a style with parental inheritance in "@android: style / Theme.Holo.Light.Dialog". Then I passed this in the constructor of AlertDialog.Builder - the new AlertDialog.Builder (this, R.style.test_dialog). This seems good, but somehow the dialog ends in a dialog box. A square rectangle surrounds the dialog. Does anyone know why?

+9
android dialog
source share
6 answers

You can create a style, for example,

<style name="cust_dialog" parent="@android:style/Theme.Dialog"> <item name="android:windowTitleStyle">@style/dialog_title_style</item> </style> <style name="dialog_title_style" parent="android:Widget.TextView"> <item name="android:background">@android:color/black</item> <item name="android:padding">10dp</item> </style> 

And you can create a dialog box:

 Dialog dialog=new Dialog(this,R.style.cust_dialog); dialog.setContentView(R.layout.fragment_features_dialog); dialog.setTitle(R.string.features); 

A dialog box now appears with a black background for the title bar.

+36
source share

The appearance of the dialog box is caused by the background of the dialog box. In each dialog box, this is, but in the Android dialog box, the background is set to transparent by default. To do this, add this element to your custom dialog topic:

 <style name="CustomDialog" parent="@android:style/Theme.Holo.Dialog"> <item name="android:windowBackground">@android:color/transparent</item> </style> 
+4
source share

I am using Mono Android (Xamarin); I will show you another alternative that I use in my application to create a dialog in a fragment:

 Dialog itemDialog = new Dialog(this.Activity); TextView alertTitle=(TextView)itemDialog.Window.DecorView.FindViewById(Android.Resource.Id.Title); alertTitle.SetTextColor(Android.Graphics.Color.Blue); alertTitle.SetBackgroundColor(Android.Graphics.Color.Orange); itemDialog.SetContentView(Resource.Layout.listview_custom_dialog); string[] options = new string[] { "Open", "Mark as Unread","Mute","View Profile","Block Connection","Delete Conversation" }; ArrayAdapter<string> adapter = new ArrayAdapter<string>(this.Activity, Resource.Layout.listitem_custom_dialog,Resource.Id.textViewDialogDescription, options); 

Resource.Layout.listitem_custom_dialog: this is a custom list layout, here is the XML file:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/transparent" > <TextView android:id="@+id/textViewDialogDescription" android:layout_width="match_parent" android:layout_height="44dp" android:background="#ffffff" android:textColor="#386B96" android:paddingLeft="4dp" android:textSize="14dp" /> </RelativeLayout> ListView lv = itemDialog.FindViewById<ListView> (Resource.Id.listViewDialogItems); lv.Adapter = adapter; adapter.NotifyDataSetChanged(); itemDialog.SetCancelable(true); itemDialog.SetTitle("Conversation"); itemDialog.Show(); 

Android.Resource.Id.Title : this is the identifier of the text field containing the title of the dialog. and it is predetermined by android. this way you get a dialog that you can create as you want.

+1
source share

Create an xml layout file for your header and inflate it and set AlertDialog as

 View view = getLayoutInflater().inflate(R.layout.cust_dialog_title, null); alertDialog.setCustomTitle(view); 
+1
source share

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> 
+1
source share
  <style name="question_dialog" parent="@android:style/Theme.Holo.Dialog"> <item name="android:textColorPrimary">@android:color/black</item> <item name="android:background">#ffffff</item> <item name="android:windowTitleStyle">@style/question_dialog_title</item> <item name="android:textColor">#000000</item> </style> <style name="question_dialog_title" parent="android:Widget.TextView"> <item name="android:background">#ffffff</item> <item name="android:textColor">#000000</item> <item name="android:textAlignment">center</item> <item name="android:text">Please select time</item> <item name="android:textSize">21sp</item> </style> 

Code in activity

  final Dialog d = new Dialog(QuickLaunch.this,R.style.question_dialog); 
0
source share

All Articles