Get the radio button pointer from a dynamic radiogroup dialog - android

I have a custom radio group dialog and i am inflating it dynamically.
In my onCreate() I am doing this
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
alarmType1 = (TextView) findViewById(R.id.tvAlarmType);
View view = LayoutInflater.from(this).inflate(R.layout.custom_layout, null, false);
radioAlarmtype = (RadioGroup) view.findViewById(R.id.radiogroup);
initRadioDialog();
then initializing the dialog with the following code
String string_Alarmtype[]={"Melody","Vibration","Melody and Vibration"};
public void initRadioDialog()
{
Context context=AlarmActivity.this;
dialog_radio=new Dialog(context);
dialog_radio.requestWindowFeature(Window.FEATURE_NO_TITLE);
View view = LayoutInflater.from(context).inflate(R.layout.custom_layout, null, false);
radioAlarmtype = (RadioGroup) view.findViewById(R.id.radiogroup);
RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
// add 3 radio buttons to the group
RadioButton rb;
for (int i = 0; i < 3; i++){
rb = new RadioButton(context);
rb.setText(string_Alarmtype[i]);
rb.setId(i);
radioAlarmtype.addView(rb, layoutParams);
}
radioAlarmtype.check(2);
radioAlarmtype.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
RadioButton rb = (RadioButton) findViewById(checkedId);
//alarmType1.setText(rb.getText());
Toast.makeText(AlarmActivity.this, checkedId + "", Toast.LENGTH_SHORT).show();
}
});
dialog_radio.setContentView(view);
}
but above commented line //alarmType1.setText(rb.getText());is causing a nullpointerException
so i tried it on my textview onClickListener
like this
View.OnClickListener alarmTypeListener = new View.OnClickListener() {
public void onClick(View v) {
dialog_radio.show();
RadioButton rb = (RadioButton) findViewById(radioAlarmtype.getCheckedRadioButtonId());
alarmType1.setText(rb.getText());//NUllpointerException here
Toast.makeText(AlarmActivity.this,"HEllo"+rb.getText(),Toast.LENGTH_SHORT).show();
//here also nullpointerException if I comment above line
}
};
But the problem still persist so how can i change the textview as change in the selection of radio button in my dialog.

Related

Dynamically added RadioButtons do not listen to onCheckChangedListener()

I need to add RadioGroups and RadioButtons dynamically and generate desired view and implement onCheckChangedListener() on my dynamically created RadioGroup . The problem is, when a newly added button is checked, it is not unchecked when I am clicking other button. I have already checked this - Issue in radiogroup while adding radiobuttons dynamically , but to no avail.
I am working in a fragment and adding views dynamically inside it. The radiobuttons do not exhibit check changed behavior. Please help me to sort this out.
//optionsGroup(RadioGroup),optionButton(RadioButton)
optionsGroup = new RadioGroup(getActivity());
optionsGroup.setId(radioGroupCount);
optionsGroup.setPadding(20, 20, 20, 20);
optionsGroup.setLayoutParams(layparams);
radioGroupList.add(optionsGroup);
ArrayList<Options> RadioButtonOptions = question.getQuestionOptions().getOptions();
for (int i = 0; i < RadioButtonOptions.size(); i++) {
optionButton = new RadioButton(getActivity());
optionButton.setId(i);
optionButton.setText(RadioButtonOptions.get(i).getOptionValue());
optionsGroup.addView(optionButton);
optionsGroup.clearCheck();
}
optionsGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
for(int rb=0;rb<optionsGroup.getChildCount();rb++){
RadioButton btn = (RadioButton) optionsGroup.getChildAt(rb);
if(checkedId==btn.getId()){
radioGroupAnswerList.clear();
radioGroupAnswerList.add(btn.getText().toString());
return;
}
}
}
});
holderlayout.addView(optionsGroup);
I think problem is you are adding view before assigning its listener . so please try this.
for (int i = 0; i < RadioButtonOptions.size(); i++) {
optionButton = new RadioButton(getActivity());
optionButton.setId(i);
optionButton.setText(RadioButtonOptions.get(i).getOptionValue());
optionsGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
for(int rb=0;rb<optionsGroup.getChildCount();rb++){
RadioButton btn = (RadioButton) optionsGroup.getChildAt(rb);
if(checkedId==btn.getId()){
radioGroupAnswerList.clear();
radioGroupAnswerList.add(btn.getText().toString());
return;
}
}
}
});
optionsGroup.addView(optionButton);
}
You need to uncheck other radiobutton if one radio button clicked right? and all this will be dynamically created radiobutton and radio groupe.. then you have to do this..
//optionsGroup(RadioGroup),optionButton(RadioButton)
optionsGroup = new RadioGroup(getActivity());
optionsGroup.setId(radioGroupCount);
optionsGroup.setPadding(20, 20, 20, 20);
optionsGroup.setLayoutParams(layparams);
radioGroupList.add(optionsGroup);
ArrayList<Options> RadioButtonOptions = question.getQuestionOptions().getOptions();
for (int i = 0; i < RadioButtonOptions.size(); i++) {
optionButton = new RadioButton(getActivity());
optionButton.setId(i);
optionButton.setText(RadioButtonOptions.get(i).getOptionValue());
optionsGroup.addView(optionButton);
optionsGroup.clearCheck();
}
optionsGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
for(int rb=0;rb<optionsGroup.getChildCount();rb++){
RadioButton btn = (RadioButton) optionsGroup.getChildAt(rb);
if(checkedId==btn.getId()){
radioGroupAnswerList.clear();
radioGroupAnswerList.add(btn.getText().toString());
optionsGroup.clearCheck();
if (isChecked) {
optionsGroup.check(btn.getId());
}
return;
}
}
}
});
holderlayout.addView(optionsGroup);

