android dialog set icon to title
Solution 1
follow this on any button
click or on your onActivity
or any place you want this dialog to show. do read my comments every after //
AlertDialog alertDialog = new AlertDialog.Builder(
this).create();
alertDialog.setTitle("TITLE"); // your dialog title
alertDialog.setMessage("Your message"); // a message above the buttons
alertDialog.setIcon(R.drawable.ic_home); // the icon besides the title you have to change it to the icon/image you have.
alertDialog.setButton("Got IT", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) { // here you can add a method to the button clicked. you can create another button just by copying alertDialog.setButton("okay")
}
});
alertDialog.show();
do me a favor and delete this
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();
}
and add mine instead !
Solution 2
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
Solution 3
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.
Marco Dinatsoli
Updated on June 08, 2022Comments
-
Marco Dinatsoli almost 2 years
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