9

I have a Activity like this:

TextView txt_bank = (TextView) findViewById(R.id.txt_search_bank);
    txt_bank.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            dialog_bank = new Dialog(Activity_Search2.this);

            dialog_bank.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
            dialog_bank.setContentView(R.layout.list_bank);
            dialog_bank.show();

now in the list_bank.xml I have about 20 image that I want to set their onClick field (in layout in properties window) to a method. the problem is that my method can't be find because this method should be in layout's Activity but this is a dialog and don't have any activity please help me how to this use onClick ?

nurealam11
  • 537
  • 4
  • 16
Sadegh
  • 769
  • 1
  • 6
  • 21
  • What is your question? be more specific – M D Mar 01 '14 at 17:00
  • 1
    where should I define my method? – Sadegh Mar 01 '14 at 17:03
  • @s i have a solution for this but you have to create separate Dialog for this. – M D Mar 01 '14 at 17:35
  • Kindly go to this :[http://stackoverflow.com/questions/4243704/using-onclick-attribute-in-layout-xml-causes-a-nosuchmethodexception-in-android](http://stackoverflow.com/questions/4243704/using-onclick-attribute-in-layout-xml-causes-a-nosuchmethodexception-in-android) – M D Mar 01 '14 at 17:39
  • Am I understanding correctly, you have images in your dialog (not in your activity) and want to set your dialog's images onclicklistener? Actually, if you set onClick in XML, your activity has to extend OnClickListener and will than handle the click... Of course it is possible to handle your image clicks in the dialog as well, I can explain how, if that's what you want to do... – prom85 May 28 '14 at 19:45

2 Answers2

1

I am not sure that is absolute answer of your question. but i think may be your question's answer became like this. such as

Please follow this step.

1 Android Layout Files

File : res/layout/main.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" >

    <Button
        android:id="@+id/buttonShowCustomDialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Custom Dialog" />

</LinearLayout>

File : res/layout/custom.xml

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

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp" />

    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#FFF" 
        android:layout_toRightOf="@+id/image"/>/>

     <Button
        android:id="@+id/dialogButtonOK"
        android:layout_width="100px"
        android:layout_height="wrap_content"
        android:text=" Ok "
        android:layout_marginTop="5dp"
        android:layout_marginRight="5dp"
        android:layout_below="@+id/image"
        />

</RelativeLayout>
  1. Activity

File : MainActivity.java

public class MainActivity extends Activity {

    final Context context = this;
    private Button button;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        button = (Button) findViewById(R.id.buttonShowCustomDialog);

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

          @Override
          public void onClick(View arg0) {

            // custom dialog
            final Dialog dialog = new Dialog(context);
            dialog.setContentView(R.layout.custom);
            dialog.setTitle("Title...");

            // set the custom dialog components - text, image and button
            TextView text = (TextView) dialog.findViewById(R.id.text);
            text.setText("Android custom dialog example!");
            ImageView image = (ImageView) dialog.findViewById(R.id.image);
            image.setImageResource(R.drawable.ic_launcher);

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

            dialog.show();
          }
        });
    }
}

Best of luck!

nurealam11
  • 537
  • 4
  • 16
0

the way to solve it..

    public class TestDialog extends Dialog implements android.view.View.OnClickListener
{
    protected void onCreate(final Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog);
        ((Button)findViewById(R.id.dialog_btn_mybutton)).setOnClickListener(this);
    }



public void onClick(View view) 
{
    switch (view.getId())
    {
        case R.id.dialog_btn_mybutton:
            //do stuff
            // dismiss();
            // cancel etc.
        break;
    }
}

see here

Community
  • 1
  • 1
AndroidGeek
  • 30,803
  • 14
  • 212
  • 262