1

I show a DialogFragment from another DialogFragment. I need to set title and a button just next to it. I don't want to reimplement the theme and create a custom view inside DialogFragment's content view instead of dialog title (because it's error-prone and time wasting). Is it even possible? I tried many API functions, AlertBuilder, ActionBar, this and that, still didn't found anything that fits my needs.

efpies
  • 3,625
  • 6
  • 34
  • 45

2 Answers2

1

Try something like this:

// Title
final int titleId = getResources().getIdentifier("alertTitle","id","android");
TextView title = (TextView) popup.findViewById(titleId);
title.setText("My new title");

// Title's parent layout
ViewGroup viewGroup = (ViewGroup) title.getRootView();

// Button
Button button = new Button(this);
button.setText("A Button");
viewGroup.addView(button);

Note: You may need to adjust the button and title objects' LayoutParams.

Simas
  • 43,548
  • 10
  • 88
  • 116
  • That worked. Small fix: I used `TextView title = (TextView) getDialog().findViewById(android.R.id.title)` – efpies Aug 27 '14 at 15:48
  • 1
    @efpies you may want to be careful using `android.R.id.title` as well as `alertTitle`. I remember trying both and only the latter worked. Perhaps it's device dependent. So you may want to add a null check :) – Simas Aug 27 '14 at 15:52
1

An update. This can be achieved by calling setCustomTitle on AlertDialog.Builder class. Available since api level 1, and do not require a bunch of code.

Example:

AlertDialog.Builder(getActivity()).setCustomTitle(getActivity().getLayoutInflater().inflate(R.layout.my_custom_view, viewGroup)).create()

Ze Luis
  • 236
  • 2
  • 15