Search code examples
androidandroid-dialogandroid-dialogfragment

Place a button inside DialogFragment's title


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.


Solution

  • 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.