Editset custom layout title for my dialog - android

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

Related

How center custom dialog title on Android

I'm writing a custom dialog on android.
I did this using the onCreateView method.
public class CheckpointLongPressDialog extends DialogFragment {
public void CheckpointLongPressDialog() {}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_checkpoint_long_press_dialog, container);
getDialog().setTitle("TITLE");
return view;
}
How can i center the title programmatically?
Maybe its not the best way, I use a custom title TextView.
TextView title = new TextView(mainActivity);
title.setText(alertTitle);
title.setBackgroundResource(R.drawable.gradient);
title.setPadding(10, 10, 10, 10);
title.setGravity(Gravity.CENTER); // this is required to bring it to center.
title.setTextSize(22);
getDialog().setCustomTitle(title);
I solve the problem using a builder and inflating the xml layout.
private AlertDialog.Builder builder;
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.fragment_checkpoint_long_press_dialog, null));
// Create the AlertDialog object and return it
return builder.create();
}
Try this..
final int titleId = getActivity().getResources().getIdentifier("alertTitle", "id", "android");
TextView title = (TextView) getDialog().findViewById(titleId);
if (title != null) {
title.setGravity(Gravity.CENTER);
}
What if you use the whole layout to inflate also your custom title?. Instead of getDialog().setTitle("TITLE"); you can also include a TextView in your custom layout for the title.
The title view is using default theme. You have 2 ways to do what you want, first one is better for having a more customized experience:
Use this to have a dialog without title, and then make custom title bar in the layout of this fragment.
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
Extend the default theme for the dialog and update it, then set it in this dialog.

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

Android AlertDialog Multi Choice Items with customised items

Does anyone know how to customize items in AlertDialog when multi choice items are set with setMultiChoiceItems(...). I would like to change the text size for the items.
Thanks
Sure, you can use Dialog.setContentView() to set the content of a dialog to be an arbitrary layout.
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.yourLayoutId);
dialog.show();
Make yourself a layout file with a components that you want in it and call setContentView on your dialog, passing the name of your layout file.
If you are deadset on AlertDialog you can do something similar with builder.setView()
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.yourLayoutId, (ViewGroup) findViewById(R.id.yourLayoutRoot));
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setView(layout);
AlertDialog alertDialog = builder.create();
alertDialog.show();
You can create a custom AlertDialog where you can change the text size of any element. A simple generic example: http://android-codes-examples.blogspot.in/2011/03/how-to-display-custom-dialog-and.html

Dialog extending from AlertBuilder?

Is there a way to use my own Dialog-extended class with an AlertBuilder style? Right now I have:
#Override
protected Dialog onCreateDialog(int id) {
LayoutInflater inflater = LayoutInflater.from(this);
switch (id) {
case MY_DIALOG:
View view = inflater.inflate(R.layout.my_dlg_layout, null);
AlertDialog dlg = new AlertDialog.Builder(this)
.setView(view)
.create();
return dlg;
}
return null;
}
But it's getting too crazy, and I'd like to move the dialog definition into its own class. (the above doesn't look too bad, I'm skipping a lot for the sake of clarity - basically I'm using a somewhat complicated custom view with AlertBuilder).
I very much want to use Dialog Fragment, but am not ready to do it yet, I need to build this as a temporary thing.
Thanks
Is there a way to use my own Dialog-extended class with an
AlertBuilder style
class MyClass extends AlertDialog.Builder{
. . .
}

How can I customize the header layout of a dialog

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);
}
}

Categories

Resources