Reputation: 465
I have try to show Yes No dialog after clicking a button and send a message in case Yes has been pressed.
my problem is that the Alert showing only after the second click on the button.
the button xml
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/clrTotalButton"
android:clickable="false"
android:enabled="true"
android:onClick="dialogevent"
android:layout_alignParentStart="false"
android:layout_alignParentEnd="false"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:focusable="true"
android:background="@drawable/clrtotal_btn" /
the button definition :
public static View clrTotalBtn;
and the alert window and command:
public void dialogevent(View view){
clrTotalBtn = (Button) findViewById(R.id.clrTotalButton);
clrTotalBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder altdial = new AlertDialog.Builder(DeviceControlActivity.this);
altdial.setMessage("ARE YOU SURE TO CLEAR TOTAL?").setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
writeClrTotal();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = altdial.create();
alert.setTitle("Warning!");
alert.show();
}
});
}
Many Thanks!
Upvotes: 0
Views: 1037
Reputation: 1761
I think removing
android:focusable="true"
from xml file will solve your problem.
Upvotes: 0
Reputation: 154
You can done by using this code
XML Button Code
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/clrTotalButton"
android:onClick="dialogevent"
android:layout_alignParentStart="false"
android:layout_alignParentEnd="false"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:focusable="true"
android:background="@drawable/clrtotal_btn" /
Java Code
public void dialogevent(View view){
AlertDialog.Builder altdial = new AlertDialog.Builder(DeviceControlActivity.this);
altdial.setMessage("ARE YOU SURE TO CLEAR TOTAL?").setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
writeClrTotal();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = altdial.create();
alert.setTitle("Warning!");
alert.show();
}
In your code alrady apply onClick code on XML.
Upvotes: 0
Reputation: 167
Remove button onClickListener from dialogevent(View ) method.
public void dialogevent(View view){
AlertDialog.Builder altdial = new AlertDialog.Builder(DeviceControlActivity.this);
altdial.setMessage("ARE YOU SURE TO CLEAR TOTAL?").setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
writeClrTotal();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = altdial.create();
alert.setTitle("Warning!");
alert.show();
}
});}
Upvotes: 0
Reputation: 1627
remove this
clrTotalBtn = (Button) findViewById(R.id.clrTotalButton);
clrTotalBtn.setOnClickListener(new View.OnClickListener() {
it should be like this:
public void dialogevent(View view){
AlertDialog.Builder altdial = new AlertDialog.Builder(DeviceControlActivity.this);
altdial.setMessage("ARE YOU SURE TO CLEAR TOTAL?").setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
writeClrTotal();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = altdial.create();
alert.setTitle("Warning!");
alert.show();
}
Because you define an onClickListener
again on first click and second showing dialog. If you define click event in xml it is needed to set click listener anymore.
Upvotes: 1
Reputation: 10650
This is because you are defining the OnClick twice. Once in your xml, and once that is clicked, you are defining a new OnClickListener which needs to be clicked in order to show the overlay. Use this code:
public void dialogevent(View view){
AlertDialog.Builder altdial = new AlertDialog.Builder(DeviceControlActivity.this);
altdial.setMessage("ARE YOU SURE TO CLEAR TOTAL?").setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
writeClrTotal();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = altdial.create();
alert.setTitle("Warning!");
alert.show();
}
Upvotes: 0
Reputation: 3851
You have to set the OnClickListener before clicking the Button. Currently you register the listener when clicking the button so you have to click it twice before the action happens. If you want to do it in code you have to register e.g. during onCreate. If you want to go with the xml way you can just remove the code registering the Listener and only leave the content of the actual listener there:
public void dialogevent(View view){
AlertDialog.Builder altdial = new AlertDialog.Builder(DeviceControlActivity.this);
altdial.setMessage("ARE YOU SURE TO CLEAR TOTAL?").setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
writeClrTotal();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = altdial.create();
alert.setTitle("Warning!");
alert.show();
}
edit: and of course clean up your Button XML:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/clrTotalButton"
android:onClick="dialogevent"
android:layout_alignParentStart="false"
android:layout_alignParentEnd="false"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:background="@drawable/clrtotal_btn" />
Upvotes: 0