Search code examples
androidandroid-widgetandroid-alertdialogandroid-theme

Android: Alert dialog with custom dialog and neutral button


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:

I have to make a dialog which looks like this

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:

Till now implemented

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.


Solution

  • 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.