71058

sending data to an intent depending on a view (a radio button)

Question:

i have an activity showing a dialog which contains radioButtons, and to buttons (Ok,cancel)

<ol><li>can i launch an activity from this dialog ? </li> <li>how to send the id of the radioButton selected to the second activity ?</li> </ol>

Answer1:

in the onClick() method of the OK Button, you can add this code to start another Activity and pass the id of the checked radio button :

Intent intent = new Intent(FirstActivity.this, SecondActitvity.class); intent.putextras("extra_selected_radio_button", checkedRadioButton.getId()); startActivity(intent);

and in the onCreate() method of the SecondActivity , you can retrieve the id of the selected radio button like this :

Intent intent = getIntent(); int radioButtonId = intent.getIntExtra("extra_selected_radio_button", -1); // -1 is the default value

<strong>PS : Check this <a href="http://www.android-ios-tutorials.com/117/how-to-switch-between-different-activities-in-android/" rel="nofollow">tutorial</a> to learn more about sending data between activities in Android.</strong>

Answer2:

Yes you can:

<ol><li>On the OK button listener just do startActivity(intent)</li> <li>Before the previous method call you have to put an extra in the intent, like so intent.putExtra("radioButtonId", radioButtonValue)</li> </ol>

Answer3:

To start an activity from your dialog you'll have to add this into your button's onClick() code:

Intent intentLoad = new Intent(getBaseContext(), YourNewActivity.class); startActivityForResult();

If you want to send data to your activity, use: putExtra

Like this:

Intent intentLoad = new Intent(getBaseContext(), YourNewActivity.class); intent.putExtra("radioID", id-of-your-radioButton); startActivityForResult();

You can catch your id in the activity with:

int myRadioId = extras.getInt("radioID");

Recommend

  • How to move again MainActivity after sending the mail?
  • Android - Call default browser with and redirect to a designated url
  • Receive custom intent without activity restart
  • Android NFC read Tags issue. Activity starts each time on data received
  • How to protect an asp:textbox from user input?
  • Wait for .each() .getJSON request to finish before executing a callback
  • passing parameter to DownloadStringCompletedEventHandler in C#
  • Send data from edittext to listview
  • Configuring multiple DefaultJmslistenercontainerfactory
  • EditText is covered by Keyboard
  • How to merge keras sequential models with same input?
  • (Tcl/Expect) clear screen after exit
  • Faster Way To Simultaneously Iterate Over Rolling Window Of Two Or More Numpy Arrays?
  • Recording values of radio buttons in ember
  • What command do i need to pass in SabreCommandLLSRQ to get current price of PNR?
  • How can go in last activity with resume button?
  • Flash radiobutton: how do I get the selected radiobutton?
  • Error in installing package: fatal error: stdlib.h: no such file or directory
  • JSR-330 support in Picocontainer : @Inject … @Named(\"xxx)
  • Creating a DropDownList
  • How to make R's read_csv2() recognise the text characters properly
  • Who propagate bugfixes across branches (corporate development)?
  • How do I display a dialog that asks the user multi-choice questıon using tkInter?
  • URLConnection doesn't work since API 10 and higher?
  • Android application: how to use the camera and grab the image bytes?
  • Unity3D & Android: Difference between “UnityMain” and “main” threads?
  • Dialing with Intent.ACTION_CALL stopps at # in phone number
  • Android fill_parent issue
  • Scrapy recursive link crawler
  • Deselecting radio buttons while keeping the View Model in synch
  • Nant, Vault & Windows Integrated Authentication
  • jquery mobile loadPage not working
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • python regex in pyparsing
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How do you join a server to an Active Directory (domain)?
  • Android Google Maps API OnLocationChanged only called once
  • unknown Exception android
  • How can I use threading to 'tick' a timer to be accessed by other threads?