Getting the check changed event for a dynamically created Radio Group within a ListView

We are working on a project where there is a questionnaire with dynamically created radio groups with radio buttons.At the bottom of the listview we have placed a button and we need the selected values from the radio buttons on clicking the button.The issue is how to get the selected Radio button values.Also i wanted to know in which class i can get the values in the main activity or in the Base Adapter class?
We have done the following:
The main activity:
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for(m=0;m<question_list_array.size();m++)
{
View view= question_list.getChildAt(m);
RadioGroup radioGroup= (RadioGroup) view.findViewById(R.id.single_radiogroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
for(int n=0;n<group.getChildCount();n++)
{
RadioButton rb= (RadioButton) group.getChildAt(n);
if (rb.getId() == checkedId) {
String result= rb.getText()+"";
Toast.makeText(MainActivity.this,result, Toast.LENGTH_LONG).show();
// do something with text
return;
}
}
}
});
}
}
});
But i am getting NPE at the following line:
RadioGroup radioGroup= (RadioGroup) view.findViewById(R.id.single_radiogroup);
The adapter class:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
String question_type = question_list_array.get(position).getQuestion_type();
option_list = question_list_array.get(position).option_list;
number_of_options = option_list.size();
v_single=convertView;
if (v_single == null) {
v_single = inflater.inflate(R.layout.single_choice_layout, null);
sh = new SingleHolder();
sh.single_question_name = (TextView) v_single.findViewById(R.id.single_question_name);
sh.single_radiogroup = (RadioGroup) v_single.findViewById(R.id.single_radiogroup);
v_single.setTag(sh);
}
else
{
sh = (SingleHolder) v_single.getTag();
}
sh.single_radiogroup.clearCheck();
sh.single_radiogroup.removeAllViews();
sh.single_question_name.setText(question_list_array.get(position).getQuestion_name());
for (int j = 0; j < number_of_options; j++)
{
RadioButton rb = new RadioButton(context);
rb.setText(option_list.get(j).getOption_name());
sh.single_radiogroup.addView(rb);
}
v_final=v_single;
return v_final;
}
We dont have any issues with the Adapter as such.Please reply at the earliest.

how to create radio group just below on checkbox in android

