here I have a custom dialog with background 2 ImageButton inside it.
the problem is, when I try to set onclick listener to that buttons, the program will return NullPointerException. I don't know why is this happen. how to assign operation to button inside dialog anyway??
pause menu xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:id="#+id/linearLayout1" android:layout_width="wrap_content" android:background="#drawable/pause_menu_cropped" android:layout_gravity="center" android:gravity="center|center_horizontal">
<TableLayout android:layout_width="wrap_content" android:id="#+id/tableLayout1" android:layout_height="wrap_content">
<ImageButton android:src="#drawable/pause_button_option" android:layout_width="wrap_content" android:background="#drawable/pause_button_option" android:layout_height="wrap_content" android:id="#+id/btn_pause_option"></ImageButton>
<ImageButton android:src="#drawable/pause_button_quit" android:layout_width="wrap_content" android:background="#drawable/pause_button_quit" android:layout_height="wrap_content" android:id="#+id/btn_pause_quit"></ImageButton>
</TableLayout>
</LinearLayout>
dialog code
Dialog pauseMenu = new Dialog(this, R.style.NewDialog);
pauseMenu.setContentView(R.layout.pause_menu);
ImageButton quit = (ImageButton)findViewById(R.id.btn_pause_quit);
quit.setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View v) {
TestActivity.this.finish();
}
});
return pauseMenu;
the code is returning error in line
quit.setOnClickListener();
ImageButton quit =
(ImageButton)findViewById(R.id.btn_pause_quit);
should be
ImageButton quit = (ImageButton)pauseMenu.findViewById(R.id.btn_pause_quit);
This happens because findViewById is invoked for the activity, and it doesn't have btn_pause_quit button in it's layout. But your dialog has.
U can use this custom dialog and onclicklistener..
public class CustomizeDialog extends Dialog implements OnClickListener {
Button okButton;
public CustomizeDialog(Context context) {
super(context);
/** 'Window.FEATURE_NO_TITLE' - Used to hide the title */
requestWindowFeature(Window.FEATURE_NO_TITLE);
/** Design the dialog in main.xml file */
setContentView(R.layout.main);
okButton = (Button) findViewById(R.id.OkButton);
okButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
/** When OK Button is clicked, dismiss the dialog */
if (v == okButton)
dismiss();
}
}
I think your onClickListener should be DialogInterface.OnClickListener
Related
Am trying to click on a button i just added to an AlertDialog but its not responding. The button is on a layout file in the resources folder and then i added this layout to the AlertDialog via the Builder method setView()
I accessed the same Button via my mainactivity after inflating a random view with the layout and set an OnClickListener but its still not working
Here is detail code of what i have tried so far
Layout containing the button
<LinearLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#FFF"
android:background="#drawable/round2"
android:textSize="20sp"
android:textAlignment="center"
android:padding="5dp"
android:text="Submit"
android:textAllCaps="false"
android:drawablePadding="10sp"
android:id="#+id/submit"
/>
</Linearlayout>
Then in my MainActivity.class, i access the button this way
class MainActivity extends AppCompatActivity implements View.OnClickListener{
Button submit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//inflate layout containing button
datView=getLayoutInflater().inflate(R.layout.data_entry,null);
//access button
submit=datView.findViewById(R.id.submit);
//set event listener
submit.setOnClickListener(this);
}
//the interface
#Override
public void onClick(View v) {
check();
}
//custom method check
private void check(){
Toast.makeText(this,"Registering data into database",Toast.LENGTH_LONG).show();
}
}
Despite all that the toast does not show on clicking the button, please help
Please try using android:onClick = "check" in the button in your xml, and also create a method in your MainActivity called public void check(View view){...} with the toast inside of it
When you click on the image button, pop up notification pops up. How do I customize the "ok" and "cancel" button to instead of using the default look of the buttons, I want to use my own custom ImageButtons as "ok" and "cancel".
Here's my code for pop up notification.
public class Notifications extends AppCompatActivity {
ImageButton Notifications;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notifications);
Notifications = (ImageButton) findViewById(R.id.AllowNotifications);
Notifications.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(Notifications.this);
builder.setCancelable(false); //False= ONLY way to exist notification is by clicking NO
//True= Exit notification by clicking anywhere on screen outside of notification box.
builder.setTitle("Here is the alert dialog");
builder.setMessage("Here is my message thing");
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int WhichButton) {
dialog.cancel();
}
});
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}
});
}
}
Here's the default pop up notification with the above code:
So instead of there being an "ok" and "cancel" in red color, I want to put the "ok" and "cancel" as my own custom image buttons and I'd want to change the color from red to something else. How do I go about doing this inside the Pop Up notification?
As the documentation says, in the Creating a Custom Layout session, you can create a custom layout and inflate it at your Dialog.
To use another button than the one create by the AlertDialog.Builder you will need to handle the click listener of them.
This is the layout I created to test the solution:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#android:color/white"
android:orientation="vertical"
android:padding="20dp">
<TextView
android:id="#+id/dialogTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Here is the alert dialog"
android:textColor="#android:color/black"
android:textSize="16sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/dialogSubtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Here is my message thing"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp">
<Button
android:id="#+id/positiveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:background="#android:color/transparent"
android:text="OK"
android:textColor="#android:color/holo_red_light"/>
<Button
android:id="#+id/negativeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="40dp"
android:layout_toStartOf="#id/positiveButton"
android:background="#android:color/transparent"
android:text="Cancel"
android:textColor="#android:color/holo_red_light"/>
</RelativeLayout>
</LinearLayout>
And the code to make it run:
LayoutInflater layoutInflater = LayoutInflater.from(this);
View promptView = layoutInflater.inflate(R.layout.test, null);
final AlertDialog alertD = new AlertDialog.Builder(this).create();
TextView title = (TextView) promptView.findViewById(R.id.dialogTitle);
TextView subtitle = (TextView) promptView.findViewById(R.id.dialogSubtitle);
title.setText("My new Custom Dialog");
subtitle.setText("With everything that I want");
Button positive = (Button) promptView.findViewById(R.id.positiveButton);
Button negativeButton = (Button) promptView.findViewById(R.id.negativeButton);
positive.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// btnAdd1 has been clicked
}
});
negativeButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// btnAdd2 has been clicked
}
});
alertD.setView(promptView);
alertD.show();
This is an screenshot of how it looks like in my phone. Feel free to change the layout in the way it better fits your needs.
Thanks to Vikram that explains it very well in this answers for other question, but I thought that a specific code for your question would be better.
If you want to customize everything, the look of the dialog, add your own buttons, TextViews etc. - you need to make a class that extends DialogFragment and implements View.OnClickListener and you need to create your own layout with two custom made buttons for that. Give them ids and set OnClickListeners
As typed in: https://developer.android.com/reference/android/app/DialogFragment.html
public static class MyDialogFragment extends DialogFragment implements View.OnClickListener {
static MyDialogFragment newInstance() {
return new MyDialogFragment();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.dialog_fragment, container, false);
v.findViewById(R.id.btn_ok).setOnClickListener(this);
return v;
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_ok:
// do something
break;
default:
break;
}
}
}
And from your Activity you do:
void showDialog() {
// Create the fragment and show it as a dialog.
DialogFragment newFragment = MyDialogFragment.newInstance();
newFragment.show(getFragmentManager(), "dialog");
}
I have a button that calls a dialog. From that button i have 8 buttons: 1,2,3,4,5,6,7, and cancel. These buttons will be used to change the text of the button. The thing is that it doesn't do anything if i set the text inside the dialog.
buttonDefineHits = (Button) rowView.findViewById(R.id.button_define_hits);
buttonDefineHits.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Dialog 1-7 i x para definir los holes
setDialogSetHits();
}
});
.
private void setDialogSetHits(){
final Dialog dialogConfirmPlayers = new Dialog (activity);
dialogConfirmPlayers.setCancelable(false);
dialogConfirmPlayers.setContentView(R.layout.dialog_set_hits);
Button button1Hit = (Button) dialogConfirmPlayers.findViewById(R.id.button_1_hit);
button1Hit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Dialog 1-7 i x para definir los holes
buttonDefineHits.setText("1");
dialogConfirmPlayers.cancel();
}
});
dialogConfirmPlayers.show();
}
You can set the Text of a Button that is defined in a activity from the dialogBox. I guess Aniruddha is wrong in his comment. I mean yes the user cannot have a "Iteration" with the Activity's UI elements as long as a Dialog is shown over it, But programatically you can change the Text property of the Button in your activity. To confirm, this is what I tried:
Created a Dialog on the click event of ImageView.
From the Dialog Button's Click listener, I changed the Text of a editText in the Activity.
Similarly, you should also be able to set the text of the button from the dialog button's click listener.
I think you should remove the 7 buttons from your dialog, and for testing purpose just have one button on it. Then handle the click event on this button and try n set the Activity button's Text. This should work like charm.
Then later you can integrate your 7-buttons.
Here is the working example, you need to modify it accordingly
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:id="#+id/tv1"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:id="#+id/buttonMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tv1"
android:layout_below="#+id/tv1"
android:layout_marginTop="44dp"
android:text="Button" />
</RelativeLayout>
dialog_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel" />
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
TextView t;
Button bMain;
Dialog d;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t = (TextView) findViewById(R.id.tv1);
bMain = (Button) findViewById(R.id.buttonMain);
bMain.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
d = new Dialog(MainActivity.this);
d.setTitle("Hello Android..!");
d.setContentView(R.layout.dialog_view);
Button bOK = (Button) d.findViewById(R.id.button1);
Button bCancel = (Button) d.findViewById(R.id.button2);
d.show();
bOK.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
t.setText("OK");
bMain.setText("Changed the text");
d.cancel();
}
});
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
I have a button with id "ok" in my dialog.
Partial - Dialog Layout:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="8dp"
android:layout_below="#+id/licenseTypeLayout">
<Button
android:id="#+id/ok"
style="#style/agreement_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Ok"/>
</LinearLayout>
In activity:
Dialog dialog;
#OptionsItem(R.id.action_about)
boolean displayPopup() {
dialog = new Dialog(this);
dialog.setContentView(R.layout.about_dialog);
dialog.setTitle(R.string.app_name);
dialog.show();
Button btn=(Button)findViewById(R.id.ok);
//btn remains empty
return true;
}
How would I write onClick() event for this button?
To initialize the Button, you need to use the Dialog object
Button btn = (Button) dialog.findViewById(R.id.ok);
Then set OnClickListener to the Button using setOnClickListener() as follows...
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
//write your code here
}
});
I would guess you make the method:
public void closeDialog(View v){
// Do what you want to do here (event handler)
}
?
This question already has answers here:
How to prevent a dialog from closing when a button is clicked
(21 answers)
Closed 3 years ago.
The subject kinda says it all.. I'm requesting a PIN code from the user, if they enter it, click the OK Positive Button and the PIN is incorrect I want to display a Toast but keep the dialog open. At the moment it closes automatically.. Sure this is very trivial thing to correct but can't find the answer yet.
Thanks..
You do not need to create a custom class. You can register a View.OnClickListener for the AlertDialog. This listener will not dismiss the AlertDialog. The trick here is that you need to register the listener after the dialog has been shown, but it can neatly be done inside an OnShowListener. You can use an accessory boolean variable to check if this has already been done so that it will only be done once:
/*
* Prepare the alert with a Builder.
*/
AlertDialog.Builder b = new AlertDialog.Builder(this);
b.setNegativeButton("Button", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {}
});
this.alert = b.create();
/*
* Add an OnShowListener to change the OnClickListener on the
* first time the alert is shown. Calling getButton() before
* the alert is shown will return null. Then use a regular
* View.OnClickListener for the button, which will not
* dismiss the AlertDialog after it has been called.
*/
this.alertReady = false;
alert.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialog) {
if (alertReady == false) {
Button button = alert.getButton(DialogInterface.BUTTON_NEGATIVE);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do something
}
});
alertReady = true;
}
}
});
Part of this solution was provided by http://groups.google.com/group/android-developers/browse_thread/thread/fb56c8721b850124#
Build a custom dialog with a EditText with the attribute android:password="true" a button, then manually set onClick listener the button, and explicitly choose what to do in it.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minWidth="180dip"
android:digits="1234567890"
android:maxLength="4"
android:password="true"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/Accept"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Accept"/>
</LinearLayout>
</LinearLayout>
Then when you want it to pop up:
final Dialog dialog = new Dialog(RealizarPago.this);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("PIN number:");
dialog.setCancelable(true);
Button button = (Button) dialog.findViewById(R.id.Accept);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(password_wrong){
// showToast
} else{
dialog.dismiss();
// other stuff to do
}
}
});
dialog.show();
You can set an OnClickListener as follows to keep the dialog open:
public class MyDialog extends AlertDialog {
public MyDialog(Context context) {
super(context);
setMessage("Hello");
setButton(AlertDialog.BUTTON_POSITIVE, "Ok", (new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// this will never be called
}
});
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (ok) {
// do something
dismiss();
} else {
Toast.makeText(getContext(), "when you see this message, the dialog should stay open", Toast.LENGTH_SHORT).show();
}
}
});
}
}
You can just continue using the dialog you already have, just put an if clause in the onClick() saying
if(pin_check_method){ //pin_check_method should be a boolean returned method
//close the Dialog, then continue
}
else{
//dont put the dialog.dismiss() in here, put instead
Toast.makeText(getApplicationContext(),"Invalid pin, please try again",Toast.LENGTH_LONG).show();
}
Now, to use this code, simply invoke text.setText(""); and put in the text you want here
common error is that when you type in:
TextView text = (TextView) findViewById(R.id.dialog);
you miss that it needs to actually be
dialog.findViewById
and this is regardless of what the name of the dialog is, in my example it just happens to be the same name.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="#+id/text"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"/>
<Button android:text="Continue"
android:id="#+id/Button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_below="#+id/text">
</Button>
</RelativeLayout>
Try this:
final AlertDialog alertDialog = new AlertDialog.Builder(context)
.setView(v)
.setTitle(R.string.my_title)
.setPositiveButton(android.R.string.ok, null) //Set to null. We override the onclick
.setNegativeButton(android.R.string.cancel, null)
.create();
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialog) {
Button b = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// TODO Do something
}
});
}
});
alertDialog.show();
Source: Prevent Alertdialog from closing after button click
Hope This Helps! Good Luck!
Same problem for me in a FragmentDialog. Here's my criminal/elegant solution:
Remove all buttons from the dialog (positive,negative,neutral). Add your buttons from the xml.eg.:
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="#+id/button_cancel"
style="#style/Widget.AppCompat.Button.Borderless.Colored"
android:text="#android:string/cancel"
android:layout_gravity="left"
/>
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="#+id/button_ok"
style="#style/Widget.AppCompat.Button.Borderless.Colored"
android:text="#android:string/ok"
android:layout_gravity="right"
/>
</LinearLayout>
And then in your code handle it with:
view.findViewById(R.id.button_ok).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view2) {
if (wannaClose)
dismiss();
else
//do stuff without closing!
}
});
where view is the view assigned to the dialog!