How center custom dialog title on Android - 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.

Related

How to get the inflated view from AlertDialog when using setView(int layoutResId)?

I use this code to create a custom AlertDialog:
val dialog = AlertDialog.Builder(context)
.setView(R.layout.layout)
.create()
The problem is I cannot get the inflated view. dialog.findViewById(R.id.a_view_in_the_layout) returns null.
Alternatively, I can use .setView(View.inflate(context, R.layout.layout, null) but this sometimes makes the dialog fill the screen and take more space than setView(int layoutResId).
If I remember correctly, create sets up the Dialog, but its layout is not inflated until it needs to be shown. Try calling show first then, then finding the view you're looking for.
val dialog = AlertDialog.Builder(context)
.setView(R.layout.layout)
.create()
dialog.show() // Cause internal layout to inflate views
dialog.findViewById(...)
Just inflate the layout yourself (its Java code but I think you know what to do):
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE );
View view = inflater.inflate( R.layout.layout, null );
dialog.setView(view);
dialog.create().show();
Your inflated view is now view and you can use it to find other views in it like:
EditText editText = view.findViewById(R.id.myEdittext);
Instead of using alert dialog use simple Dialog its Easy and very simple
final Dialog dialog = new Dialog(context);
dialog.setContentView((R.layout.layout);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
TextView tvTitle = (TextView) dialog.findViewById(R.id.tvTitle);
tvTitle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
You don't have to need to inflate the View.
Try this;
View dialogView; //define this as a gobal field
dialogView = LayoutInflater.from(context).inflate(R.layout.your_view, null);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Title");
builder.setView(dialogView);
View yourView = dialogView.findViewById(R.id.a_view_in_the_layout);
TextView yourTextView = dialogView.findViewById(R.id.a_textView_in_the_layout);
Button yourButton = dialogView.findViewById(R.id.a_button_in_the_layout);

Set text in TextView in custom dialog

I want to set the text in a TextView contained in a custom dialog programmatically, so that I can use Html.fromHtml. In what function should I call setText? I've tried doing it in onCreateDialog, but this does not actually change the text.
public class InfoDialogFragment extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.infodialog, null));
TextView textView = (TextView) getActivity().findViewById(R.id.info);
textView.setText(Html.fromHtml("<h1>Text has been correctly set</h1>"));
...
Try this:
View content = inflater.inflate(R.layout.infodialog, null);
builder.setView(content);
TextView textView = (TextView) content.findViewById(R.id.info);

how to add custom views to alertdialog in android?

I am trying to add custom views(chekbox and two radio buttons) as shown in image below in alertdialog but not succeded.
Please suggest me a way to get views as shown in the image.
Thanks in advance!!
Use DialogFragment instead of AlertDialog.
public class CustomDialogFragment extends DialogFragment {
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = LayoutInflater.from(getActivity());
View viewRoot = inflater.inflate(R.layout.dialog_view, null);
//do something with your view
builder.setView(viewRoot);
return builder.create();
}
}
R.layout.dialog_view - it's your view, which you want to display
You can build dialog with custom layout. Here's some tutorial how to do that:
http://www.mkyong.com/android/android-custom-dialog-example/

Assign Dialog box view to another view

I am creating a dialog and setting setContentView of a layout. And I am programmatically adding buttons, images to layout in dialog setContentView . Now how I can assign dialog box view to another view.
That is a layout is assigned to a a view like below
View getview=R.layout.tamil_alphabet_speak_word;
Similarly how can I assign the dialog box view to another view. Since I am adding all elements to the view "TamilAlphabets" programmatically the child are null it returns for the below code.
Alphbetdialog=new Dialog(TamilAlphabets.this);
Alphbetdialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
Alphbetdialog.setContentView(R.layout.tamil_alphabetsdialog);
(adding elements to the layout "TamilAlphabets" code
..............
)
LayoutInflater inflator=(LayoutInflater)TamilAlphabets.this.getSystemService
(TamilAlphabets.this.LAYOUT_INFLATER_SERVICE);
View row=inflator.inflate(tamil_alphabetsdialog, Parent,false);
LinearLayout l1=(LinearLayout)row.findViewById(R.id.alphabetlayout1);
ViewGroup vg=(ViewGroup)l1;
vg.getChildCount();
So I need to assign the dialog box view to another view how do I do that.
I need something like this
View getview=<I need dialog box view>
You should avoid using Dialog class directly and instead use Dialog subclass's or DialogFragment
https://developer.android.com/guide/topics/ui/dialogs.html:
The Dialog class is the base class for dialogs, but you should avoid instantiating Dialog directly. Instead, use one of the following subclasses:
AlertDialog
A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout.
DatePickerDialog or TimePickerDialog
A dialog with a pre-defined UI that allows the user to select a date or time.
In any case im guessing that what you want is a custom dialog and what is recomendaded is using the DialogFragment class in that case here is an example
Dialog fragment layout
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent">
Dialog class
public class DialogExampleFragment extends DialogFragment {
private static final String ARG_PARAM = "extra:PARAM";
private String mText;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle arguments = getArguments();
mText = arguments.getString(ARG_PARAM);
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.setTitle("title");
return dialog;
}
public static DialogExampleFragment newInstance(String message) {
Bundle args = new Bundle();
args.putSerializable(ARG_PARAM, message);
DialogExampleFragment fragment = new DialogExampleFragment();
fragment.setArguments(args);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_dialog_example, container, false);
TextView t = (TextView) root.findViewById(R.id.textView1);
t.setText(mText);
return root;
}
}
To show as a dialog
DialogExampleFragment.newInstance("Message")
.show(getFragmentManager(), "dialog");
Note since a DialogFragment is a fragment it has de advantage of being able to be shown as a Dialog or as a regular fragment you can get all the information on the link i posted above

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

Categories

Resources