android dialog set icon to title - android

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

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 !

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

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.

Related

QustomDialogBuilder can't find item

I need to change the appearence of my dialogs so I decided to use QustomDialog.
I'm using a custom layout via setCustomView, but when i handle onClick of PositiveButton I can't find any element of my layout.
The layout consist in a EditText:
<?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="vertical">
<EditText
android:id="#+id/setServer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/server_url" />
</LinearLayout>
The Listener is:
QustomDialogBuilder builder = new QustomDialogBuilder(this);
/* Some customization */
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
String url = ((EditText) findViewById(R.id.setServer)).getText().toString();
if (Utilities.isUrl(url)) {
PreferenceHelper.setRestUrl(getApplicationContext(), url);
} else {
Toast.makeText(getApplicationContext(), "Invalid URL", Toast.LENGTH_SHORT);
}
}
});
But when i press "OK" i get java.lang.ClassCastException: com.android.internal.view.menu.ActionMenuItemView cannot be cast to android.widget.EditText.
How can I solve this?
Probably the findViewById method is looking in the wrong layout (in the activity layout instead of the dialog).
You can try to create a final variable with the Dialog when it is shown:
final AlertDialog dialog = builder.show();
And then use it in the findViewById:
String url = ((EditText) dialog.findViewById(R.id.setServer)).getText().toString();

How to customize the Title Section of a Dialog

