I'm using an AlertDialog with custom layout. The color of TextView in the layout is black, so when opening the dialog on Android 4.0 with Holo.Light, the text is visible. However if you open the dialog in Android 2.2 the text is not visible because of the gray background. Is there a way to change the background color?
on your dialog builder. It will force the background to white color (instead of dark grey) on android version before Froyo.
To change the background color of all dialogs and pop-ups in your app, use colorBackgroundFloating attribute. Show activity on this post. I was looking at the material.io/components/dialogs/android#full-screen-dialog where the container color element has no attribute.
You need to wrap your Dialog in a Builder like this. After that dialogBackgroundColor will have an effect. Show activity on this post. You can now use backgroundColor property of AlertDialog to change the color.
setBackgroundColor(Color. TRANSPARENT); wv. setPadding(5, 25, 5, 0); ImageView imgcartl=(ImageView)layout.
However if you open the dialog in Android 2.2 the text is not visible because of the gray background. Is there a way to change the background color?
Yes it is possible, I used it on my app using DialogBuilder. Just put inverseBackgroundForced to true
builder.setInverseBackgroundForced(true);
AlertDialog dialog = builder.create();
dialog.show();
on your dialog builder. It will force the background to white color (instead of dark grey) on android version before Froyo.
Just define the background of the root view in the layout.xml file for your dialog to a color that you want.
Like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/dialog_background" >
...
Thank you very much to StinePike and Artjom B.
The idea of StinePike is very good.
I put a TextView in AlertDialog having a customized background.
I show how to use solid and gradient background to customize objects.
Please let me to present you the context in which I applied StinePike's Idea.
// location: MainActivity.java
AlertDialog mAlertDialog_With_Radio_Buttons;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ini();
}
public void onAlert_With_Radio_Buttons_Close_Click(View view) {
mAlertDialog_With_Radio_Buttons.dismiss();
} // onAlert_With_Radio_Buttons_Close_Click
public void alert_with_radio_buttons(){
AlertDialog.Builder
mAlertDialog_Builder = new AlertDialog.Builder(this);
mAlertDialog_Builder
.setView(getLayoutInflater()
.inflate(R.layout.alert_with_radio_buttons, null));
mAlertDialog_Builder
.setTitle("Select The Directory");
mAlertDialog_With_Radio_Buttons = mAlertDialog_Builder.create();
mAlertDialog_With_Radio_Buttons.show();
} // public void alert_with_radio_buttons(){
// location: alert_with_radio_buttons.xml in layout
<LinearLayout
android:id="@+id/alert_with_radio_buttons_tv_ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/turquoise1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<TextView
android:id="@+id/mAlert_With_Radio_Buttons_TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:background="@color/turquoise2"
android:textSize="@dimen/main_wiz_size"
android:text = "@string/alert_with_rb_tv_text" />
</LinearLayout>
// Location: colors in values
<color name="turquoise1">#FF00ABAB</color>
<color name="turquoise2">#FF00BCBC</color>
// Location: strings in values
<string name="alert_with_rb_tv_text">Directory Names</string>
// Close Definition
// location: alert_with_radio_buttons.xml in layout
<Button
android:id="@+id/alert_with_radio_buttons_close_btn"
android:text="@string/alert_with_radio_buttons_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/btn_decor"
android:onClick="onAlert_With_Radio_Buttons_Close_Click" />
// location: btn_decor.xml in drawable
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true">
<gradient
android:startColor="#700000ff"
android:endColor="#70009B80"
android:angle="-90"/>
</shape>
location: strings.xml in values
<string name="alert_with_radio_buttons_close">Close</string>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With