Jacko Jacko - 11 months ago 109
Android Question

android button onClick don't do anything

I'm trying to implement

onClick
function for a
Button
on a custom
Dialog
but nothing happen when i click on the
Button
.it's the
butEditAdd



MainActivity.java :


FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final AddDialog addDialog = new AddDialog(MainActivity.this);
addDialog.setContentView(R.layout.dialogedit);
final Button butEditAdd =(Button)addDialog.findViewById(R.id.buttonAdd);
//final Button butEditAdd=addDialog.getAdd();
addDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
butEditAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//addDialog.dismiss();
Toast.makeText(getApplicationContext(),"It's working",Toast.LENGTH_SHORT).show();
}
});
addDialog.show();
}
});


addDialog.java:

public class AddDialog extends Dialog implements android.view.View.OnClickListener{

Activity a;
Dialog d;
Button add , cancel;
RadioButton owes,lent ,money,things ;
EditText name ,amount,object,note;
Spinner spin;

public AddDialog(Activity c) {
super(c);
this.a = c;
}

@Override
public void onClick(View v){

};

@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.dialogedit);
add=(Button)findViewById(R.id.buttonAdd);
cancel=(Button)findViewById(R.id.buttonCancel);
owes = (RadioButton)findViewById(R.id.radioButtonOwes);
lent = (RadioButton)findViewById(R.id.radioButtonLent);
money = (RadioButton)findViewById(R.id.radioButtonAmount);
things =(RadioButton)findViewById(R.id.radioButtonThings);
name = (EditText)findViewById(R.id.editName);
object = (EditText)findViewById(R.id.editTextWhat);
amount =(EditText)findViewById(R.id.editTextAmount);
note =(EditText)findViewById(R.id.editTextNote);
spin=(Spinner) findViewById(R.id.spinnerdevise);

owes.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
lent.setChecked(false);
}
});
lent.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
owes.setChecked(false);
}
});
money.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
things.setChecked(false);
object.setEnabled(false);
amount.setEnabled(true);
spin.setEnabled(true);
object.setText("");

}
});
things.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
money.setChecked(false);
object.setEnabled(true);
amount.setEnabled(false);
spin.setEnabled(false);
amount.setText("");
}
});
object.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
things.performClick();
things.setChecked(true);
}
});

amount.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
money.performClick();
money.setChecked(true);

}
});


thanks for help!

Answer Source

Dialog fragment has separate set of life cycles.If you want to create a class adddialog,then extend dialog fragment and in oncreate of adddialog use setcontent view.

If you want to create simple dialog try this line of code.This will solve your problem.

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final Dialog addDialog = new Dialog(this);
            addDialog.setContentView(R.layout.dialogedit);
            final Button butEditAdd =(Button)addDialog.findViewById(R.id.btn);
            //final Button butEditAdd=addDialog.getAdd();
            addDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            butEditAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //addDialog.dismiss();
                    Toast.makeText(getApplicationContext(),"It's working", Toast.LENGTH_SHORT).show();
                }
            });
            addDialog.show();
        }
    });
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download