It's better to create buttons programmatically and pass the context to the declared class like this. change your class to this:
public class MyCustomButton extends Button {
private Context context;
View.OnClickListener myOnlyhandler = new View.OnClickListener() {
public void onClick(View v) {
MyDialogFragment dialog = new MyDialogFragment();
dialog.show(getFragmentManager(), "Tag");
}
};
public void MyCustomButton(Context context){
this.context=context;
}
}
and then declare your button like this:
MyCustomButton button = new MyCustomButton(MainActivity.this);
then you have to add this view to main LinearLayout
.