+ Reply to Thread
Results 1 to 2 of 2

Thread: Using Radio Buttons within a Dialog in Android app

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    14

    Using Radio Buttons within a Dialog in Android app

    I have an Android app where I want to offer the user the choice of ordering a table using different fields. I've used a dialog with three radio buttons to offer the choice of each sort type.

    Below is the code within the dialog code which declares the radio buttons
    Code:
            LayoutInflater layoutInflater = (LayoutInflater)
            getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final View layout = layoutInflater.inflate(R.layout.lectindex_dialog, (ViewGroup) findViewById(R.id.lect_index));
    
            builder.setView(layout);
    
            // Now configure the AlertDialog
            builder.setTitle(R.string.exindex_title);                       
    
            final RadioButton radio_date = (RadioButton) findViewById(R.id.RBdate);
            final RadioButton radio_loctn = (RadioButton) findViewById(R.id.RBloctn);
            final RadioButton radio_stream = (RadioButton) findViewById(R.id.RBstream);        
            radio_date.setOnClickListener(radio_listener);
            radio_loctn.setOnClickListener(radio_listener);
            radio_stream.setOnClickListener(radio_listener);
    
            builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    // We forcefully dismiss and remove the Dialog, so it cannot be used again (no cached info)
                    LecturesActivity.this.removeDialog(SELINDEX_DIALOG_ID);
                }
            });
    The code for radio_listener is declared separately like so
    Code:
            private OnClickListener radio_listener = new OnClickListener() {
              public void onClick(View v) {
                // Perform action on clicks
                RadioButton rb = (RadioButton) v;
                Toast.makeText(LecturesActivity.this, rb.getText(), Toast.LENGTH_SHORT).show();
              }
            };
    Everything seems to work ok but the code for radio_listener never gets used, Why?

  2. #2
    Junior Member
    Join Date
    Jul 2012
    Posts
    8
    The reason for the error and radio_listener never being called is that it throws an error when trying to using radio_date as it is not declared properly. When I changed the declaration to final RadioButton radio_date = (RadioButton) layout.findViewById(R.id.RBdate); Where layout is the id for the dialog the code worked perfectly.

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts