Question

how can i dynamically update my layout based on what radio button is selected? Currently i have all my code within the oncreate and it seems to only check once to see which radio button is selected, but how can i make it update each time it is changed?

here is my code

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  LinearLayout mainLinear = new LinearLayout(this);
  mainLinear.setOrientation(LinearLayout.VERTICAL);
  LinearLayout ButtonLayout = new LinearLayout(this);
  ButtonLayout.setOrientation(LinearLayout.HORIZONTAL);
  ButtonLayout.setPadding(120, 15, 0, 0);
  mainLinear.addView(ButtonLayout);
  Button deleteSelectedButton = new Button(this);
  deleteSelectedButton.setText("Delete Selected");
  Button backButton = new Button(this);
  backButton.setText("Back");
  ButtonLayout.addView(deleteSelectedButton);
  ButtonLayout.addView(backButton);
  LinearLayout deleteAppointmentLayout = new LinearLayout(this);
  deleteAppointmentLayout.setOrientation(LinearLayout.VERTICAL);
  mainLinear.addView(deleteAppointmentLayout);
  TextView dayLabel = new TextView(this);
  dayLabel.setText("Days");
  dayLabel.setPadding(220, 10, 0, 0);
  dayLabel.setTypeface(Typeface.DEFAULT_BOLD);
  dayLabel.setTextSize(15);
  deleteAppointmentLayout.addView(dayLabel);
  RadioGroup radioGroup = new RadioGroup(this);
  radioGroup.setOrientation(LinearLayout.HORIZONTAL);
  radioGroup.setPadding(10, 0, 0, 0);
  deleteAppointmentLayout.addView(radioGroup);
  RadioButton mondayRadioButton = new RadioButton(this);
  RadioButton tuesdayRadioButton = new RadioButton(this);
  RadioButton wednesdayRadioButton = new RadioButton(this);
  RadioButton thursdayRadioButton = new RadioButton(this);
  RadioButton fridayRadioButton = new RadioButton(this);
  mondayRadioButton.setText("Mon");
  tuesdayRadioButton.setText("Tue");
  wednesdayRadioButton.setText("Wed");
  thursdayRadioButton.setText("Thu");
  fridayRadioButton.setText("Fri");
  mondayRadioButton.setTextSize(12);
  tuesdayRadioButton.setTextSize(12);
  wednesdayRadioButton.setTextSize(12);
  thursdayRadioButton.setTextSize(12);
  fridayRadioButton.setTextSize(12);
  mondayRadioButton.setChecked(true);
  mondayRadioButton.setId(1);
  tuesdayRadioButton.setId(2);
  wednesdayRadioButton.setId(3);
  thursdayRadioButton.setId(4);
  fridayRadioButton.setId(5);
  //OnClickListener radlistener;
  //radioGroup.setOnClickListener(radlistener);
  radioGroup.addView(mondayRadioButton);
  radioGroup.addView(tuesdayRadioButton);
  radioGroup.addView(wednesdayRadioButton);
  radioGroup.addView(thursdayRadioButton);
  radioGroup.addView(fridayRadioButton);
  CheckBox checkBox = new CheckBox(this);
  ScrollView scrollView = new ScrollView(this);
  mainLinear.addView(scrollView);
  LinearLayout deleteAppointmentsLayout = new LinearLayout(this);
  deleteAppointmentsLayout.setOrientation(LinearLayout.VERTICAL);
  scrollView.addView(deleteAppointmentsLayout);
  APData = new AppointmentDataSource(this);
  APData.open();
  //printf("Go to here");
  List < Appointment > appointments = APData.retrieveAllAppointments();
  APData.close();
  String time, duration, description, boxText;
  long id;
  int loop = 0;
  if (mondayRadioButton.isChecked()) {
    deleteAppointmentsLayout.removeAllViews();
    for (Iterator < Appointment > i = appointments.iterator(); i.hasNext();) {
      Appointment item = i.next();
      if (item.getDay() == 1) {
        System.out.println("fucken did work");
        id = item.getId();
        time = item.getTime();
        duration = item.getDuration();
        description = item.getDescription();
        boxText = time + ", " + duration + ", " + description;
        checkBox.setText(boxText);
        checkBox.setId((int) id);
        deleteAppointmentsLayout.addView(checkBox);
      } else {
        System.out.println("fucken didnt work");
      }
    }
  }
  if (tuesdayRadioButton.isChecked()) {
    deleteAppointmentsLayout.removeAllViews();
    for (Iterator < Appointment > i = appointments.iterator(); i.hasNext();) {
      Appointment item = i.next();
      if (item.getDay() == 2) {
        id = item.getId();
        time = item.getTime();
        duration = item.getDuration();
        description = item.getDescription();
        boxText = time + ", " + duration + ", " + description;
        checkBox.setText(boxText);
        checkBox.setId((int) id);
        deleteAppointmentsLayout.addView(checkBox);
      } else {}
    }
  }
  if (wednesdayRadioButton.isChecked()) {
    deleteAppointmentsLayout.removeAllViews();
    for (Iterator < Appointment > i = appointments.iterator(); i.hasNext();) {
      Appointment item = i.next();
      if (item.getDay() == 3) {
        id = item.getId();
        time = item.getTime();
        duration = item.getDuration();
        description = item.getDescription();
        boxText = time + ", " + duration + ", " + description;
        checkBox.setText(boxText);
        checkBox.setId((int) id);
        deleteAppointmentsLayout.addView(checkBox);
      } else {}
    }
  }
  if (thursdayRadioButton.isChecked()) {
    deleteAppointmentsLayout.removeAllViews();
    for (Iterator < Appointment > i = appointments.iterator(); i.hasNext();) {
      Appointment item = i.next();
      if (item.getDay() == 4) {
        id = item.getId();
        time = item.getTime();
        duration = item.getDuration();
        description = item.getDescription();
        boxText = time + ", " + duration + ", " + description;
        checkBox.setText(boxText);
        checkBox.setId((int) id);
        deleteAppointmentsLayout.addView(checkBox);
      } else {}
    }
  }
  if (fridayRadioButton.isChecked()) {
    deleteAppointmentsLayout.removeAllViews();
    for (Iterator < Appointment > i = appointments.iterator(); i.hasNext();) {
      Appointment item = i.next();
      if (item.getDay() == 5) {
        id = item.getId();
        time = item.getTime();
        duration = item.getDuration();
        description = item.getDescription();
        boxText = time + ", " + duration + ", " + description;
        checkBox.setText(boxText);
        checkBox.setId((int) id);
        deleteAppointmentsLayout.addView(checkBox);
      } else {}
    }
  }
  this.setContentView(mainLinear);
  deleteSelectedButton.setOnClickListener(new View.OnClickListener() {@
    Override
    public void onClick(View view) {}
  });
  backButton.setOnClickListener(new View.OnClickListener() {@
    Override
    public void onClick(View view) {
      finish();
    }
  });
}

