Java – Custom button in the title bar of the dialog box

Custom button in the title bar of the dialog box… here is a solution to the problem.

Custom button in the title bar of the dialog box

I’m trying to make a dialog similar to the one in Nova Launcher (upper right corner of the dialog):

enter image description here

Displays the settings for the application. I can’t get it to display, my current dialog only shows a normal title bar:
Current code:

Custom dialog files: (add_dialog_custom_title)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:paddingLeft="15.0dip"
    android:paddingRight="15.0dip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <TextView
        android:textAppearance="?android:textAppearanceLarge"
        android:gravity="center_vertical"
        android:layout_width="0.0dip"
        android:layout_height="wrap_content"
        android:minHeight="?android:listPreferredItemHeight"
        android:text="@string/menu_item_add_item"
        android:drawablePadding="14.0dip"
        android:layout_weight="1.0" />
    <ImageView
        android:layout_gravity="center"
        android:id="@id/settings_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/ic_menu_preferences"
        android:layout_weight="0.0" />
</LinearLayout>

NfcUnlockActivity:

package com.quinny898.gcse.doorcontrol;
import android.app.*;
import android.widget.*;
import android.content.*;
import android.text.*;
import android.os.*;
import android.view.*;

public class NfcUnlockActivity extends Activity 
{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AlertDialog.Builder builder;
        AlertDialog alertDialog;
    Context mContext = getApplicationContext();
    Dialog dialog = new Dialog (mContext);
    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.add_dialog_custom_title,
    (ViewGroup) findViewById(R.id.layout_root));


    dialog.setContentView(R.layout.add_dialog_custom_title);
    dialog.setTitle("Door Control");
    builder = new AlertDialog.Builder(mContext);
    builder.setView(layout);
    alertDialog = builder.create();
    }
}

All the IDs and strings are there, I checked
I feel like I’m missing something really stupid here, how can that help?

Edit:
Fixed, I did as you said and added
alertDialog.show();
Then I’m here using the second answer to fix the resulting crash: Android: ProgressDialog.show() crashes with getApplicationContext
Works perfectly, thanks for your help

Best Solution

One mistake is when you write an ImageView

android:id="@id/settings_button"

In lieu of

android:id="@+id/settings_button"

Related Problems and Solutions