I have created a customized dialog and applied the layout xml to it. But the layout is always applied to the body of the dialog and never to the title section. All what I can do is to set the title of the dilaog programmatically using setTitle and add an icon using setFeatureDrawableResource. Please let me know how to customize the title section of a customized dialog?
Side Question: today when I accessed my stackoverflow account I found that, there are more than 200 points are deducted? any Idea why?
Java_Code:
reportDialog = new Dialog(MeetingPointFix.this);
reportDialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
reportDialog.setCancelable(false);
LayoutInflater reportAlertDialogLayoutInflater = LayoutInflater.from(getApplicationContext());
View reportAlertDialogInflatedView = reportAlertDialogLayoutInflater.inflate(R.layout.meetingpointfix_report_dialog, null);
reportDialog.setContentView(reportAlertDialogInflatedView);
int [] viewsRefsIds = {R.id.reportLocNameValue, R.id.reportLocLatValue, R.id.reportLocLngValue, R.id.reportTimeValue,
R.id.reportDateValue, R.id.reportImgTitleValue, R.id.reportImgPathValue
};
reportDialog.setTitle(REPORT_ALERT_DIALOG_TITLE);
reportDialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,R.drawable.reporticon01);
TextView reportDialogMSG = (TextView) reportDialog.findViewById(R.id.reportDialogMessageValue);
Button reportOkBtn = (Button) reportDialog.findViewById(R.id.reportOkBtnID);
Button reportNavigateBtn = (Button) reportDialog.findViewById(R.id.reportNavigateBtnID);
Yes , I agree that some times the default dialog title doesn'nt match the theme style of your app .
Luckily android provides you a way to update the title layout you just need to take care of these three lines of code while you are creating your dialog.
dialog.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
dialog.setContentView(R.layout.test);
dialog.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.dialog_title_test);
Make sure that the call to setContentView() occurs after requestWindowFeature() and before setFetureInt()
So , suppose for a dialog Fragment you can do that as
public class DialogTest extends DialogFragment{
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
dialog.setContentView(R.layout.test);
dialog.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.dialog_title_test);
return dialog;
}
}
Happy Coding ! :)
add the following to your styles xml file:
<style name="FullHeightDialog" parent="android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
</style>
use this to create the dialog (modify as you wish and set your ids for buttons)
private void showDialog() {
final Dialog dialog = new Dialog(this, R.style.FullHeightDialog);
dialog.setContentView(R.layout.alert_dialog); //replace with your layout xml
dialog.setCancelable(false);
Button ignoreButton = (Button) dialog.findViewById(R.id.ignore_button);
ignoreButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
Button answerButton = (Button) dialog.findViewById(R.id.answer_button);
answerButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.show();
The easiest way would be to use this library. I read other answers in this post and wanted to add one like to the code similar to theirs. You can provide custom title bar in your layout file itself. See this code:
public void createDialogLanguage() {
ListView listViewLanguage;
final Dialog dialog = new Dialog(getActivity());
dialog.getWindow().addFlags(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawableResource(
android.R.color.transparent);
dialog.setContentView(R.layout.dialog_language);
dialog.show();
}
Please note this line:
dialog.getWindow().setBackgroundDrawableResource(
android.R.color.transparent);
This causes your dialog border and title bar to be transparent. It is useful if you are setting custom background with border(mycase: rounded corners) to display appropriately. No edges visible.
To customize completely your Dialog the best would be probably to extend it and create a custom Dialog.
This will give you all the freedom that you need, but of course you have to do more work.
public class MyDialog extends Dialog {
public MyDialog(Context context) {
super(context, android.R.style.Theme_Black_NoTitleBar);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setCanceledOnTouchOutside(false);
setContentView(R.layout.my_dialog_layout);
TextView title = (TextView) findViewById(R.id.my_dialog_title);
title.setText(context.getString(R.string.my_dialog_title_text));
findViewById(R.id.error_dialog_btn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do something
MyDialog.this.dismiss();
}
});
findViewById(R.id.success_dialog_btn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do something
MyDialog.this.dismiss();
}
});
}
}
You need to create a custom AlertDialog for this:
public class CustomAlertDialogBuilder extends AlertDialog.Builder {
private final Context mContext;
private TextView mTitle;
private ImageView mIcon;
private TextView mMessage;
public CustomAlertDialogBuilder(Context context) {
super(context);
mContext = context;
View customTitle = View.inflate(mContext, R.layout.dialog_title, null);
mTitle = (TextView) customTitle.findViewById(R.id.alertTitle);
mIcon = (ImageView) customTitle.findViewById(R.id.icon);
setCustomTitle(customTitle);
}
#Override
public CustomAlertDialogBuilder setTitle(int textResId) {
mTitle.setText(textResId);
return this;
}
#Override
public CustomAlertDialogBuilder setTitle(CharSequence text) {
mTitle.setText(text);
return this;
}
#Override
public CustomAlertDialogBuilder setIcon(int drawableResId) {
mIcon.setImageResource(drawableResId);
return this;
}
#Override
public CustomAlertDialogBuilder setIcon(Drawable icon) {
mIcon.setImageDrawable(icon);
return this;
}
}
You also need a custom layout:
res/layout/dialog_title.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/title_template"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_marginTop="6dip"
android:layout_marginBottom="9dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip">
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:paddingTop="6dip"
android:paddingRight="10dip"
android:src="#drawable/ic_dialog_alert" />
<TextView
android:id="#+id/alertTitle"
style="#style/?android:attr/textAppearanceLarge"
android:singleLine="true"
android:ellipsize="end"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<ImageView android:id="#+id/titleDivider"
android:layout_width="fill_parent"
android:layout_height="1dip"
android:scaleType="fitXY"
android:gravity="fill_horizontal"
android:src="#drawable/divider_horizontal_bright" />
</LinearLayout>
Hope this helps.
You can find more info here:
How to change theme for AlertDialog
FYI your StackOverflow account was created less than 24 hours ago, so it looks like you made a new account rather than accessing your original one.
Try this, This will definitly solve youre problem
AlertDialog.Builder builder = new AlertDialog.Builder(MeetingPointFix.this);
builder.setCancelable(false);
LayoutInflater reportAlertDialogLayoutInflater = LayoutInflater.from(getApplicationContext());
View reportAlertDialogInflatedView = reportAlertDialogLayoutInflater.inflate(R.layout.meetingpointfix_report_dialog, null);
builder.setView(reportAlertDialogInflatedView);
int [] viewsRefsIds = {R.id.reportLocNameValue, R.id.reportLocLatValue, R.id.reportLocLngValue, R.id.reportTimeValue,
R.id.reportDateValue, R.id.reportImgTitleValue, R.id.reportImgPathValue
};
builder.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,R.drawable.reporticon01);
TextView reportDialogMSG = (TextView) reportDialog.findViewById(R.id.reportDialogMessageValue);
Button reportOkBtn = (Button) reportDialog.findViewById(R.id.reportOkBtnID);
Button reportNavigateBtn = (Button) reportDialog.findViewById(R.id.reportNavigateBtnID);
reportDialog=builder.create();

