1)MainActivity.java
package com.manish.dynamicspinner; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.widget.LinearLayout; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; public class MainActivity extends Activity { String countryName[] = { "India", "Pakistan", "China", "Nepal", "Bangladesh" }; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.linear1); for (int k = 1; k <= 20; k++) { //create text button TextView title = new TextView(this); title.setText("Question Number:" + k); title.setTextColor(Color.BLUE); mLinearLayout.addView(title); // create radio button final RadioButton[] rb = new RadioButton[5]; RadioGroup rg = new RadioGroup(this); rg.setOrientation(RadioGroup.VERTICAL); for (int i = 0; i < 5; i++) { rb[i] = new RadioButton(this); rg.addView(rb[i]); rb[i].setText(countryName[i]); } mLinearLayout.addView(rg); } } }2)activity_main.xml
Nice... is it possible to download it in zip ? Thx
ReplyDeleteHi GG thanks for your comment. Please don't mind it is a very simple demo an activity and a layout so just copy paste it and enjoy.
DeleteThanks
sir, what aplication to write this syntac??
ReplyDeletejava or eclipse??
This syntax for Android(Java). you can use any IDE or simple notepad no issue...
Deletehow to get the selected item.
ReplyDeletetext.getText().toString();
DeleteSure your previous experiance help you to learn java. Nothing is impossible. Best of luck..
ReplyDeletevery useful and how to save the result when using the database ....?
ReplyDeleteYou can put a Submit button for save data into database. And simply get all data from selected radio button pass to database.
Deletesir, excellent tutorial .... i want to know how to do click event for radio buttons. ie.... here i want to display a message as the radio button is clicked...e.g if radiobutton 1 is clicked then i need it as "Radiobutton 1 is clicked.."can u pls help me its very urgent
ReplyDeletePlease use onItemSelected..
DeleteHi Manish,
ReplyDeleteI need small help. My requirement is getting the listview with Questions and options(which has radio buttons)
I used the same logic what you made but when I scroll the radio buttons are getting duplicates (Like scrolling twice gives two sets of radio buttons,thrice gives three sets of radio buttons ) I used view.setTag(viewHolder) method to eliminate this issue.Still it is not solved.
Please give me an idea how to resolve it.
Is it? Okay so where are your question-answers? on server or sqlite? no issue so just use listview and in every row generate dynamic radio button. Or may be on scroll you can add item.clear() if it is work so every-time your item will be new.
ReplyDeleteQuestion-Answers are coming from SQlite
Deleteso just put them inside list-view. It will be right approach to do this task.
DeleteHi Manish can you please give a reference code how to get all the selected values when a button clicked thankyou...
ReplyDeleteOn click of button just get the all selected item. Any issue here?
DeleteHi manish thanku for ur reply.In the above code there is no id's for radio group as well as radio buttons i tried to set the id's dynamically and tried to get the all the selected items on a button click. But am unable to get the values of check radio buttons i used below code for this.....
Deleteint selectedId = radioGroup.getCheckedRadioButtonId();
I have a question
ReplyDeleteif i upload folder at my website and i make user and password for the folder
i mean if my website like it www.name.com and i upload folder name : india
local host like this : www.name.com/india
what the code can i use of eclipse pro ?
I copy pasted the code ....only changed the name of the package as "com.example.questions" and i got an error- "didn't find class android.view.scrollview" in eclipse. Can you point out what i might be doing wrong?? .
ReplyDeleteThe name of package is set as per the name of the project i created, so it should be fine i think
Got the error ...scrollview should be ScrollView and linearlayout should be LinearLayout in the XML file.
DeleteGood luck :)
DeleteHow to Loop get value ?
ReplyDeletehow do i add on click listener for each of the radio group?
ReplyDeleteI think you can use add id for radiogroup and then get them when you needed-
Deleterg.addId(i);
i have tried that with setOnCheckedChangeListener, but no response for whichever radio button i tried
DeleteHow to get selected value form particular group?
ReplyDelete