5

I have an AlertDialog that I use a custom dialog view with. The idea of the custom title view seems simple enough, but there is a black border around the custom title that I can't seem to get rid of. The top, left and right sides have a single-pixel border, while the bottom side has about a 5 pixel border.

Creating the dialog in Java:

View titleView = inflater.inflate(R.layout.part_list_item, parent, false);
((TextView) titleView.findViewById(R.id.partName)).setText(titleText);
AlertDialog productDialog = new AlertDialog.Builder(getContext())
    .setCustomTitle(titleView)
    .setAdapter(adapter, doNothingClickListener)
    .create();

Custom title view layout, part_list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#ff0000"
    android:id="@+id/partName"
    android:layout_marginLeft="6dip"
    android:textAppearance="?android:attr/textAppearanceLargeInverse"
    />

What I see:

screenshot broken

What I want to see:

screenshot fixed

Any ideas?

Jacob Marble
  • 28,555
  • 22
  • 67
  • 78

2 Answers2

3

Try this:

LayoutInflater inflater = (LayoutInflater)yourClass.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View titleView = inflater.inflate(R.layout.custom_dialog, null);

((TextView) titleView.findViewById(R.id.partName)).setText("Your Title");
alert1.setCustomTitle(titleView);
Jacob Marble
  • 28,555
  • 22
  • 67
  • 78
Dinesh Anuruddha
  • 7,137
  • 6
  • 32
  • 45
0

This is the result created by the android when you have a title on the alert. From what i can see of the screen-shots, the "body" of the alert is also a custom view and not the alert message property.

So the easiest way to have the result you want is to add the title layout in the custom view of the alert.

example:

View titleView = inflater.inflate(R.layout.part_list_item, parent, false);

View bodyView = ....
bodyView.addview(titleView);
((TextView) itleView.findViewById(R.id.partName)).setText(titleText); 

AlertDialog productDialog = new AlertDialog.Builder(getContext());
productDialog.setView(bodyView);
...

productDialog.create();

Where the bodyView.addview(titleView); adds the title layout on your body of the alert.

And the productDialog.setView(bodyView); sets the custom view as the body of your alert.

  • 1
    What advantage does your approach have to dropping `AlertDialog` in favor of a custom `Dialog`? For me, the point in using `AlertDialog` is that there's already a title and a body that I can simply fill in. – Jacob Marble Apr 19 '12 at 06:05
  • I am simply suggesting that you can add the titleView on the "body" view. that will solve the issue with the border. Because the border is set by default between the title and the message! – Thanos Karpouzis Apr 19 '12 at 06:33