Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android dialog set icon to title

I have this dialog class and i want to set icon to its title:

public class DialogMealInformation extends Dialog implements
        android.view.View.OnClickListener {
    Context context;
    private TextView tv_information;
    private Button b_ok;
    private String information;

    public DialogMealInformation(Context context, String information) {
        // TODO Auto-generated constructor stub
        super(context);
        this.context = context;
        this.information = information;
        setContentView(R.layout.dialog_meal_information);
        getWindow().setLayout(android.view.ViewGroup.LayoutParams.FILL_PARENT,
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
        setTitle("Info !");

        initialize();
    }

it tried like this:

setTitle(R.layout.dialog_simple_header);

dialog_simple_header.xml

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

    <TextView
        android:id="@+id/tv_dialog_simple_header_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tv_information"
        android:drawableLeft="@drawable/more_information" />

</LinearLayout>

but the title after that was "res/layout/dialog_simple_header.x" just text, no icon is appear, why please , what is the solution , thanks alot

like image 758
Marco Dinatsoli Avatar asked Feb 17 '13 19:02

Marco Dinatsoli


2 Answers

here is a dialog with custom theme and View

        final Dialog dialog = new Dialog(mContext,android.R.style.Theme_Translucent_NoTitleBar);
        dialog.setContentView(R.layout.dialog_layout_third);
        ImageView image = (ImageView) dialog.findViewById(R.id.image2);
        //image.setImageResource(R.drawable.ic_launcher);
        image.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                dialog.dismiss();
            }
        });

        dialog.show();
        dialog.setCancelable(false);

here i'm setting the theme to transparent and then i'm using dialog.SetContentView to add the layout. in my layout i'm using only an imageview and here is my layout for the dialog.

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:id="@+id/onetimeedit"
    android:visibility="visible"
    >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <ImageView
            android:id="@+id/image2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"

            android:src="@drawable/drag" />

    </RelativeLayout>

</FrameLayout>

you can then add textView as Title just add it to the layout. and or using dialog.setTitle("MyTitleShouldByHere");

hope my example clear it for you, and if that what you want please do accept the answer so other people can get it easy. thanks

like image 97
Kosh Avatar answered Nov 16 '22 00:11

Kosh


Dialog.setTitle(int resId) is used if you wish to set the title text to a string resource.

What you are looking for is what you are already doing - setContentView. In your custom xml there make the title look like what you prefer, or - if you wish to set it at runtime, just get a reference to the ImageView and set it in code.

Hope this helps.

like image 1
Valentin Avatar answered Nov 15 '22 22:11

Valentin