7

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?

2
  • 1
    Declare your custom layout in an xml file and than just set the view for your dialog. Jan 23, 2013 at 14:35
  • You can make custom DlertDilaog. See this. Jan 23, 2013 at 14:36

5 Answers 5

9

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.

2
  • Cleaner than setting background of the View being added (it would show the View being white but around it would be all gray). I was trying it for quite some time before finding this question. Thanks. :)
    – Sufian
    Oct 14, 2013 at 12:03
  • Same as for the accepted answer, I can comment here: It works OK for the main body of the Dialog, but Header with Title and Footer with OK button are still system defined "dark" color (for the OS 2.3). Dec 29, 2013 at 8:47
1

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" >
...
1
  • 1
    It works OK for the main body of the Dialog, but Header with Title and Footer with OK button are still system defined "dark" color (for the OS 2.3). Dec 29, 2013 at 8:46
1

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>
1
  • Thank you very much for your help. I put a TextView into AlertDialog having a customized background to keep my answer in the topic. I fallow your excellent piece of advice "Stand Alone Answer". Please help me to find out how to insert an image next to my comment. Jul 4, 2015 at 19:30
1

"Is there a way to change the background color?" Yes there are several ways for different contexts.

Please let me to "provide details and share my research" to you. My code shows how to get customized TextView Background for items of ListView incorporated in Alert Dialog.

Let's start with the model for the item of ListView
// location: customized_tv_for_list_view.xml from layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"
android:layout_height="fill_parent"

android:background="@drawable/layer_border">

<TextView
android:id="@+id/text_view_for_lv"

android:layout_width="fill_parent"
android:layout_height="wrap_content"

android:layout_gravity ="center"

android:padding ="5dip"
android:background="@color/turquoise2"

android:textSize="@dimen/lv_text_size"

android:textColor="@color/blue0"/>

</LinearLayout>

// location: main_activity.xml in layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/decor"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/main_activity_files_btn_ll"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/layer_border" >

<Button
    android:text="Files"
    android:id="@+id/files_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/btn_decor"
    android:onClick="onMainActivity_Files_Click" />

</LinearLayout>

</LinearLayout>

// location: colors.xml in values
<color name="blue0">#0000FF</color>
<color name="turquoise2">#FF00BCBC</color>

// location: dimens.xml in values
<dimen name="lv_text_size">24dp</dimen>

// location: layer_border.xml in drawable
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke  android:width="1dp" android:color="#9999FF" />
<solid   android:color="#CCCCFF" />
<padding android:left ="4dp" android:top="4dp"
android:right="4dp" android:bottom="4dp" />
<corners android:radius="4dp" />
</shape>

// location: decor.xml in drawable
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true">
<gradient
android:startColor="#aa0000ff"
android:endColor="#aa009B80"
android:angle="-90"/>
</shape>

// location: MainActivity.java
ListView mListView;
AlertDialog mAlertDialog;
ArrayAdapter<String> mArrayAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mListView = new ListView(this);
ArrayList<String>
mArrayList_Days = new ArrayList<>();

for(int i = 0; i< 32; i++)
mArrayList_Days.add("Day " + String.valueOf(i));

mArrayAdapter = new ArrayAdapter<>(
this, R.layout.customized_tv_for_list_view,
R.id.text_view_for_lv, mArrayList_Days);

mListView.setAdapter(mArrayAdapter);        
mListView
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String sel_item = (String) mListView
.getItemAtPosition(position);

Toast.makeText(MainActivity.this, sel_item, Toast.LENGTH_SHORT).show();
mAlertDialog.cancel();
} //  onItemClick
}); // .setOnItemClickListener

build_files_alert_dialog();
}

public void build_files_alert_dialog() {
AlertDialog.Builder
mAlertBuilder = new AlertDialog.Builder(MainActivity.this);

mAlertBuilder.setTitle("Days");
mAlertBuilder.setView(mListView);
mAlertDialog = mAlertBuilder.create();

WindowManager.LayoutParams mLayoutParams = new WindowManager.LayoutParams();
mLayoutParams.copyFrom(mAlertDialog.getWindow().getAttributes());
}

public void onMainActivity_Files_Click(View view) {
mAlertDialog.show();
} // onMainActivity_Files_Click
0
AlertDialog.Builder.setView(getLayoutInflater().inflate(R.layout.your_layout, null));

by using this function you can inflate a layout to your dialogue. now do whatever you want in the layout xml. for example see the following code.

AlertDialog.Builder about = new AlertDialog.Builder(this);    
about.setTitle(getString(name));
about.setIcon(R.drawable.icon);
about.setView(getLayoutInflater().inflate(R.layout.your_layout, null));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.