2011. 6. 23. 21:39
안드로이드
출처 : http://blog.androgames.net/10/custom-android-dialog/
그냥 여기있는 소스 그대로다
리소스는 출처에도없어서 그냥 돌아다니는 아무 리소스 해서
header.png
footer.png
key.png
title.png로 이름 바꿔서 res\drawable에다가 너어뒀다
진짜 다이얼로그에서 타이틀 색깔 바꿀라고 엄청 노력했는데 이거 한방으로 끝났다 참으로 감사하다
멜론의 겨자색 다이어로그박스가 탐났었는데 드디어 원풀어다
Main.java
package np.comp.AlertDialogTest;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
/**
* Intent intent = new Intent(Index.this, AlertDialogs.class);<br>
* intent.putExtra(AlertDialogs.setMode, AlertDialogs.PROGRESS);<br>
* 얼랏식으로 액티비티를 띄움.<br>
*
*/
// 버턴을 상수값으로 지정했다.
public class Main extends Activity
{
public static final String Mode = "MODE";
public int setMode = 0;
public static final String mode_InputText = "1";
public static final String mode_Login = "2";
private EditText edit = null;
private Button button1 = null;
private Button button2 = null;
final static String PIN = "0000";
final static int CUSTOM_DIALOG = 0;
final static int DEFAULT_DIALOG=1;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
showDialog(CUSTOM_DIALOG);
}
@Override
public Dialog onCreateDialog(int dialogId) {
Dialog dialog = null;
switch (dialogId) {
case CUSTOM_DIALOG :
CustomDialog.Builder customBuilder = new
CustomDialog.Builder(Main.this);
customBuilder.setTitle("Custom title")
.setMessage("Custom body")
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Main.this
.dismissDialog(CUSTOM_DIALOG);
}
})
.setPositiveButton("Confirm",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog = customBuilder.create();
break;
case DEFAULT_DIALOG :
AlertDialog.Builder alertBuilder = new
AlertDialog.Builder(Main.this);
alertBuilder.setTitle("Default title")
.setMessage("Default body")
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setPositiveButton("Confirm",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Main.this
.dismissDialog(DEFAULT_DIALOG);
}
});
dialog = alertBuilder.create();
break;
}
return dialog;
}
}
///이부분은 그냥 다이얼로그를 상속 받아서 재정의했는데 굳이 이럴 필요없이 CustomDialog create() 이부분을 적당히 긁어서 Main의 Dialog onCreateDialog부분에다가 적용시킨다면 소스 코드가 짧게 될 수있겠다
CustomDialog.java
package np.comp.AlertDialogTest;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class CustomDialog extends Dialog
{
public CustomDialog(Context context)
{
super(context);
// TODO Auto-generated constructor stub
}
public CustomDialog(Context context,int theme)
{
super(context,theme);
// TODO Auto-generated constructor stub
}
public static class Builder {
private Context context;
private String title;
private String message;
private String positiveButtonText;
private String negativeButtonText;
private View contentView;
private DialogInterface.OnClickListener
positiveButtonClickListener,
negativeButtonClickListener;
public Builder(Context context) {
this.context = context;
}
/**
* Set the Dialog message from String
* @param title
* @return
*/
public Builder setMessage(String message) {
this.message = message;
return this;
}
/**
* Set the Dialog message from resource
* @param title
* @return
*/
public Builder setMessage(int message) {
this.message = (String) context.getText(message);
return this;
}
/**
* Set the Dialog title from resource
* @param title
* @return
*/
public Builder setTitle(int title) {
this.title = (String) context.getText(title);
return this;
}
/**
* Set the Dialog title from String
* @param title
* @return
*/
public Builder setTitle(String title) {
this.title = title;
return this;
}
/**
* Set a custom content view for the Dialog.
* If a message is set, the contentView is not
* added to the Dialog...
* @param v
* @return
*/
public Builder setContentView(View v) {
this.contentView = v;
return this;
}
/**
* Set the positive button resource and it's listener
* @param positiveButtonText
* @param listener
* @return
*/
public Builder setPositiveButton(int positiveButtonText,
DialogInterface.OnClickListener listener) {
this.positiveButtonText = (String) context
.getText(positiveButtonText);
this.positiveButtonClickListener = listener;
return this;
}
/**
* Set the positive button text and it's listener
* @param positiveButtonText
* @param listener
* @return
*/
public Builder setPositiveButton(String positiveButtonText,
DialogInterface.OnClickListener listener) {
this.positiveButtonText = positiveButtonText;
this.positiveButtonClickListener = listener;
return this;
}
/**
* Set the negative button resource and it's listener
* @param negativeButtonText
* @param listener
* @return
*/
public Builder setNegativeButton(int negativeButtonText,
DialogInterface.OnClickListener listener) {
this.negativeButtonText = (String) context
.getText(negativeButtonText);
this.negativeButtonClickListener = listener;
return this;
}
/**
* Set the negative button text and it's listener
* @param negativeButtonText
* @param listener
* @return
*/
public Builder setNegativeButton(String negativeButtonText,
DialogInterface.OnClickListener listener) {
this.negativeButtonText = negativeButtonText;
this.negativeButtonClickListener = listener;
return this;
}
/**
* Create the custom dialog
*/
public CustomDialog create() {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// instantiate the dialog with the custom Theme
final CustomDialog dialog = new CustomDialog(context,
R.style.Dialog);
View layout = inflater.inflate(R.layout.dialog, null);
dialog.addContentView(layout, new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
// set the dialog title
((TextView) layout.findViewById(R.id.title)).setText(title);
// set the confirm button
if (positiveButtonText != null) {
((Button) layout.findViewById(R.id.positiveButton))
.setText(positiveButtonText);
if (positiveButtonClickListener != null) {
((Button) layout.findViewById(R.id.positiveButton))
.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
positiveButtonClickListener.onClick(
dialog,
DialogInterface.BUTTON_POSITIVE);
}
});
}
} else {
// if no confirm button just set the visibility to GONE
layout.findViewById(R.id.positiveButton).setVisibility(
View.GONE);
}
// set the cancel button
if (negativeButtonText != null) {
((Button) layout.findViewById(R.id.negativeButton))
.setText(negativeButtonText);
if (negativeButtonClickListener != null) {
((Button) layout.findViewById(R.id.negativeButton))
.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
positiveButtonClickListener.onClick(
dialog,
DialogInterface.BUTTON_NEGATIVE);
}
});
}
} else {
// if no confirm button just set the visibility to GONE
layout.findViewById(R.id.negativeButton).setVisibility(
View.GONE);
}
// set the content message
if (message != null) {
((TextView) layout.findViewById(
R.id.message)).setText(message);
} else if (contentView != null) {
// if no message set
// add the contentView to the dialog body
((LinearLayout) layout.findViewById(R.id.content))
.removeAllViews();
((LinearLayout) layout.findViewById(R.id.content))
.addView(contentView,
new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
}
dialog.setContentView(layout);
return dialog;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- 여기에서 가장 핵심은 이부분이다 보통의 다이어로그를 레이아웃으로만 커스텀 형식으로 만들다보면 다이얼로그의 테두리때문에 보기 싫어서 포기 할때가있다 하지만 이부분을 추가해서 소스 코드에 적용한다면 완전 내가 원하는 이미지로 만들어진 다이얼로그 박스를 볼수있다 -->
<style name="Dialog" parent="android:style/Theme.Dialog">
<item name="android:windowBackground">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
</style>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="DialogText">
<item name="android:textColor">#FF000000</item>
<item name="android:textSize">12sp</item>
</style>
<style name="DialogText.Title">
<item name="android:textSize">16sp</item>
<item name="android:textStyle">bold</item>
</style>
</resources>
MANIFEST.xml파일
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="np.comp.AlertDialogTest"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Main" android:configChanges="orientation"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
그냥 여기있는 소스 그대로다
리소스는 출처에도없어서 그냥 돌아다니는 아무 리소스 해서
header.png
footer.png
key.png
title.png로 이름 바꿔서 res\drawable에다가 너어뒀다
진짜 다이얼로그에서 타이틀 색깔 바꿀라고 엄청 노력했는데 이거 한방으로 끝났다 참으로 감사하다
멜론의 겨자색 다이어로그박스가 탐났었는데 드디어 원풀어다
Main.java
package np.comp.AlertDialogTest;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
/**
* Intent intent = new Intent(Index.this, AlertDialogs.class);<br>
* intent.putExtra(AlertDialogs.setMode, AlertDialogs.PROGRESS);<br>
* 얼랏식으로 액티비티를 띄움.<br>
*
*/
// 버턴을 상수값으로 지정했다.
public class Main extends Activity
{
public static final String Mode = "MODE";
public int setMode = 0;
public static final String mode_InputText = "1";
public static final String mode_Login = "2";
private EditText edit = null;
private Button button1 = null;
private Button button2 = null;
final static String PIN = "0000";
final static int CUSTOM_DIALOG = 0;
final static int DEFAULT_DIALOG=1;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
showDialog(CUSTOM_DIALOG);
}
@Override
public Dialog onCreateDialog(int dialogId) {
Dialog dialog = null;
switch (dialogId) {
case CUSTOM_DIALOG :
CustomDialog.Builder customBuilder = new
CustomDialog.Builder(Main.this);
customBuilder.setTitle("Custom title")
.setMessage("Custom body")
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Main.this
.dismissDialog(CUSTOM_DIALOG);
}
})
.setPositiveButton("Confirm",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog = customBuilder.create();
break;
case DEFAULT_DIALOG :
AlertDialog.Builder alertBuilder = new
AlertDialog.Builder(Main.this);
alertBuilder.setTitle("Default title")
.setMessage("Default body")
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setPositiveButton("Confirm",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Main.this
.dismissDialog(DEFAULT_DIALOG);
}
});
dialog = alertBuilder.create();
break;
}
return dialog;
}
}
///이부분은 그냥 다이얼로그를 상속 받아서 재정의했는데 굳이 이럴 필요없이 CustomDialog create() 이부분을 적당히 긁어서 Main의 Dialog onCreateDialog부분에다가 적용시킨다면 소스 코드가 짧게 될 수있겠다
CustomDialog.java
package np.comp.AlertDialogTest;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class CustomDialog extends Dialog
{
public CustomDialog(Context context)
{
super(context);
// TODO Auto-generated constructor stub
}
public CustomDialog(Context context,int theme)
{
super(context,theme);
// TODO Auto-generated constructor stub
}
public static class Builder {
private Context context;
private String title;
private String message;
private String positiveButtonText;
private String negativeButtonText;
private View contentView;
private DialogInterface.OnClickListener
positiveButtonClickListener,
negativeButtonClickListener;
public Builder(Context context) {
this.context = context;
}
/**
* Set the Dialog message from String
* @param title
* @return
*/
public Builder setMessage(String message) {
this.message = message;
return this;
}
/**
* Set the Dialog message from resource
* @param title
* @return
*/
public Builder setMessage(int message) {
this.message = (String) context.getText(message);
return this;
}
/**
* Set the Dialog title from resource
* @param title
* @return
*/
public Builder setTitle(int title) {
this.title = (String) context.getText(title);
return this;
}
/**
* Set the Dialog title from String
* @param title
* @return
*/
public Builder setTitle(String title) {
this.title = title;
return this;
}
/**
* Set a custom content view for the Dialog.
* If a message is set, the contentView is not
* added to the Dialog...
* @param v
* @return
*/
public Builder setContentView(View v) {
this.contentView = v;
return this;
}
/**
* Set the positive button resource and it's listener
* @param positiveButtonText
* @param listener
* @return
*/
public Builder setPositiveButton(int positiveButtonText,
DialogInterface.OnClickListener listener) {
this.positiveButtonText = (String) context
.getText(positiveButtonText);
this.positiveButtonClickListener = listener;
return this;
}
/**
* Set the positive button text and it's listener
* @param positiveButtonText
* @param listener
* @return
*/
public Builder setPositiveButton(String positiveButtonText,
DialogInterface.OnClickListener listener) {
this.positiveButtonText = positiveButtonText;
this.positiveButtonClickListener = listener;
return this;
}
/**
* Set the negative button resource and it's listener
* @param negativeButtonText
* @param listener
* @return
*/
public Builder setNegativeButton(int negativeButtonText,
DialogInterface.OnClickListener listener) {
this.negativeButtonText = (String) context
.getText(negativeButtonText);
this.negativeButtonClickListener = listener;
return this;
}
/**
* Set the negative button text and it's listener
* @param negativeButtonText
* @param listener
* @return
*/
public Builder setNegativeButton(String negativeButtonText,
DialogInterface.OnClickListener listener) {
this.negativeButtonText = negativeButtonText;
this.negativeButtonClickListener = listener;
return this;
}
/**
* Create the custom dialog
*/
public CustomDialog create() {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// instantiate the dialog with the custom Theme
final CustomDialog dialog = new CustomDialog(context,
R.style.Dialog);
View layout = inflater.inflate(R.layout.dialog, null);
dialog.addContentView(layout, new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
// set the dialog title
((TextView) layout.findViewById(R.id.title)).setText(title);
// set the confirm button
if (positiveButtonText != null) {
((Button) layout.findViewById(R.id.positiveButton))
.setText(positiveButtonText);
if (positiveButtonClickListener != null) {
((Button) layout.findViewById(R.id.positiveButton))
.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
positiveButtonClickListener.onClick(
dialog,
DialogInterface.BUTTON_POSITIVE);
}
});
}
} else {
// if no confirm button just set the visibility to GONE
layout.findViewById(R.id.positiveButton).setVisibility(
View.GONE);
}
// set the cancel button
if (negativeButtonText != null) {
((Button) layout.findViewById(R.id.negativeButton))
.setText(negativeButtonText);
if (negativeButtonClickListener != null) {
((Button) layout.findViewById(R.id.negativeButton))
.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
positiveButtonClickListener.onClick(
dialog,
DialogInterface.BUTTON_NEGATIVE);
}
});
}
} else {
// if no confirm button just set the visibility to GONE
layout.findViewById(R.id.negativeButton).setVisibility(
View.GONE);
}
// set the content message
if (message != null) {
((TextView) layout.findViewById(
R.id.message)).setText(message);
} else if (contentView != null) {
// if no message set
// add the contentView to the dialog body
((LinearLayout) layout.findViewById(R.id.content))
.removeAllViews();
((LinearLayout) layout.findViewById(R.id.content))
.addView(contentView,
new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
}
dialog.setContentView(layout);
return dialog;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- 여기에서 가장 핵심은 이부분이다 보통의 다이어로그를 레이아웃으로만 커스텀 형식으로 만들다보면 다이얼로그의 테두리때문에 보기 싫어서 포기 할때가있다 하지만 이부분을 추가해서 소스 코드에 적용한다면 완전 내가 원하는 이미지로 만들어진 다이얼로그 박스를 볼수있다 -->
<style name="Dialog" parent="android:style/Theme.Dialog">
<item name="android:windowBackground">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
</style>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="DialogText">
<item name="android:textColor">#FF000000</item>
<item name="android:textSize">12sp</item>
</style>
<style name="DialogText.Title">
<item name="android:textSize">16sp</item>
<item name="android:textStyle">bold</item>
</style>
</resources>
MANIFEST.xml파일
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="np.comp.AlertDialogTest"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Main" android:configChanges="orientation"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
'안드로이드' 카테고리의 다른 글
안드로이드 웹뷰 프로그래스바 android webview prograssbar wheel (0) | 2011.06.24 |
---|---|
안드로이드 타이틀도 커스텀 내용도 커스텀 android alertdialog custom title view (0) | 2011.06.24 |
안드로이드 apk 디컴파일(까보자) (0) | 2011.06.22 |
안드로이드 소스 f3누르면 보이게 하는 방법(android.jar source 보이는 방법) (0) | 2011.06.20 |
안드로이드 assets파일 읽을때 file not found 에러 (0) | 2011.05.20 |