I have dialog layout xml file which stores button, and I have set android:onClick="doSomething", but I dont have any class files attached to that dialog. So practically dialog_layout.xml is the only file that I have in my project. I am able to show it and able to interact with it, but I can’t make onClick listener for the button in dialog.
I tried making the Activity that launches my dialog an owner of that dialog and then in that Activity create public void doSomething(View view) but it still doesnt seem to work. I get this error:
08-03 23:39:23.085: E/AndroidRuntime(6757): java.lang.IllegalStateException: Could not find a method doSomething(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.Button with id 'button2'
any idea what am I doing wrong?
EDIT
here is the code:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
findViewById(R.id.button2).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//Do something.
}
});
Preference keyP = (Preference) findPreference("keyP");
keyP.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
Dialog dialog = new Dialog(SettingsActivity.this);
dialog.setContentView(R.layout.draws_dialog);
dialog.setTitle("Title");
dialog.show();
return true;
}
});
Your code looks good except you need to add button from code as it is recommended. After you loaded dialog layout the following code needs to be added. Note that you need to get rid of buttons in your xml layout, in this case android will create buttons with current theme style.
If you want your button has a specific design or features then you can use following:
Click listeners can be moved out of dialog generation body.
And dialog generation body will look like this: