6

I want to show text in dark color on a light background on alert dialog. But I can't figure out how to do this. Please help me.

Thanks.

Greenhorn
  • 769
  • 2
  • 8
  • 17

2 Answers2

7

You can create your own layout in an XML View just like you would for an Activity:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/layout_root"
              android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:padding="10dp"
              >
    <ImageView android:id="@+id/image"
               android:layout_width="wrap_content"
               android:layout_height="fill_parent"
               android:layout_marginRight="10dp"
               />
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="fill_parent"
              android:textColor="#FFF"
              />
</LinearLayout>

Then you can use this View in the Dialog by calling setContentView(View) on the Dialog:

Context mContext = getApplicationContext();
Dialog dialog = new Dialog(mContext);

dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Dialog");

TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.android);

As the example shows, you'll have to set some of the values after you declare the content view.

Example provided from http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog

McStretch
  • 20,495
  • 2
  • 35
  • 40
  • According to google's Documentation you should avoid instantiating the `Dialog` class directly. Better to use `DialogFragment` – Mahdi-Malv Aug 08 '17 at 12:44
6

see this example,It will help you:http://www.helloandroid.com/tutorials/how-display-custom-dialog-your-android-application


as in this example layout defined in file for alert dialog.You can set your style for alert dialog.

Yauraw Gadav
  • 1,706
  • 1
  • 18
  • 39
chikka.anddev
  • 9,569
  • 7
  • 38
  • 46