Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customizing the Alert dialog in Android

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.

like image 881
Greenhorn Avatar asked Jan 24 '11 13:01

Greenhorn


2 Answers

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

like image 95
McStretch Avatar answered Sep 23 '22 09:09

McStretch


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.

like image 21
chikka.anddev Avatar answered Sep 25 '22 09:09

chikka.anddev