AndroidのAlertDialogのフォント

AndroidのAlertDialogのフォントを手軽に変更するソースです。海外の端末で中華フォントしかない場合、AlertDialogのフォントの変更方法がなかなか見つからないですよね。それにAlertDialogのリストを使ったときも大変。
だから書いてみました。というか必要に迫られて便利だったので。
一応、AlertDialog内のButtonとTextView、EditTextとListViewに対応しています。
必要ならRadioButtonなども追加してみたらどうでしょうか。

あ、もちろん、いろんな人のソースを参考にしています。

<br />
//IPAゴシックをディレクトリにコピーしてから使うとき<br />
Typeface type_face = Typeface.createFromFile(getFilesDir() + &quot;/ipaexg.ttf&quot;);<br />
copyRawFile(this, R.raw.ipaexg, &quot;ipaexg.ttf&quot;);</p>
<p>AlertDialog dialog = new AlertDialog.Builder(this)<br />
	.setIcon(R.drawable.icon)<br />
	.setTitle(R.string.title)<br />
	.setMessage(R.string.msg)<br />
	.setPositiveButton(R.string.yes,new DialogInterface.OnClickListener() {<br />
		@Override<br />
		public void onClick(DialogInterface dialog, int whichButton) {<br />
			dialog.dismiss();<br />
			finish();<br />
		}<br />
	})<br />
	.setNegativeButton(R.string.no,new DialogInterface.OnClickListener() {<br />
		@Override<br />
		public void onClick(DialogInterface dialog, int whichButton) {<br />
			dialog.dismiss();<br />
		}<br />
	})<br />
	.create();<br />
	dialog = set_dialog_font(this,dialog,type_face);<br />
	dialog.show();</p>
<p>//Rawからフォルダにコピー<br />
public void copyRawFile(Context context, int resourceId, String fileName) {<br />
	   InputStream input = null;<br />
	   OutputStream output = null;<br />
	   try {<br />
		   input = context.getResources().openRawResource(resourceId);<br />
		   output = context.openFileOutput(fileName, Context.MODE_PRIVATE);<br />
		   byte[] buff = new byte[1024*4];<br />
		   int size = 0;<br />
		   while((size = input.read(buff)) &gt;= 0) {<br />
			   output.write(buff, 0, size);<br />
		   }<br />
		   output.flush();<br />
		   output.close();<br />
		   input.close();<br />
		   output = null;<br />
		   input = null;<br />
	   }catch(Exception e) {<br />
		   try {<br />
			   if(output != null) output.close();<br />
			   if(input != null) input.close();<br />
		   }catch(Exception e2){<br />
		   }<br />
	   }<br />
   }</p>
<p>//子要素のフォントを変更<br />
private void overrideFonts(final Context context, final View v ,Typeface type) {<br />
		try {<br />
			if (v instanceof ViewGroup) {<br />
			ViewGroup vg = (ViewGroup) v;<br />
			for (int i = 0; i &lt; vg.getChildCount(); i++) {<br />
			View child = vg.getChildAt(i);<br />
			A_PointSystem.overrideFonts(context, child,type);<br />
			}<br />
			} else if (v instanceof EditText ||<br />
				   v instanceof TextView ||<br />
				   v instanceof Button) {<br />
				((TextView) v).setTypeface(type);<br />
			}<br />
		} catch (Exception e) {<br />
			e.printStackTrace();<br />
		}<br />
	}</p>
<p>//AlertDialogの要素を変更<br />
private AlertDialog set_dialog_font(final Context context,AlertDialog alertDialog ,Typeface type){<br />
    	alertDialog.setOnShowListener(new OnShowListener() {<br />
            @Override<br />
            public void onShow(DialogInterface alert) {<br />
            	Button bt0 =  ((AlertDialog)alert).getButton(Dialog.BUTTON1);<br />
                Button bt1 =  ((AlertDialog)alert).getButton(Dialog.BUTTON2);<br />
                Button bt2 =  ((AlertDialog)alert).getButton(Dialog.BUTTON3);<br />
                if(bt0!=null){bt0.setTypeface(TitleActivity.g_type_face);}<br />
                if(bt1!=null){bt1.setTypeface(TitleActivity.g_type_face);}<br />
                if(bt2!=null){bt2.setTypeface(TitleActivity.g_type_face);}</p>
<p>                Window w = ((AlertDialog)alert).getWindow();</p>
<p>                View v = w.getDecorView();<br />
                if(v!=null){<br />
                	overrideFonts(context,v,TitleActivity.g_type_face);<br />
                }<br />
                ListView listView = ((AlertDialog)alert).getListView();<br />
                if(listView==null) return;<br />
                final ListAdapter originalAdapter = listView.getAdapter();<br />
                listView.setAdapter(new ListAdapter()<br />
                {<br />
                    @Override<br />
                    public int getCount() {<br />
                        return originalAdapter.getCount();<br />
                    }<br />
                    @Override<br />
                    public Object getItem(int id) {<br />
                        return originalAdapter.getItem(id);<br />
                    }<br />
                    @Override<br />
                    public long getItemId(int id) {<br />
                        return originalAdapter.getItemId(id);<br />
                    }<br />
                    @Override<br />
                    public int getItemViewType(int id) {<br />
                        return originalAdapter.getItemViewType(id);<br />
                    }<br />
                    @Override<br />
                    public View getView(int position, View convertView, ViewGroup parent) {<br />
                        View view = originalAdapter.getView(position, convertView, parent);<br />
                        TextView textView = (TextView)view;<br />
                        textView.setTypeface(type_face);<br />
                        return view;<br />
                    }<br />
                    @Override<br />
                    public int getViewTypeCount() {<br />
                        return originalAdapter.getViewTypeCount();<br />
                    }<br />
                    @Override<br />
                    public boolean hasStableIds() {<br />
                        return originalAdapter.hasStableIds();<br />
                    }<br />
                    @Override<br />
                    public boolean isEmpty() {<br />
                        return originalAdapter.isEmpty();<br />
                    }<br />
                    @Override<br />
                    public void registerDataSetObserver(DataSetObserver observer) {<br />
                        originalAdapter.registerDataSetObserver(observer);</p>
<p>                    }<br />
                    @Override<br />
                    public void unregisterDataSetObserver(DataSetObserver observer) {<br />
                        originalAdapter.unregisterDataSetObserver(observer);</p>
<p>                    }<br />
                    @Override<br />
                    public boolean areAllItemsEnabled() {<br />
                        return originalAdapter.areAllItemsEnabled();<br />
                    }<br />
                    @Override<br />
                    public boolean isEnabled(int position) {<br />
                        return originalAdapter.isEnabled(position);<br />
                    }</p>
<p>                });<br />
            }<br />
        });<br />
    	return alertDialog;<br />
}<br />