I'm developing an Android
application where I have Dialogs
with custom layout
. At first I decided to use AlertDialog
without title and without buttons, since I have my own Buttons
in my custom layout
. This works fine in Android
4.4, but not in Android
2.3.
This is how it's seen in Android
4.4:
This is how it's seen in Android
2.3:
Please notice the weird gray background behind my Dialog
.
In SO I saw that some people solved it using Dialog
instead of AlertDialog
. I tried it and it really fixed it, but now my Dialog
fills the whole screen:
And I don't want that, I want the Dialog
to have some margin like the AlertDialog
has.
This is my layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/dialog_background"
android:orientation="vertical" >
<TextView
android:id="@+id/adTitleTextView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:layout_marginTop="1dp"
android:background="@drawable/background_green"
android:gravity="center"
android:textColor="#FFFFFFFF"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#FFFFFFFF" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:paddingBottom="15dp"
android:paddingTop="10dp"
android:textColor="#FFFFFFFF" />
<LinearLayout
android:id="@+id/adButtonBar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
<Button
android:id="@+id/adNegativeButton"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/selector_button_alert_dialog"
android:textColor="#FFFFFFFF"
android:textSize="14sp" />
<Button
android:id="@+id/adNeutralButton"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/selector_button_alert_dialog"
android:textColor="#FFFFFFFF"
android:textSize="14sp" />
<Button
android:id="@+id/adPositiveButton"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/selector_button_alert_dialog"
android:textColor="#FFFFFFFF"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
This is how I create my Dialog
:
Dialog mDialog = new Dialog( this, android.R.style.Theme_Dialog );
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
View content = getLayoutInflater().inflate( R.layout.alert_dialog_layout, null);
((TextView)content.findViewById(R.id.adTitleTextView)).setText( title );
((TextView) content.findViewById( R.id.adBodyTextView )).setText( message );
mDialog.setContentView( content );
Button b = (Button) content.findViewById(R.id.adPositiveButton);
b.setText( posText );
b.setOnClickListener( this );
b = (Button) content.findViewById(R.id.adNegaveButton);
b.setText( negText );
b.setOnClickListener( this );
mDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
mDialog.show();
And this is how I create the AlertDialog
:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder( ctx );
alertDialogBuilder.setView( content );
final AlertDialog mDialog = alertDialogBuilder.create();
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
...
Anybody knows how can I remove that weird background from AlertDialog
in Android
2.3 OR how to make Dialog
not to fill the whole screen?
Thank you!