Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Icons in custom dialogs android

Is there a way to set an icon on a custom dialog without using the AlertDialog methods? Dialog has title, but is missing that nice divider and the ability to set an icon, but surely there must be a way of getting both without having to use AlertDialog?

like image 213
Chris Avatar asked Nov 29 '22 17:11

Chris


2 Answers

You can add an icon with the following code:

Dialog dialog = new Dialog(context);

dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Dialog Title");

dialog.show();
dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.your_icon);

For a divider you can simply add an ImageView to your dialog layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ImageView
        android:src="@android:drawable/divider_horizontal_dim_dark"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:text="content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
like image 63
Gubbel Avatar answered Dec 02 '22 07:12

Gubbel


A nice way of adding a divider is by using a gradient shape.

Simply make a file gradient.xml or so, in your res/drawable/ catalog and put something like this into it:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape="rectangle">

<gradient android:startColor="#424542" 
          android:centerColor="#FFFFFF"
          android:endColor="#424542" 
          android:angle="0" />
</shape>

And then inside your LinearLayout you can put a View:

<View android:id="@+id/divider" 
      android:layout_width="fill_parent"
      android:layout_height="1dip"
      android:background="@drawable/gradient">
</View>

Then it paints a nice gradient divider :)

like image 33
lobner Avatar answered Dec 02 '22 07:12

lobner