0

I want to create custom dialog box. in which i displayed two buttons yes and no. i want to set onclicklistener event of this two buttons in my MainActivity.java activity but this two buttons are in different layout so how can I do this?

This is my code.

MainActivity.java

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button B = (Button) findViewById(R.id.button1);
        final Button B1 = (Button) findViewById(R.id.btn1);
        final Button B2 = (Button) findViewById(R.id.btn2);
        final Dialog d = new Dialog(MainActivity.this);
        B.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                d.setContentView(R.layout.dialog);
                d.setTitle("This is custom dialog box");
                d.setCancelable(true);
                d.show();

            }
        });

    }

this is activity_main xml which i inflated in MainActivity.java class.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Button" />
</RelativeLayout>

But my two buttons are in dialog.xml layout.

dialog.xml

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

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/lion" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="20dp"
        android:text="Hey there i am lion"
        android:textAppearance="?android:attr/textAppearanceLarge" />


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:layout_weight="1">
        <Button 
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Yes"
         android:layout_weight="0.50"
        />
    <Button 
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="No"
        android:layout_weight="0.50"
        android:layout_marginLeft="100dp"
        />
    </RelativeLayout>

</LinearLayout>

so how I set btn1 and btn2 onclicklistener event in MainActivity.java class.

Nidhi Bhatt
  • 71
  • 1
  • 3
  • 12

4 Answers4

2

try this way in your onClick(....)

 LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

 View view = inflater.inflate(R.layout.dialog, null);
 d.setContentView(view);

 Button btn1= (Button) view.findViewById(R.id.btn1);
 Button btn2= (Button) view.findViewById(R.id.btn2);

  btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //do your job
        }
    });

    btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           //do your job
        }
    });
M D
  • 47,665
  • 9
  • 93
  • 114
0

Because both Button btn1 and btn2 is in dialog.xml layout so to access both buttons you should use Dialog Context as:

  Dialog d;
private void showDialog(){
   d = new Dialog(MainActivity.this);
   d.setContentView(R.layout.dialog);
   d.setTitle("This is custom dialog box");
   d.setCancelable(true);
   d.show();
   final Button B1 = (Button)d. findViewById(R.id.btn1);
   final Button B2 = (Button)d. findViewById(R.id.btn2);
}

Now class showDialog() to show alert on Button Click

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
0

Try below code:-

    // add listener to button
    buttonClick.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            // Create custom dialog object
            final Dialog dialog = new Dialog(CustomDialog.this);
            // Include dialog.xml file
            dialog.setContentView(R.layout.dialog);
            // Set dialog title
            dialog.setTitle("Custom Dialog");

            // set values for custom dialog components - text, image and button
            TextView text = (TextView) dialog.findViewById(R.id.textDialog);
            text.setText("Custom dialog Android example.");
            ImageView image = (ImageView) dialog.findViewById(R.id.imageDialog);
            image.setImageResource(R.drawable.image0);

            dialog.show();

            Button declineButton = (Button) dialog.findViewById(R.id.declineButton);
            // if decline button is clicked, close the custom dialog
            declineButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // Close dialog
                    dialog.dismiss();
                }
            });


        }

    });

see below link :-

http://androidexample.com/Custom_Dialog_-_Android_Example/index.php?view=article_discription&aid=88&aaid=111

duggu
  • 37,851
  • 12
  • 116
  • 113
0

you need to implement following changes:

public class MainActivity extends ActionBarActivity implements OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
       ....

        B.setOnClickListener(this);

        ....

     }

   @Override
   public void OnClick(View v) {

      int id = v.getId();

      switch(id) {
        case R.id.btn1:
           ... // Your button code here.

        break;
      }

   }

}
Faizan Mubasher
  • 4,427
  • 11
  • 45
  • 81