Android custom dialog

This will create a custom dialog.

AlertDialog.Builder builder;
AlertDialog alertDialog;
 
Context mContext = this;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.help_dialog, (ViewGroup) findViewById(R.id.layout_root));
 
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText(getString(R.string.help_string));
 
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
 
alertDialog.show();

And create a “help_dialog.xml” this name is from the inflater

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_root"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    >
    <TextView android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:textColor="#FFF"
        />
</LinearLayout>

LayoutInflater at android dev

Android getString(R.string.text) in a static method

Sometimes there is a need to access recourses from static methods.
In order to do this we need to pass along the Context to the method and use getString from that Context.

Example:

package com.f15ijp.android.test;
 
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.WindowManager;
 
public class Help {
	public static AlertDialog helpDialog(Context theContext, String helpMessage){
	AlertDialog alertDialog = new AlertDialog.Builder(theContext).create();            	
    	alertDialog.setTitle("Help");
    	alertDialog.setMessage(helpMessage);
    	alertDialog.setButton(theContext.getString(R.string.close_help_btn), new DialogInterface.OnClickListener() {
	    @Override
	    public void onClick(DialogInterface dialog, int which) {
		return;						
	    }
	});
    	WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.copyFrom(alertDialog.getWindow().getAttributes());
        lp.width = WindowManager.LayoutParams.FILL_PARENT;
        lp.height = WindowManager.LayoutParams.FILL_PARENT;
        /*by calling alertDialog.show() now and then again after setAttributes the background
         * the background activity is replaced by a black box. 
         * If this first alertDialog.show() is removed then the (current) acitivity is used as a background. 
         */
        alertDialog.show();
        alertDialog.getWindow().setAttributes(lp);
        return alertDialog;
    }
}