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.