Android: how to customize AlertDialog, Dialog Spinner headers

I want to customize all dialogs, alertDialogs and spinner. I want to change the text color and background of the title bar.

enter image description here

I am trying to explain better: I want the text (JOLLY BAR DI ...) to be white and the background of the title (from the top of the dialog to the soft blue line below the title is on) blue (# 044592).

How can i do this?

+3


source to share


1 answer


Ok, I almost solved the problem (I am answering my own question because it might help others with the same problem as mine). The problem remains with spinners, DatePickers, etc.

For AlertDialogs, I do something like this:

AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this, AlertDialog.THEME_HOLO_LIGHT);
//Then I do what I need with the builder (except for setTitle();
LayoutInflater inflater = getLayoutInflater();
View view=inflater.inflate(R.layout.dialog_custom_title, null);
TextView title = (TextView)view.findViewById(R.id.myTitle);
title.setText("Title I want to show");
builder.setCustomTitle(view);
AlertDialog alert = builder.create();
alert.show();

      

dialog_custom_titile.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#044592" >

   <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/myTitle"
        android:layout_width="fill_parent"
        android:textSize="20dp"
        android:layout_height="70dp"
        android:layout_marginLeft="25dp"
        android:paddingTop="22dp"
        android:textColor="@android:color/white"
        android:textStyle="bold" />

</RelativeLayout>

      

for operations that I want to see as dialog boxes. I like the following: I declare them in Manifest.xml like this:

<activity
     android:name="com.aveschini.AnotherActivity"
     android:screenOrientation="portrait"
     android:label="My Dialog"
     android:theme="@style/MyDialog"
     android:windowSoftInputMode="adjustPan" />

      



then in the res / values ​​/ styles.xml file I like the following:

    <style name="AppTheme" parent="android:Theme.Holo.Light" />

    <style name="MyDialog" parent="android:Theme.Holo.Light.Dialog">
        <item name="android:windowTitleStyle">@style/DialogWindowTitle</item>
    </style>

    <style name="DialogWindowTitle">
        <item name="android:textAppearance">@style/TextAppearance.DialogWindowTitle</item>
        <item name="android:background">#044592</item>
    </style>

    <style name="TextAppearance.DialogWindowTitle" parent="android:TextAppearance">
        <item name="android:textColor">@android:color/white</item>
        <item name="android:textStyle">bold</item>
        <item name="android:textSize">20sp</item>
    </style>

      

I am still getting a light blue separator between title and body ... still working on it. As said, I still don't know how to deal with Spinners, DatePicker, etc.

What's the result: AlertDialog:

enter image description here

And activity with the kind of dialogue:

enter image description here

+7


source







All Articles