Reputation: 939
im trying to create custom dialog, but im getting an error when calling show() function. I am not sure if i missed some step in creating the dialog. here's the code:
public class TrackerSettingsActivity extends Activity {
Context context;
PhoneStateBroadcastReciever prefs;
Handler handler;
SharedPreferences m_sharedPrefs;
Editor editor;
EditText timerDelay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tracker_settings);
timerDelay = (EditText) findViewById(R.id.editText_Set_Timer);
m_sharedPrefs = getSharedPreferences("MyPrefs", 0);
editor = m_sharedPrefs.edit();
Button button = (Button) findViewById(R.id.button_Confirm);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
customDialog().show();
}
}
private Dialog customDialog() {
final Dialog dialog = new Dialog(getBaseContext());
dialog.setContentView(R.layout.login_dialog);
dialog.setTitle("Title...");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Android custom dialog example!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
return dialog;
}
}
login_dialog.xml:
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/image"
android:text="sss"
android:textColor="#FFF" />
<Button
android:id="@+id/dialogButtonOK"
android:layout_width="100px"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/text"
android:text=" Ok " />
tracker_settings.xml:
<TextView
android:id="@+id/textView_Set_Timer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="68dp"
android:text="@string/tv_settimer"
android:textColor="#ffffff" />
<EditText
android:id="@+id/editText_Set_Timer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/textView_Set_Timer"
android:layout_below="@+id/textView_Set_Timer"
android:ems="10"
android:inputType="number"
android:textColorHint="#ffffff" />
<Button
android:id="@+id/button_Confirm"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText_Set_Timer"
android:onClick="click"
android:text="@string/bn_confirm" />
I dont know why show() wont work... If someone have some other suggestion im am willing to listen... Thanks in advance!
Upvotes: 0
Views: 167
Reputation: 5246
Your custom dialog can't be added due to it not having a parent view. As @jzafrilla has stated, add an activity reference to your constructor, so that android knows what to attach it to.
... = new Dialog(Main.this); // or something similar
If you do this, then you should be able to see your dialog on top of the parent view
Upvotes: 1
Reputation: 1436
Use this: use activity reference when you have to create a Dialog.
Actvity activity = this;
private Dialog customDialog() {
final Dialog dialog = new Dialog(activity); // or new Dialog(YourActivity.this);
dialog.setContentView(R.layout.login_dialog);
....
}
Upvotes: 2