updated code (wants me to change all variables to FINAL and resulting in time duration description and boxtext to error with "The final local variable description cannot be assigned, since it is defined in an enclosing type" <- only does that when i make checkBox a final aswell)

mondayRadioButton.setOnClickListener(new View.OnClickListener() {

          @Override
          public void onClick(View view) {

              if(mondayRadioButton.isChecked()){
                deleteAppointmentsLayout.removeAllViews();
            for(Iterator<Appointment> i = appointments.iterator(); i.hasNext();){ 
                 Appointment item = i.next();
                    if(item.getDay() == 1){
                        System.out.println("fucken did work");
                        id = item.getId();
                        time = item.getTime();
                        duration = item.getDuration();
                        description = item.getDescription();
                        boxText = time + ", " + duration + ", " + description;
                        checkBox.setText(boxText);
                        checkBox.setId((int) id);
                        deleteAppointmentsLayout.addView(checkBox);

                    }
                    else {
                        System.out.println("fucken didnt work");
                    }
            }
            }
          }

        });
Was it helpful?

Solution

You have write the above code in Oncreate, it will work for the first time when you enter into screen, later it won't, so you need to Execute the above code when an event triggers so

For individual radio buttons set onClickLister

 radiobutton.setOnClickListener(new View.OnClickListener() {

          @Override
          public void onClick(View view) {

                   //your code here
          }

        });

And for the group setOnCheckedChangeListener like this

grp.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChanged(RadioGroup arg0, int arg1) {
                  //your code here

            }
        });

post code inside of the above listeners

You can also use onCheckChangeListener on radiobutton

radiobtn.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                // your code here

            }
        });

Edit:

public class MainActivity extends Activity {

    EditText message, password, username;  // these are called fields
    Context context;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//instantiate like this
        username = new EditText(this);
        password = (EditText) findViewById(R.id.editText2);
        message = (EditText) findViewById(R.id.editText3);
}
}

OTHER TIPS

You could try to implement an "onCheckedChanged()" on your RadioGroup, then updating information in your layout, or even inflating different layout based on checked radio.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top