0

I'm implementing a custom dialog, but in the activity, I'm having a trouble. This is the code:

public class MainActivity extends Activity { private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button = (Button) findViewById(R.id.buttonDialog); /*Add button listener*/ button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { /*Custom dialog*/ final Dialog dialog = new Dialog(this); dialog.setTitle("Cerrar App"); dialog.setContentView(R.layout.custom_dialog); //... 

In the line final Dialog dialog = new Dialog(this); it's throwing me an error that says: The constructor Dialog(new View.OnClickListener(){}) is undefined.

What I'm doing wrong?

3 Answers 3

6

Change

final Dialog dialog = new Dialog(this); 

with

final Dialog dialog = new Dialog(MainActivity.this); 

in your case this refers to inner onClickListener

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, works! But, could you provide explanation about this? I don't understand why some cases acepts only This and others requires the class name before This
this (keyword) refers to the object itself. In your case this refers to the "new OnClickListener()" object and since the argument of Dialog's constructor is a Context and not a View.OnClickListener, you get the error
4

change:

final Dialog dialog = new Dialog(this); 

to

final Dialog dialog = new Dialog(MainActivity.this); 

Comments

-2
Dialog videoDialog = new Dialog(getApplicationContext()); videoDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); videoDialog.setContentView(R.layout.video_dialoge); videoDialog.getWindow().setGravity( Gravity.BOTTOM | Gravity.CENTER_VERTICAL); WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); Window window = videoDialog.getWindow(); lp.copyFrom(window.getAttributes()); lp.width = WindowManager.LayoutParams.FILL_PARENT; lp.height = WindowManager.LayoutParams.WRAP_CONTENT; window.setAttributes(lp); LinearLayout frmalbumb = (LinearLayout) videoDialog .findViewById(R.id.layout_album); LinearLayout frmcamera = (LinearLayout) videoDialog .findViewById(R.id.layout_camera); frmcamera.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent cameraIntent = new Intent( android.provider.MediaStore.ACTION_VIDEO_CAPTURE); startActivityForResult(cameraIntent, capturevideo); videoDialog.dismiss(); } }); frmalbumb.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent mediaChooser = new Intent(Intent.ACTION_GET_CONTENT); mediaChooser.setType("video/*"); startActivityForResult(mediaChooser, galleryVideo); videoDialog.dismiss(); } }); WindowManager.LayoutParams wmlp = videoDialog.getWindow() .getAttributes(); wmlp.gravity = Gravity.BOTTOM; wmlp.x = (img_btn_Video.getLeft() + (img_btn_Video.getLeft() / 2)); if (CommonUtilities.istablet == true) { wmlp.y = 69; } else { if (CommonUtilities.screen_height >= 1920) { wmlp.y = 130; }else if(CommonUtilities.screen_height<=854) { wmlp.y = 75; } else { wmlp.y = 90; } } videoDialog.getWindow().setBackgroundDrawable( new ColorDrawable(android.graphics.Color.TRANSPARENT)); videoDialog.show(); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.