Android OnClickListener doesn't launch button on separate layout

I have two different layouts. One of them, which loads at the start of Activity, and the other, which loads after running some checks and creates a custom dialog. There is a button in the dialog box to call, atclick has a Toast message at this point in time, so I can confirm that the button is pressed. Unfortunately, I cannot get an answer when a button is pressed. I have been everywhere on the Internet and I cannot find what I am missing.

public class myactivity extends Activity{ Dialog accesspopup; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_myactivity); View inflatedView = getLayoutInflater().inflate(R.layout.dialoglayout, null); final Button cabtn = (Button)inflatedView.findViewById(R.id.cb); cabtn.setOnClickListener(cListener); } private OnClickListener cListener = new OnClickListener() { public void onClick(View v) { //Log.d("HiThereActivity", "THIS IS DEBUG OUTPUT TO LOGCAT"); Toast.makeText(myactivity.this, "The Start button was clicked.", Toast.LENGTH_LONG).show(); } }; public void showPopup(){ accesspopup = new Dialog(myactivity.this); accesspopup.setContentView(R.layout.pop_window); accesspopup.setCancelable(false); accesspopup.setTitle("Window Title"); accesspopup.show(); } 
+4
source share
3 answers

I did a bit of work and found that I need to create an OnClickListener inside the method that I use to create and display a dialog box, and not in OnCreate.

+1
source

Maybe your R.layout.activity_myactivity is a Contentview Managed in your activity.

So, you should define your new layout as setContentView.

or you mentioned that this is a Dialog field.

That way, you can add a content view for the dialog as shown below.

 Dialog d = new Dialog (this); d.setContentView(your inflated view); 
0
source

use this method ...

 public class myactivity extends Activity{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_myactivity); View inflatedView = getLayoutInflater().inflate(R.layout.dialoglayout, null); final Button cabtn = (Button)inflatedView.findViewById(R.id.cb); cabtn.setOnClickListener(new OnClickListener() { public void onClick(View v) { Toast.makeText(myactivity.this, "The Start button was clicked.", Toast.LENGTH_LONG).show(); } }); } 
0
source

All Articles