How can I customize the header layout of a dialog - android

In Android, is it possible to customize the header layout (the icon + a text) layout of a dialog? Or can I just set a custom string value of the title text?
Thank you.

It's possible to change the header of the Dialog if you set a custom layout for both the dialog and the header. I've only ever used this method to remove the header entirely, but this ought to work for a custom header:
dialog = new Dialog(context);
Window window = dialog.getWindow();
window.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
dialog.setContentView(R.layout.my_dialog_layout);
window.setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.my_custom_header);
This is all a tad more complicated (as you have to setup the dialog's layout as well) but it's easier than subclassing Dialog.

the original Dialog class seems to lack the ability to set an icon, but you can easily extend AlertDialog and set a custom view (the same you would use for your Dialog instance), you just need something like this
class MyDialog extends AlertDialog {
public MyDialog(Context ctx) {
super(ctx);
LayoutInflater factory = LayoutInflater.from(context);
View view = factory.inflate(R.layout.dialog_layout, null);
setView(view);
setTitle("MyTitle");
setIcon(R.drawable.myicon);
}
}

Related

How to achive such a Popup in android

Can someone tell me how to implement this in an android app. If you can share code, some reference, or a tutorial. Anything will be helpful
You can inflate your custom layout to popup window.
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext(.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(popupView,400,400);
popupWindow.showAtLocation(attachment_button, Gravity.CENTER, 10, 200);
Take custom listview in your layout.
You can do this with Dialog that would show when you click at filter_box.
Just create class MyDialog extends Dialog and place your xml layout the place you need "popup" show.

Create AlertDialog using inside layout of a xml

I want to create a alert dialog using a layout which is in a xml. I tried this:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
View v = inflater.inflate(R.id.optionsmenu, null);
AlertDialog dialog = builder.setView(v).create();
dialog.show();
It does not work. optionsmenu is the layout that i want to use to create alert dialog. Can i set alert dialog view to this inner layout ?
here is the image's url . You can see the layout that i want to use.
I dont really know how you expect your dialog to look like but you should create it like this:
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.activity_main);
dialog.setTitle("");
//to use a view inside the xml (i.e. a button)
Button button = (Button)dialog.findViewById(R.id.optionsmenu);
dialog.show();
Note that you were trying to inflate not the layout but the view (R.id.optionsmenu) it should have been (R.layout.activity_main)
alertDialog.builder is used to create a dialog without using a xml layout

Editset custom layout title for my dialog

i want have my custom header for my CustomAlert.
what do i do for my custom layout for Dialog,s title?
Edit2:
i added my code:
protected Dialog onCreateDialog(int id)
{
switch(id)
{
case Dialog_Reset :
Dialog dialog=new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.about);
dialog.setCanceledOnTouchOutside(true);
return dialog;
}
return super.onCreateDialog(id);
}
and becuse i dont like default header in dialog,i remove it now,while i learn having my custom header.
but i dont understand what is "titleId". is it what really? is it my
special layout designed that i want use for title?
titleId : title identifier in other words a string resource identifier, e.g. R.string.app_name. You can add these in strings.xml found at res > values folders
Android Dev Doc: Dialog.setTitle (int titleId)
More about string resources here
You can set CustomTitle of your Alert Dialog using public AlertDialog.Builder setCustomTitle (View customTitleView) method.
As per documentation
public AlertDialog.Builder setCustomTitle (View customTitleView)
Set the title using the custom view customTitleView. The methods setTitle(int) and setIcon(int) should be sufficient for most titles, but this is provided if the title needs more customization. Using this will replace the title and icon set via the other methods.
Parameters
customTitleView The custom view to use as the title.
Returns
This Builder object to allow for chaining of calls to set methods
example
LayoutInflater inflater = (LayoutInflater)yourClass.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View yourView= inflater.inflate(R.layout.custom_dialog, null);
AlertDialog.Builder ab= new AlertDialog.Builder(this);
ab.setCustomTitle(yourView);
ab.setMessage(message);
...
ab.create();
ab.show();
for details see the documentation

Make an AlertDialog without using AlertDialog.Builder

I'm trying to create a custom AlertDialog that doesn't use the system style defaults when it uses the Theme.Holo.Light.Dialog theme. I want it to use that theme, but I want to to have the same style as a ListActivity I have using that same theme. Different classes have different styles for the same theme, so it appears I need to create a subclass of the DialogFragment. Another restriction is that I want this dialog to be general. That is, I want to be able to conditionally add buttons, message, title, icon, and items. Hence, it seems that I can't just inflate a DialogFragment from an xml file (or I may be able to if I can create all possible elements I'd want, and then hide the ones I don't want. Is it possible to programmatically build a DialogFragment without inflating it from a single xml file?
EDIT
It looks like this could help: Add controls to custom dialog programatically
I'm working on something using this answer: Dynamically add table row in table and display it in dialog box in android
Why doesn't the button appear when I use this code?
The xml elements I added in layout do appear.
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
return dialog;
}
#Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View contentView = inflater.inflate(R.layout.post_dialog, container);
RelativeLayout layout = (RelativeLayout) contentView.findViewById(R.id.post_dialog_layout);
Button testButton = new Button(getActivity());
testButton.setText("success");
testButton.setLayoutParams(new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
layout.addView(testButton);
return contentView;
}
Everything you need is here and here. Basically in order to build the content of your dialog you should override onCreateView(...), but if you want more control on the Dialog itself you can also override onCreateDialog(...).
Builder pattern is there to help and smooth things, but if you prefer to build stuff your own you can build Dialog instance as well as its content view full-programmatically, without even inflating XML and simply instantiating layout elements at runtime.
You can set arguments Bundle to created DialogFragment and then use them to configure your Dialog. Some of them may be optional and you can use it to detect which option dialog should contain(title, additional buttons, icon).
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import ru.daoffice.R;
public class AlertDialogFragment extends DialogFragment {
private static final String ARG_TITLE = "ArgTitle";
private static final String ARG_MESSAGE = "ArgMessage";
public static DialogFragment newInstance(String title, String message) {
Bundle argumnets = new Bundle();
argumnets.putString(ARG_TITLE, title);
argumnets.putString(ARG_MESSAGE, message);
DialogFragment dialogFragment = new AlertDialogFragment();
dialogFragment.setArguments(argumnets);
return dialogFragment;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setIcon(R.drawable.alert_dialog_icon)
.setTitle(getArguments().getString(ARG_TITLE))
.setMessage(getArguments().getString(ARG_MESSAGE))
.setPositiveButton(android.R.string.ok, null)
.create();
}
}

How can I make custom dialogs angeled edges

When I create my custom dialog it looks like this :
but I want it to look like this :
Create a class which extends Dialog and inflate it your layout
public class CustomDialog extends Dialog
{
public CustomDialog (Context context)
{
//use this Theme, or any other theme you like
super(context, android.R.style.Theme_Translucent_NoTitleBar);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.your_layout);
}
}
You can use custom dialog and inflate your designed xml into it.
final Dialog yourDialog=new Dialog(context);
thumbnail_click.setContentView(R.layout.yourlayout);
You can use your own cutom layout for dialog as in the follwing link
Custom dialog
Custom Dialog PoPup
You can always inflate your own custom dialog box layout.
LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.dialog_layout, (ViewGroup) findViewById(R.id.dialog_layout_root));
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialoglayout);
if you haven't got an answer yet, here is a good toturial: Toturial
You vill need to make your own XML shape-style file, and use the corner function ex:
<corners android:bottomLeftRadius="5dp" android:bottomRightRadius="5dp" />

Categories

Resources