androidxmlandroid-layoutandroid-custom-viewandroid-custom-attributes

Android Custom Alert with XML - Button Click Action


I have 2 layout files, one for main layout showing (activity_main.xml) and another is for custom dialog showing (dialog_custom.xml). The 2 files are as follows-

activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <Button android:id="@+id/btn_1_option"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Show Alert with 1 Button"
            android:padding="10dip"
            android:layout_marginTop="40dip">
        </Button>

        <Button android:id="@+id/btn_2_option"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Show Alert With 2 Buttons"
            android:padding="10dip">
        </Button>

        <Button android:id="@+id/btn_3_option"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Show Alert with 3 Buttons"
            android:padding="10dip">
        </Button>

        <Button android:id="@+id/btn_custom_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Show Alert with Custom Layout - From XML"
            android:padding="10dip">
        </Button>

    </LinearLayout>

dialog_custom.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/dialog_info"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:background="@android:color/holo_orange_dark"
            android:text="@string/dialog_text"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_below="@id/dialog_info">

            <Button
                android:id="@+id/dialog_cancel"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.50"
                android:background="#3333"
                android:text="Cancel"/>

            <Button
                android:id="@+id/dialog_ok"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.50"
                android:background="#888888"
                android:text="Agree"/>
        </LinearLayout>
    </RelativeLayout>

And what I have done in the Main class is -

    public class Activity_Main extends Activity
    {
        Context context = Activity_Main.this;
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            final Dialog dialog = new Dialog(context);
            dialog.setContentView(R.layout.dialog_main);
            dialog.setTitle(R.string.dialog_title);
            dialog.show();
        }
    }

So, I get a dialog like this-

Output

Now I want to add on click option for this CANCEL and AGREE button. But if I want, I am getting forced close. Can anyone please help me, how can I do it?

Thanks for helping.


Solution

  • Do it as follows :

    final Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.layout.dialog_main);
    Button btCancel = (Button) dialog.findViewById(R.id.dialog_cancel);
    Button btOk = (Button) dialog.findViewById(R.id.dialog_ok);
    btCancel.setOnClickListener(new View.OnClickListener() {
     @Override
       public void onClick(View v) {
       //Cancel Click
    
       }
    });
    btOk.setOnClickListener(new View.OnClickListener() {
     @Override
      public void onClick(View v) {
      //OK CLICK      
    
      }
    });
    dialog.show();
    

    If you want to add Buttons or any Widget (inside of the Dialog) make sure that you are finding the view with dialog.findViewById(R.id.ID_WIDGET) otherwise it will crash.