Android: Alert dialog with custom dialog and neutral button
Solution 1
Create custom alert dialog like
public void messageDialog(String title, String message, final Context activity) {
final Dialog myDialog = new Dialog(activity);
myDialog.setContentView(R.layout.messagescreen);
myDialog.setTitle(title);
myDialog.setCancelable(false);
TextView text = (TextView) myDialog.findViewById(R.id.bidmessage);
text.setMovementMethod(ScrollingMovementMethod.getInstance());
text.setText(message);
Button login = (Button) myDialog.findViewById(R.id.buttonlogin);
login.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
myDialog.dismiss();
}
});
Button createAccount= (Button) myDialog.findViewById(R.id.buttoncreateaccount);
createAccount.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
myDialog.dismiss();
}
});
myDialog.show();
}
where R.layout.messagescreen
is your custom created layout as you have shown in your image. try this out and let me know if you face any problem.
Solution 2
Anupam, I know you have selected an answer already, but I though I might add in my 2 cents in how I have done something similar.
I have used AlertDialog instead of Dialog and it floats above the application when called.
private void showLoginDialog() {
// layout and inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
View content = inflater.inflate(R.layout.dialog_settings, null);
AlertDialog.Builder dialog = new AlertDialog.Builder(new ContextThemeWrapper(getActivity(), android.R.style.Theme_Holo_Light));
dialog.setTitle(getString(R.string.title_settings_dialog));
dialog.setView(content);
Button butLogin = (Button) content.findViewById(R.id.log_in);
butLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Do your Login stuff here or all another method
// does not close dialog
}
});
Button butAccount = (Button) content.findViewById(R.id.create_account);
butAccount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Do your Create Account stuff here or all another method
// does not close dialog
}
});
dialog.setNegativeButton(R.string.skip, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// do nothing, just allow dialog to close
}
});
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
// do nothing, just allow dialog to close
// this happens on back button getting hit
}
});
dialog.show();
}
Related videos on Youtube
![Anupam](https://i.stack.imgur.com/onuo6.jpg?s=256&g=1)
Comments
-
Anupam almost 4 years
I'm editing my question for better understanding for what I need, because answer below were not giving me proper solution for this. So, what I have to do is make a custom alert dialog with a neutral button at the bottom. Here is the example what it should look like:
Till now I have used custom dialog using xml and changing activity to
android:theme="@android:style/Theme.Holo.Light.Dialog"
, so I can get the look and feel like the same. Here is my xml code:<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/dialog" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/login_prompt_header" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="15dp" android:text="@string/login_prompt_rewards_header" android:textSize="17sp" android:textStyle="bold" > </TextView> <TextView android:id="@+id/login_prompt_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/login_prompt_header" android:layout_marginTop="5dp" android:gravity="center_vertical|center_horizontal" android:paddingLeft="18dp" android:paddingRight="18dp" android:text="@string/login_prompt_rewards_text" android:textSize="15sp" > </TextView> <Button android:id="@+id/log_in" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/login_prompt_text" android:layout_centerHorizontal="true" android:layout_gravity="center_horizontal" android:layout_marginTop="15dp" android:background="@drawable/log_in" android:contentDescription="@string/none" > </Button> <Button android:id="@+id/create_account" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/log_in" android:layout_centerHorizontal="true" android:layout_gravity="center_horizontal" android:layout_marginTop="10dp" android:background="@drawable/create_account" android:contentDescription="@string/none" > </Button> </RelativeLayout>
By the above layout I'm getting the following outcome:
The problem I'm facing is: How to set neutral button in the custom dialog? I'm not getting any idea regarding this. If you have doubt in my question or you can't understand the language please leave comment, so that I can again give you a clear idea. Help will be appreciated.
-
Anupam over 11 yearsThis solution is not helping me. I want one neutral button also with using custom layout for alert dialog. Do you have any idea about that?
-
Abhinav Singh Maurya over 11 yearswell friend you can put as many button as you can in this alert dialog. Those buttons can be implemented in your layout
-
Anupam over 11 yearsPlease see the edited question, what I'm asking. BTW thank you for your effort friend.
-
Abhinav Singh Maurya over 11 yearswell friend while using custom you cannot use default features like neutral buttons. You can create your own view for that. But according to your problem custom alert dialog is good. thanks
-
Anupam over 11 yearsSo, for now how can I use "Skip" button, using same xml layout?
-
Abhinav Singh Maurya over 11 yearsYou can add a Skip button by yourself in the layout. may be a clickable layout with a text view in it named skip and then add listener on that layout or you can just implement a button in it named skip. You can add any thing in that layout
-
Abhinav Singh Maurya over 11 yearsyou are wellcome my friend
-
Davideas about 9 yearsActually you can use neutral button and others 2 buttons (neg/pos)... Inside the
onCreateDialog
you should use:builder.setView(customView).setNeutralButton(R.string.CANCEL, new DialogInterface.OnClickListener() {...});
-
Davideas about 9 yearsThis is a more correct answer! - However, better to put the Builder inside the DialogFragment in
onCreateDialog
method properly overridden. making the Dialog a singleton and just call from the activity YourDialog.newInstance().show(getFragmentManager(), YourDialog.TAG); -
Talung about 9 yearsCheers, I will take note of that for when I use it again Davidea.