Dialog is shrinking even with all parent layout_width set as fill_parent

Another weird behavior encountered here. This time my dialog is shrinking width wise inspite of setting all parent layout's width as fill_parent. Here is the image...
I have tried making it an activity with theme set as Dialog in manifest. Still it is behaving same. However it becomes normal as soon as I set layout_width for first TextView "User Agreement" as fill_parent. But I don't understand this behavior as it should not depend on TextView's width for its own width. Please tell me if there is any other efficient way to deal with these type of situations. My code for layout is below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#layout/gradientback"
android:orientation="vertical" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="User Agreement"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#B80303" />
<View
android:layout_width="fill_parent"
android:layout_height="3dip"
android:background="?android:attr/listDivider" >
</View>
<LinearLayout
android:id="#+id/linearLayout4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<CheckBox
android:id="#+id/checkBoxForTermsId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#7B4302"
android:text="I Agree The Terms & Conditions" >
</CheckBox>
This is not whole code for layout because I don't think it is needed...
The code for showing Dialog is as follows:
private void showAgreementBox() {
// TODO Auto-generated method stub
final Dialog dialog = new Dialog(Launcher.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.useragreement);
dialog.setTitle("User Agreement");
dialog.setCancelable(false);
final TextView userAg = (TextView) dialog.findViewById(R.id.textViewOfUserAg);
final CheckBox checkUserAg = (CheckBox) dialog.findViewById(R.id.checkBoxForTermsId);
final Button continueB = (Button) dialog.findViewById(R.id.continueB);
checkUserAg.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (checkUserAg.isChecked() == true) {
continueB.setEnabled(true);
} else {
continueB.setEnabled(false);
}
}
});
continueB.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
//checkForTrialPeriod(isUserRegisterd);
}
});
Button cancelB = (Button) dialog.findViewById(R.id.cancelB);
cancelB.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
dialog.show();
}
Fix the width of your parent layout
android:layout_width="400dp"
fill_parent works for Activity xml but for dialogs you have to set the width.
If you are looking for a general solution set an image as background in the parent layout just like we place images in different drawable folders to support multiple screens.
You can also set width as full screen using
dialog.getWindow().getAttributes().width = LayoutParams.FILL_PARENT;
Why this behavior is shown by dialog (quoting from this post by Dianne Hackborn)
The dialog theme, as part of its nature, sets the top-level window
layout to be WRAP_CONTENT. You could try manually setting the Window
layout width and height to FILL_PARENT [...]
Also took reference from this answer.
setContentView(R.layout.layout_name);
getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
this worked for me .

Set height and width in an Dialog Box?

