List of usage examples for android.widget RadioGroup RadioGroup
public RadioGroup(Context context)
From source file:org.openmrs.mobile.activities.formdisplay.FormDisplayPageFragment.java
@Override public void createAndAttachSelectQuestionRadioButton(Question question, LinearLayout sectionLinearLayout) { TextView textView = new TextView(getActivity()); textView.setPadding(20, 0, 0, 0);// w w w .j a va2 s . c om textView.setText(question.getLabel()); RadioGroup radioGroup = new RadioGroup(getActivity()); for (Answer answer : question.getQuestionOptions().getAnswers()) { RadioButton radioButton = new RadioButton(getActivity()); radioButton.setText(answer.getLabel()); radioGroup.addView(radioButton); } SelectOneField radioGroupField = new SelectOneField(question.getQuestionOptions().getAnswers(), question.getQuestionOptions().getConcept()); LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); sectionLinearLayout.addView(textView); sectionLinearLayout.addView(radioGroup); sectionLinearLayout.setLayoutParams(linearLayoutParams); SelectOneField selectOneField = getSelectOneField(radioGroupField.getConcept()); if (selectOneField != null) { if (selectOneField.getChosenAnswerPosition() != -1) { RadioButton radioButton = (RadioButton) radioGroup .getChildAt(selectOneField.getChosenAnswerPosition()); radioButton.setChecked(true); } setOnCheckedChangeListener(radioGroup, selectOneField); } else { setOnCheckedChangeListener(radioGroup, radioGroupField); selectOneFields.add(radioGroupField); } }
From source file:de.da_sense.moses.client.FormFragment.java
/** * Displays a single choice question to the user. * @param question the question to be displayed * @param linearLayoutInsideAScrollView the view to add the question to * @param ordinal the ordinal number of the question i.e. 1, 2, 3, 4 or 5 *//* w ww.j av a 2 s . c o m*/ private void makeSingleChoice(final Question question, LinearLayout linearLayoutInsideAScrollView, int ordinal) { LinearLayout questionContainer = generateQuestionContainer(linearLayoutInsideAScrollView); String questionText = question.getTitle(); List<PossibleAnswer> possibleAnswers = question.getPossibleAnswers(); Collections.sort(possibleAnswers); TextView questionView = new TextView(getActivity()); questionView.setText(ordinal + ". " + questionText); if (question.isMandatory()) questionView.setTextAppearance(getActivity(), R.style.QuestionTextStyleMandatory); else questionView.setTextAppearance(getActivity(), R.style.QuestionTextStyle); questionContainer.addView(questionView); mQuestionTitleMappings.put(question, questionView); final RadioButton[] rb = new RadioButton[possibleAnswers.size()]; RadioGroup rg = new RadioGroup(getActivity()); // create the RadioGroup rg.setOrientation(RadioGroup.VERTICAL);// or RadioGroup.VERTICAL String madeAnswer = question.getAnswer(); int madeAnswerInt = -1; if (!madeAnswer.equals(Question.ANSWER_UNANSWERED)) madeAnswerInt = Integer.parseInt(madeAnswer); for (int i = 0; i < rb.length; i++) { rb[i] = new RadioButton(getActivity()); if (i % 2 == 0) rb[i].setBackgroundColor(getActivity().getResources().getColor(R.color.light_gray)); rg.addView(rb[i]); // the RadioButtons are added to the radioGroup // instead of the layout PossibleAnswer possibleAnswer = possibleAnswers.get(i); rb[i].setText(possibleAnswer.getTitle()); final int possibleAnswerId = possibleAnswer.getId(); if (madeAnswerInt == possibleAnswerId) rb[i].setChecked(true); rb[i].setTextAppearance(getActivity(), R.style.PossibleAnswerTextStyle); LayoutParams rowParam = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); rb[i].setLayoutParams(rowParam); // click handling rb[i].setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { question.setAnswer(String.valueOf(possibleAnswerId)); } }); if (mBelongsTo == WelcomeActivityPagerAdapter.TAB_HISTORY) rb[i].setEnabled(false); rb[i].setVisibility(View.VISIBLE); } rg.setVisibility(View.VISIBLE); if (mBelongsTo == WelcomeActivityPagerAdapter.TAB_HISTORY) rg.setEnabled(false); Log.i(LOG_TAG, "last rg = " + rg); questionContainer.addView(rg); }
From source file:usbong.android.retrocc.UsbongDecisionTreeEngineActivity.java
@Override public boolean onOptionsItemSelected(MenuItem item) { if (mTts.isSpeaking()) { mTts.stop();//from w w w . j a v a2 s. c om } StringBuffer sb = new StringBuffer(); switch (item.getItemId()) { /* case(R.id.set_language): initSetLanguage(); //refresh the menu options supportInvalidateOptionsMenu(); //added by Mike, 20160507 invalidateOptionsMenu(); return true; case(R.id.speak): processSpeak(sb); return true; case(R.id.settings): //20160417 //Reference: http://stackoverflow.com/questions/16954196/alertdialog-with-checkbox-in-android; //last accessed: 20160408; answer by: kamal; edited by: Empty2K12 final CharSequence[] items = {UsbongConstants.AUTO_NARRATE_STRING, UsbongConstants.AUTO_PLAY_STRING, UsbongConstants.AUTO_LOOP_STRING}; // arraylist to keep the selected items selectedSettingsItems=new ArrayList<Integer>(); //check saved settings if (UsbongUtils.IS_IN_AUTO_NARRATE_MODE) { selectedSettingsItems.add(UsbongConstants.AUTO_NARRATE); } if (UsbongUtils.IS_IN_AUTO_PLAY_MODE) { selectedSettingsItems.add(UsbongConstants.AUTO_PLAY); selectedSettingsItems.add(UsbongConstants.AUTO_NARRATE); //if AUTO_PLAY is checked, AUTO_NARRATE should also be checked } if (UsbongUtils.IS_IN_AUTO_LOOP_MODE) { selectedSettingsItems.add(UsbongConstants.AUTO_LOOP); } selectedSettingsItemsInBoolean = new boolean[items.length]; for(int k=0; k<items.length; k++) { selectedSettingsItemsInBoolean[k] = false; } for(int i=0; i<selectedSettingsItems.size(); i++) { selectedSettingsItemsInBoolean[selectedSettingsItems.get(i)] = true; } inAppSettingsDialog = new AlertDialog.Builder(this) .setTitle("Settings") .setMultiChoiceItems(items, selectedSettingsItemsInBoolean, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int indexSelected, boolean isChecked) { Log.d(">>>","onClick"); if (isChecked) { // If the user checked the item, add it to the selected items selectedSettingsItems.add(indexSelected); if ((indexSelected==UsbongConstants.AUTO_PLAY) && !selectedSettingsItems.contains(UsbongConstants.AUTO_NARRATE)) { final ListView list = inAppSettingsDialog.getListView(); list.setItemChecked(UsbongConstants.AUTO_NARRATE, true); } } else if (selectedSettingsItems.contains(indexSelected)) { if ((indexSelected==UsbongConstants.AUTO_NARRATE) && selectedSettingsItems.contains(UsbongConstants.AUTO_PLAY)) { final ListView list = inAppSettingsDialog.getListView(); list.setItemChecked(indexSelected, false); } else { // Else, if the item is already in the array, remove it selectedSettingsItems.remove(Integer.valueOf(indexSelected)); } } //updated selectedSettingsItemsInBoolean for(int k=0; k<items.length; k++) { selectedSettingsItemsInBoolean[k] = false; } for(int i=0; i<selectedSettingsItems.size(); i++) { selectedSettingsItemsInBoolean[selectedSettingsItems.get(i)] = true; } } }).setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { try { InputStreamReader reader = UsbongUtils.getFileFromSDCardAsReader(UsbongUtils.BASE_FILE_PATH + "usbong.config"); BufferedReader br = new BufferedReader(reader); String currLineString; //write first to a temporary file PrintWriter out = UsbongUtils.getFileFromSDCardAsWriter(UsbongUtils.BASE_FILE_PATH + "usbong.config" +"TEMP"); while((currLineString=br.readLine())!=null) { Log.d(">>>", "currLineString: "+currLineString); if ((currLineString.contains("IS_IN_AUTO_NARRATE_MODE=")) || (currLineString.contains("IS_IN_AUTO_PLAY_MODE=")) || (currLineString.contains("IS_IN_AUTO_LOOP_MODE="))) { continue; } else { out.println(currLineString); } } for (int i=0; i<items.length; i++) { Log.d(">>>>", i+""); if (selectedSettingsItemsInBoolean[i]==true) { if (i==UsbongConstants.AUTO_NARRATE) { out.println("IS_IN_AUTO_NARRATE_MODE=ON"); UsbongUtils.IS_IN_AUTO_NARRATE_MODE=true; } else if (i==UsbongConstants.AUTO_PLAY) { out.println("IS_IN_AUTO_PLAY_MODE=ON"); UsbongUtils.IS_IN_AUTO_PLAY_MODE=true; } else if (i==UsbongConstants.AUTO_LOOP) { out.println("IS_IN_AUTO_LOOP_MODE=ON"); UsbongUtils.IS_IN_AUTO_LOOP_MODE=true; } } else { if (i==UsbongConstants.AUTO_NARRATE) { out.println("IS_IN_AUTO_NARRATE_MODE=OFF"); UsbongUtils.IS_IN_AUTO_NARRATE_MODE=false; } else if (i==UsbongConstants.AUTO_PLAY) { out.println("IS_IN_AUTO_PLAY_MODE=OFF"); UsbongUtils.IS_IN_AUTO_PLAY_MODE=false; } else if (i==UsbongConstants.AUTO_LOOP) { out.println("IS_IN_AUTO_LOOP_MODE=OFF"); UsbongUtils.IS_IN_AUTO_LOOP_MODE=false; } } } out.close(); //remember to close //copy temp file to actual usbong.config file InputStreamReader reader2 = UsbongUtils.getFileFromSDCardAsReader(UsbongUtils.BASE_FILE_PATH + "usbong.config"+"TEMP"); BufferedReader br2 = new BufferedReader(reader2); String currLineString2; //write to actual usbong.config file PrintWriter out2 = UsbongUtils.getFileFromSDCardAsWriter(UsbongUtils.BASE_FILE_PATH + "usbong.config"); while((currLineString2=br2.readLine())!=null) { out2.println(currLineString2); } out2.close(); UsbongUtils.deleteRecursive(new File(UsbongUtils.BASE_FILE_PATH + "usbong.config"+"TEMP")); } catch(Exception e) { e.printStackTrace(); } } }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { // Your code when user clicked on Cancel } }).create(); inAppSettingsDialog.show(); return true; */ case (R.id.about): new AlertDialog.Builder(UsbongDecisionTreeEngineActivity.this).setTitle("About") .setMessage(UsbongUtils.readTextFileInAssetsFolder(UsbongDecisionTreeEngineActivity.this, "credits.txt")) //don't add a '/', otherwise the file would not be found .setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }).show(); return true; case (R.id.account): final EditText firstName = new EditText(this); firstName.setHint("First Name"); final EditText surName = new EditText(this); surName.setHint("Surname"); final EditText contactNumber = new EditText(this); contactNumber.setHint("Contact Number"); contactNumber.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL); //added by Mike, 20170223 final RadioGroup preference = new RadioGroup(this); preference.setOrientation(RadioGroup.HORIZONTAL); RadioButton meetup = new AppCompatRadioButton(this); meetup.setText("Meet-up"); preference.addView(meetup); RadioButton shipping = new AppCompatRadioButton(this); shipping.setText("Shipping"); preference.addView(shipping); final EditText shippingAddress = new EditText(this); shippingAddress.setHint("Shipping Address"); shippingAddress.setMinLines(5); //added by Mike, 20170223 final RadioGroup modeOfPayment = new RadioGroup(this); modeOfPayment.setOrientation(RadioGroup.VERTICAL); RadioButton cashUponMeetup = new AppCompatRadioButton(this); cashUponMeetup.setText("Cash upon meet-up"); modeOfPayment.addView(cashUponMeetup); RadioButton bankDeposit = new AppCompatRadioButton(this); bankDeposit.setText("Bank Deposit"); modeOfPayment.addView(bankDeposit); RadioButton peraPadala = new AppCompatRadioButton(this); peraPadala.setText("Pera Padala"); modeOfPayment.addView(peraPadala); //Reference: http://stackoverflow.com/questions/23024831/android-shared-preferences-example //; last accessed: 20150609 //answer by Elenasys //added by Mike, 20150207 SharedPreferences prefs = getSharedPreferences(UsbongConstants.MY_ACCOUNT_DETAILS, MODE_PRIVATE); if (prefs != null) { firstName.setText(prefs.getString("firstName", ""));//"" is the default value. surName.setText(prefs.getString("surname", "")); //"" is the default value. contactNumber.setText(prefs.getString("contactNumber", "")); //"" is the default value. //added by Mike, 20170223 ((RadioButton) preference.getChildAt(prefs.getInt("preference", UsbongConstants.defaultPreference))) .setChecked(true); shippingAddress.setText(prefs.getString("shippingAddress", "")); //"" is the default value. //added by Mike, 20170223 ((RadioButton) modeOfPayment .getChildAt(prefs.getInt("modeOfPayment", UsbongConstants.defaultModeOfPayment))) .setChecked(true); } LinearLayout ll = new LinearLayout(this); ll.setOrientation(LinearLayout.VERTICAL); ll.addView(firstName); ll.addView(surName); ll.addView(contactNumber); ll.addView(preference); ll.addView(shippingAddress); ll.addView(modeOfPayment); new AlertDialog.Builder(this).setTitle("My Account").setView(ll) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { //ACTION } }).setPositiveButton("Save & Exit", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { //ACTION //Reference: http://stackoverflow.com/questions/23024831/android-shared-preferences-example //; last accessed: 20150609 //answer by Elenasys //added by Mike, 20170207 SharedPreferences.Editor editor = getSharedPreferences( UsbongConstants.MY_ACCOUNT_DETAILS, MODE_PRIVATE).edit(); editor.putString("firstName", firstName.getText().toString()); editor.putString("surname", surName.getText().toString()); editor.putString("contactNumber", contactNumber.getText().toString()); for (int i = 0; i < preference.getChildCount(); i++) { if (((RadioButton) preference.getChildAt(i)).isChecked()) { currPreference = i; } } editor.putInt("preference", currPreference); //added by Mike, 20170223 editor.putString("shippingAddress", shippingAddress.getText().toString()); for (int i = 0; i < modeOfPayment.getChildCount(); i++) { if (((RadioButton) modeOfPayment.getChildAt(i)).isChecked()) { currModeOfPayment = i; } } editor.putInt("modeOfPayment", currModeOfPayment); //added by Mike, 20170223 editor.commit(); } }).show(); return true; case android.R.id.home: //added by Mike, 22 Sept. 2015 /*//commented out by Mike, 201702014; UsbongDecisionTreeEngineActivity is already the main menu processReturnToMainMenuActivity(); */ return true; default: return super.onOptionsItemSelected(item); } }