In my app i have created list view of checkbox in linear layout and i have created dynamic radio group of three radio button.when user click on any checkbox than radio group will be created at the end. but i want to create this radio group just below the clicked checked box. i am trying to set cursor position but not able to do.i don't have any idea what to do for this.
this is part of main.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ViewGroup checkboxContainer = (ViewGroup) findViewById(R.id.checkbox_container);
for (int i = 0; i < 25; i++) {
final CheckBox checkBox = new CheckBox(this);
checkBox.setText(Oils_and_Condiments[i]);
checkboxContainer.addView(checkBox);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked)
{
for (int i = 0; i < Oils_and_Condiments.length; i++) {
String a= (String) checkBox.getText();
if(a==Oils_and_Condiments[i])
{
RadioGroup rg = create_rb(i);
checkboxContainer.get
checkboxContainer.addView(rg);
rg.setVisibility(View.VISIBLE);
//rg.getChildAt(i).setVisibility(View.VISIBLE);
}
RadioGroup create_rb(int a ){
final RadioGroup radiogp = new RadioGroup(this);
radiogp.setOrientation(RadioGroup.HORIZONTAL);
RadioButton rb0 = new RadioButton(this);
RadioButton rb1 = new RadioButton(this);
RadioButton rb2 = new RadioButton(this);
rb0.setText("1KG");
rb0.setTextSize(10);
rb1.setText("2KG");
rb1.setTextSize(10);
rb2.setText("3KG");
rb2.setTextSize(10);
radiogp.addView(rb0);
radiogp.addView(rb1);
radiogp.addView(rb2);
//checkboxContainer.addView(radiogp);
return radiogp;
}

How to get values from dynamic radiogroup android?

Hi i created dynamic radiogroup with radiobutton.Now i want to get values from dynamic radiogroup. Below is my code
final RadioGroup rg = new RadioGroup(this);
rg.setId(questionNo);
RadioButton[] rb = new RadioButton[answers.length()];
for (int i = 0; i < answers.length(); i++) {
JSONObject answerObject = answers.getJSONObject(i);
rb[i] = new RadioButton(this);
rb[i].setText(answerObject.getString("AnswerValue"));
rb[i].setId(answerObject.getInt("AnswerId"));
rg.addView(rb[i]);
}
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
int selectedId = rg.getCheckedRadioButtonId();
Log.i("ID", String.valueOf(selectedId));
}
});
Button click event
submit_button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(rg.getCheckedRadioButtonId()!=-1){
int id= rg.getCheckedRadioButtonId();
View radioButton = rg.findViewById(id);
int radioId = rg.indexOfChild(radioButton);
RadioButton btn = (RadioButton) rg.getChildAt(radioId);
String selection = (String) btn.getText();
Log.i("selection", selection);
}
}
});
I am getting only last index of the radio group.
I know it's too late, But this may help someone else
While creating your Dynamic Radiobutton Group, Create a parent layout(Linear layout or Relative Layout)
All radio groups will be the child of that Layout and all radio button will be a child of the specific radio group.
Creating No of Dynamic Radio Group
Create all Dynamic radio group in a parent layout
Add all radio button to a group
Add all radio group to the parent layout
public RadioButton[] rb;
public RadioGroup grp;
public LinearLayout layoutmain; //Parent layout
for(int i=0;i<qus.size();i++) //No of Questions
{
rb = new RadioButton[size];
grp = new RadioGroup(this);
grp.setId(a+qus.get(i).qusetionId);
for(int j=0;j<qus.get(i).choices.size();j++) //No of Radio button in radio group
{
rb[j] = new RadioButton(this);
rb[j].setText(qus.get(i).choices.get(j).getChoiceText());
grp.addView(rb[j]); // adding button to group
}
layoutmain.addView(grp); // adding group to layout
}
Retrieve All selected button task
From parent, layout get all child
Find all radio group using the child Instance
Create an object for that radio group
using that object get the selected radio button
Button bt = (Button) findViewById(R.id.button);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
for(int i=0;i<layoutmain.getChildCount();i++) //From the parent layout (Linear Layout) get the child
{
View child = layoutmain.getChildAt(i);
if(child instanceof RadioGroup) //Check weather its RadioGroup using its INSTANCE
{
RadioGroup rg = (RadioGroup) child; //create a RadioButton for each group
int selectedId = rg.getCheckedRadioButtonId(); // get the selected button
RadioButton radiochecked = (RadioButton) findViewById(selectedId);
Log.e("Radio",radiochecked.getText().toString()); //from the button get the selected text
}
}
} });

Pass value from radio button to database

I made a layout which contains radio button. I want to pass value of selected radio button to database so how can I get this value.?
Code is given below..Please help me..
private View drawRadioTypeQuestion(Question question, ArrayList<QuestionOption> questionOptions) {
LinearLayout layout = new LinearLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
layout.setOrientation(LinearLayout.VERTICAL);
layout.setPadding(5, 5, 5, 5);
TextView tv = new TextView(this);
tv.setText(question.Question_Order + ". " + question.Question_Title);
tv.setTextSize(16);
// tv.setTypeface(PhoneStatus.getRobotoMediumTypeface(this));
layout.addView(tv);
RadioGroup rg = new RadioGroup(this);
rg.setOrientation(RadioGroup.VERTICAL);
for (QuestionOption opt : questionOptions) {
RadioButton rb = new RadioButton(this);
rb.setText(opt.Question_Answer_Value);
rb.setTextSize(16);
rg.addView(rb);
}
layout.addView(rg);
PhoneStatus.overrideFonts(QuestionListActivity.this, layout);
tv.setTypeface(PhoneStatus.getRobotoMediumTypeface(this));
return layout;
}
try this code
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
RadioButton rb = (RadioButton) findViewById(checkedId);
String value = rb.getText().toString();
}
});
Implement click listener on radio button. On onclick event add value of selected radio button to database..
Visit Here for official document
http://developer.android.com/guide/topics/ui/controls/radiobutton.html
and
http://android-er.blogspot.in/2009/11/radiogroup-and-radiobutton.html

Categories

Resources