I am using a XML-layout which I am prompting as the dialog box.
Designing of XML-layout is well formatted with enough required height and width..
But when I open it as the dialog box its width is getting disturbed so how to set height and width of dialog box through coding.
I even had referred this previous STACK OVERFLOW QUESTION
Here is the code:
// Layout Inflater Code..
editDialog = new Dialog(this);
layoutEdit = LayoutInflater.from(this).inflate(R.layout.createlayout, null);
//layoutEdit.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
editDialog.setContentView(layoutEdit);
// Called the Dialogbox to inflate
updateButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
editDialog.show();
}
});
// XML File Code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/bd"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="false"
android:text="Enter Name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/whtie"
android:typeface="monospace" />
<EditText
android:id="#+id/txtname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" >
</EditText>
</LinearLayout>
</ScrollView>
Try this...
1.Dialog snippet:
private void CustomDialog(String msg) {
final Dialog dialog = new Dialog(YourActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
LinearLayout.LayoutParams dialogParams = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, 300);//set height(300) and width(match_parent) here, ie (width,height)
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dislogView = inflater
.inflate(R.layout.my_custom_popup, null);
dialog.setContentView(dislogView, dialogParams);
TextView popupMsg = (TextView) dialog.findViewById(R.id.popupMsg);
Button popupOk = (Button) dialog.findViewById(R.id.popupOk);
popupMsg.setText(msg);
popupOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
2.Then call CustomDialog(Str) where you want to prompt in your activity.
CustomDialog("This is customized popup dialog!");
You better use an activity that looks like a dialog (I feel it will be better in your case). Here is an example code:
public class DialogActivity extends Activity {
/**
* Initialization of the Activity after it is first created. Must at least
* call {#link android.app.Activity#setContentView setContentView()} to
* describe what is to be displayed in the screen.
*/
#Override
protected void onCreate(Bundle savedInstanceState) {
// Be sure to call the super class.
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_LEFT_ICON);
// See assets/res/any/layout/dialog_activity.xml for this
// view layout definition, which is being set here as
// the content of our screen.
setContentView(R.layout.dialog_activity);
getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
android.R.drawable.ic_dialog_alert);
}
}
This code is from api demos
View layout = inflater.inflate(R.layout.view, NULL);
layout.setMinimumWidth(200);
layout.setMinimumHeight(200);
dialog.setContentView(layout);
Try
dialog.getWindow().setLayout(height, width);

How to configure custom button in dialog?

here I have a custom dialog with background 2 ImageButton inside it.
the problem is, when I try to set onclick listener to that buttons, the program will return NullPointerException. I don't know why is this happen. how to assign operation to button inside dialog anyway??
pause menu xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:id="#+id/linearLayout1" android:layout_width="wrap_content" android:background="#drawable/pause_menu_cropped" android:layout_gravity="center" android:gravity="center|center_horizontal">
<TableLayout android:layout_width="wrap_content" android:id="#+id/tableLayout1" android:layout_height="wrap_content">
<ImageButton android:src="#drawable/pause_button_option" android:layout_width="wrap_content" android:background="#drawable/pause_button_option" android:layout_height="wrap_content" android:id="#+id/btn_pause_option"></ImageButton>
<ImageButton android:src="#drawable/pause_button_quit" android:layout_width="wrap_content" android:background="#drawable/pause_button_quit" android:layout_height="wrap_content" android:id="#+id/btn_pause_quit"></ImageButton>
</TableLayout>
</LinearLayout>
dialog code
Dialog pauseMenu = new Dialog(this, R.style.NewDialog);
pauseMenu.setContentView(R.layout.pause_menu);
ImageButton quit = (ImageButton)findViewById(R.id.btn_pause_quit);
quit.setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View v) {
TestActivity.this.finish();
}
});
return pauseMenu;
the code is returning error in line
quit.setOnClickListener();
ImageButton quit =
(ImageButton)findViewById(R.id.btn_pause_quit);
should be
ImageButton quit = (ImageButton)pauseMenu.findViewById(R.id.btn_pause_quit);
This happens because findViewById is invoked for the activity, and it doesn't have btn_pause_quit button in it's layout. But your dialog has.
U can use this custom dialog and onclicklistener..
public class CustomizeDialog extends Dialog implements OnClickListener {
Button okButton;
public CustomizeDialog(Context context) {
super(context);
/** 'Window.FEATURE_NO_TITLE' - Used to hide the title */
requestWindowFeature(Window.FEATURE_NO_TITLE);
/** Design the dialog in main.xml file */
setContentView(R.layout.main);
okButton = (Button) findViewById(R.id.OkButton);
okButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
/** When OK Button is clicked, dismiss the dialog */
if (v == okButton)
dismiss();
}
}
I think your onClickListener should be DialogInterface.OnClickListener

Categories

Resources