Example usage for android.widget RadioGroup indexOfChild

List of usage examples for android.widget RadioGroup indexOfChild

Introduction

In this page you can find the example usage for android.widget RadioGroup indexOfChild.

Prototype

public int indexOfChild(View child) 

Source Link

Document

Returns the position in the group of the specified child view.

Usage

From source file:com.github.johnpersano.supertoasts.demo.fragments.AttributeRadioGroupFragment.java

@Nullable
@Override/*from   ww w  .  j a v a  2s.c  o m*/
@SuppressWarnings("ConstantConditions")
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment_attribute_radiogroup, container, false);

    // Make sure the Fragment has found its arguments
    if (this.getArguments() == null) {
        throw new IllegalArgumentException(
                getClass().getName().concat(" cannot be " + "instantiated without arguments."));
    }

    final TextView subtitleTextView = (TextView) view.findViewById(R.id.subtitle);
    subtitleTextView.setText(getArguments().getString(ARG_SUBTITLE));

    final TextView summaryTextView = (TextView) view.findViewById(R.id.summary);
    summaryTextView.setText(getArguments().getString(ARG_SUMMARY));

    final RadioGroup radioGroup = (RadioGroup) view.findViewById(R.id.radiogroup);
    for (String string : getArguments().getStringArrayList(ARG_ARRAY)) {
        final RadioButton radioButton = new RadioButton(getActivity());
        radioButton.setText(string);
        radioButton.setId(ViewUtils.generateViewId());
        radioGroup.addView(radioButton);
    }
    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            PreferenceManager.getDefaultSharedPreferences(getActivity()).edit()
                    .putInt(getArguments().getString(ARG_TITLE),
                            group.indexOfChild(group.findViewById(group.getCheckedRadioButtonId())))
                    .commit();
        }
    });

    // RadioGroup.check() is misleading, we must check the RadioButton manually
    ((RadioButton) radioGroup.getChildAt(PreferenceManager.getDefaultSharedPreferences(getActivity())
            .getInt(getArguments().getString(ARG_TITLE), 0))).setChecked(true);

    return view;
}

From source file:com.aniruddhc.acemusic.player.WelcomeActivity.GooglePlayMusicFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mContext = getActivity().getApplicationContext();
    mApp = (Common) mContext;/*from ww  w.  j a v a2  s  .  com*/
    View rootView = (View) inflater.inflate(R.layout.fragment_welcome_screen_5, null);

    welcomeHeader = (TextView) rootView.findViewById(R.id.welcome_header);
    welcomeHeader.setTypeface(TypefaceHelper.getTypeface(getActivity(), "Roboto-Light"));

    welcomeText1 = (TextView) rootView.findViewById(R.id.welcome_text_1);
    welcomeText1.setTypeface(TypefaceHelper.getTypeface(getActivity(), "Roboto-Regular"));

    googlePlayMusicDisclaimer = (TextView) rootView.findViewById(R.id.google_play_music_disclaimer);
    googlePlayMusicDisclaimer.setTypeface(TypefaceHelper.getTypeface(getActivity(), "Roboto-Regular"));

    radioGroup = (RadioGroup) rootView.findViewById(R.id.google_play_music_radio_group);

    final AccountManager accountManager = AccountManager.get(getActivity().getApplicationContext());
    final Account[] accounts = accountManager.getAccountsByType("com.google");
    final int size = accounts.length + 1; //We're adding 1 here to account (no pun intended) for the extra "Don't use Google Play Music" option.

    final RadioButton[] radioButton = new RadioButton[size];

    //Add a new radio button the group for each username.
    for (int i = 0; i < size; i++) {
        radioButton[i] = new RadioButton(getActivity());
        radioGroup.addView(radioButton[i]);

        //The first radio button will always be "Don't use Google Play Music".
        if (i == 0) {
            radioButton[i].setChecked(true);
            radioButton[i].setText(R.string.dont_use_google_play_music);
        } else {
            radioButton[i].setText(accounts[i - 1].name);
        }

        radioButton[i].setTag(i);
        radioButton[i].setTypeface(TypefaceHelper.getTypeface(mContext, "Roboto-Regular"));

    }

    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            int radioButtonID = group.getCheckedRadioButtonId();
            View radioButton = group.findViewById(radioButtonID);
            int index = group.indexOfChild(radioButton);

            if (index != 0) {

                account = accounts[index - 1];
                mApp.getSharedPreferences().edit().putString("GOOGLE_PLAY_MUSIC_ACCOUNT", account.name)
                        .commit();

                AsyncGoogleMusicAuthenticationTask task = new AsyncGoogleMusicAuthenticationTask(mContext,
                        getActivity(), true, account.name);

                task.execute();
            } else {
                mApp.getSharedPreferences().edit().putString("GOOGLE_PLAY_MUSIC_ACCOUNT", "").commit();
                mApp.getSharedPreferences().edit().putBoolean("GOOGLE_PLAY_MUSIC_ENABLED", false).commit();
            }

        }

    });

    return rootView;
}

From source file:com.conferenceengineer.android.iosched.ui.SessionFeedbackFragment.java

int getCheckedRadioIndex(RadioGroup rg) {
    int radioId = rg.getCheckedRadioButtonId();
    View rb = rg.findViewById(radioId);
    return rg.indexOfChild(rb);
}

From source file:com.cw.litenote.config.Config.java

void selectStyleDialog(View view) {
    mContext = getActivity();//from  w w w.j a  v a2  s.  c  o m
    AlertDialog.Builder builder = new AlertDialog.Builder(mContext);

    builder.setTitle(R.string.config_set_style_title).setPositiveButton(R.string.btn_OK, listener_ok)
            .setNegativeButton(R.string.btn_Cancel, null);

    // inflate select style layout
    mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = mInflater.inflate(R.layout.select_style, null);
    RadioGroup RG_view = (RadioGroup) view.findViewById(R.id.radioGroup1);

    setButtonColor((RadioButton) RG_view.findViewById(R.id.radio0), 0);
    setButtonColor((RadioButton) RG_view.findViewById(R.id.radio1), 1);
    setButtonColor((RadioButton) RG_view.findViewById(R.id.radio2), 2);
    setButtonColor((RadioButton) RG_view.findViewById(R.id.radio3), 3);
    setButtonColor((RadioButton) RG_view.findViewById(R.id.radio4), 4);
    setButtonColor((RadioButton) RG_view.findViewById(R.id.radio5), 5);
    setButtonColor((RadioButton) RG_view.findViewById(R.id.radio6), 6);
    setButtonColor((RadioButton) RG_view.findViewById(R.id.radio7), 7);
    setButtonColor((RadioButton) RG_view.findViewById(R.id.radio8), 8);
    setButtonColor((RadioButton) RG_view.findViewById(R.id.radio9), 9);

    builder.setView(view);

    RadioGroup radioGroup = (RadioGroup) RG_view.findViewById(R.id.radioGroup1);
    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup RG, int id) {
            mStyle = RG.indexOfChild(RG.findViewById(id));
        }
    });

    dialog = builder.create();
    dialog.show();
}

From source file:com.cw.litenote.folder.FolderUi.java

public static void addNewFolder(final AppCompatActivity act, final int newTableId,
        final SimpleDragSortCursorAdapter folderAdapter) {
    // get folder name
    final String hintFolderName = act.getResources().getString(R.string.default_folder_name)
            .concat(String.valueOf(newTableId));

    // get layout inflater
    View rootView = act.getLayoutInflater().inflate(R.layout.add_new_folder, null);
    final TouchableEditText editFolderName = (TouchableEditText) rootView.findViewById(R.id.new_folder_name);

    // set cursor
    try {/*  w  ww .  j a  v  a 2 s  .  c o m*/
        Field f = TextView.class.getDeclaredField("mCursorDrawableRes");
        f.setAccessible(true);
        f.set(editFolderName, R.drawable.cursor);
    } catch (Exception ignored) {
    }

    // set hint
    editFolderName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                ((EditText) v).setHint(hintFolderName);
            }
        }
    });

    editFolderName.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            ((EditText) v).setText(hintFolderName);
            ((EditText) v).setSelection(hintFolderName.length());
            v.performClick();
            return false;
        }
    });

    // radio buttons
    final RadioGroup mRadioGroup = (RadioGroup) rootView.findViewById(R.id.radioGroup_new_folder_at);

    // get new folder location option
    mPref_add_new_folder_location = act.getSharedPreferences("add_new_folder_option", 0);
    if (mPref_add_new_folder_location.getString("KEY_ADD_NEW_FOLDER_TO", "bottom").equalsIgnoreCase("top")) {
        mRadioGroup.check(mRadioGroup.getChildAt(0).getId());
        mAddFolderAt = 0;
    } else if (mPref_add_new_folder_location.getString("KEY_ADD_NEW_FOLDER_TO", "bottom")
            .equalsIgnoreCase("bottom")) {
        mRadioGroup.check(mRadioGroup.getChildAt(1).getId());
        mAddFolderAt = 1;
    }

    // update new folder location option
    mRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup RG, int id) {
            mAddFolderAt = mRadioGroup.indexOfChild(mRadioGroup.findViewById(id));
            if (mAddFolderAt == 0) {
                mPref_add_new_folder_location.edit().putString("KEY_ADD_NEW_FOLDER_TO", "top").apply();
            } else if (mAddFolderAt == 1) {
                mPref_add_new_folder_location.edit().putString("KEY_ADD_NEW_FOLDER_TO", "bottom").apply();
            }
        }
    });

    // set view to dialog
    Builder builder1 = new Builder(act);
    builder1.setView(rootView);
    final AlertDialog dialog1 = builder1.create();
    dialog1.show();

    // cancel button
    Button btnCancel = (Button) rootView.findViewById(R.id.new_folder_cancel);
    btnCancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog1.dismiss();
        }
    });

    // add button
    Button btnAdd = (Button) rootView.findViewById(R.id.new_folder_add);
    btnAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DB_drawer db_drawer = new DB_drawer(act);

            String folderTitle;
            if (!Util.isEmptyString(editFolderName.getText().toString()))
                folderTitle = editFolderName.getText().toString();
            else
                folderTitle = act.getResources().getString(R.string.default_folder_name)
                        .concat(String.valueOf(newTableId));

            MainAct.mFolderTitles.add(folderTitle);
            // insert new drawer Id and Title
            db_drawer.insertFolder(newTableId, folderTitle, true);

            // insert folder table
            db_drawer.insertFolderTable(newTableId, true);

            // insert initial page table after Add new folder
            if (Define.INITIAL_PAGES_COUNT > 0) {
                for (int i = 1; i <= Define.INITIAL_PAGES_COUNT; i++) {
                    DB_folder dB_folder = new DB_folder(act, newTableId);
                    int style = Util.getNewPageStyle(act);
                    dB_folder.insertPage(DB_folder.getFocusFolder_tableName(), Define.getTabTitle(act, 1), i,
                            style, true);

                    dB_folder.insertPageTable(dB_folder, newTableId, i, true);
                }
            }

            // add new folder to the top
            if (mAddFolderAt == 0) {
                int startCursor = db_drawer.getFoldersCount(true) - 1;
                int endCursor = 0;

                //reorder data base storage for ADD_NEW_TO_TOP option
                int loop = Math.abs(startCursor - endCursor);
                for (int i = 0; i < loop; i++) {
                    swapFolderRows(startCursor, endCursor);
                    if ((startCursor - endCursor) > 0)
                        endCursor++;
                    else
                        endCursor--;
                }

                // update focus folder position
                if (db_drawer.getFoldersCount(true) == 1)
                    setFocus_folderPos(0);
                else
                    setFocus_folderPos(getFocus_folderPos() + 1);

                // update focus folder table Id for Add to top
                Pref.setPref_focusView_folder_tableId(act,
                        db_drawer.getFolderTableId(getFocus_folderPos(), true));

                // update playing highlight if needed
                if (BackgroundAudioService.mMediaPlayer != null)
                    MainAct.mPlaying_folderPos++;
            }

            // recover focus folder table Id
            DB_folder.setFocusFolder_tableId(Pref.getPref_focusView_folder_tableId(act));

            folderAdapter.notifyDataSetChanged();

            //end
            dialog1.dismiss();
            updateFocus_folderPosition();

            MainAct.mAct.invalidateOptionsMenu();
        }
    });
}

From source file:com.tbay.android.FrequentSMS.MainActivity.java

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    switch (item.getItemId()) {
    case R.id.edit:
        //editNote(info.id);
        return true;
    case R.id.show:
        RadioGroup rg = (RadioGroup) findViewById(R.id.whichSMS);
        int rbid = rg.getCheckedRadioButtonId();

        RadioButton rb = (RadioButton) findViewById(rbid);

        int position = rg.indexOfChild(rb);

        Toast.makeText(MainActivity.this, "Telefon nummer: " + Integer.toString(position), Toast.LENGTH_LONG)
                .show();/*from   w w w  . ja  v  a 2s. c  o m*/
        return true;
    default:
        return super.onContextItemSelected(item);
    }
}

From source file:org.openmrs.mobile.activities.formdisplay.FormDisplayPageFragment.java

private void setOnCheckedChangeListener(RadioGroup radioGroup, final SelectOneField radioGroupField) {
    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override/*w  w  w.j  av  a  2 s .com*/
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            View radioButton = radioGroup.findViewById(i);
            int idx = radioGroup.indexOfChild(radioButton);
            radioGroupField.setAnswer(idx);
        }
    });
}

From source file:com.mycodehurts.rapidmath.app.TestActivity.java

public void onBtnClicked(View v) {
    if (v.getId() == R.id.btnNextQuestion) {

        //Store User's answer first
        RadioGroup chkSex = (RadioGroup) findViewById(R.id.groupAnswers);
        Question q = lstQuestions.get(iQuestion);

        q.iUserAnswer = chkSex.indexOfChild(findViewById(chkSex.getCheckedRadioButtonId()));
        Log.i("JS: ", "Answer: " + q.iAnswer + " Selection: " + q.iUserAnswer);

        //Increase counter and check if results can be displayed.
        iQuestion++;//from w  ww .j a  v a2  s . co  m
        if (iQuestion >= 10) {

            timer.cancel();
            timer.purge();

            int iPass = 0;
            String strFailedQ = new String();
            for (int i = 0; i < 10; i++) {
                //Log.i("JS: "," "+lstQuestions.get(i).iUserAnswer + " "+lstQuestions.get(i).iAnswer);
                if (lstQuestions.get(i).iUserAnswer == lstQuestions.get(i).iAnswer) {
                    iPass++;
                } else {
                    if (lstQuestions.get(i).iUserAnswer < 0) {
                        strFailedQ += "\n\n Q" + (i + 1) + ". " + lstQuestions.get(i).strQuestion
                                + "\n Your Answer: " + " " + "\n Correct Answer"
                                + lstQuestions.get(i).strChoices[lstQuestions.get(i).iAnswer];
                    } else {
                        strFailedQ += "\n\n Q" + (i + 1) + ". " + lstQuestions.get(i).strQuestion
                                + "\n Your Answer: "
                                + lstQuestions.get(i).strChoices[lstQuestions.get(i).iUserAnswer]
                                + "\n Correct Answer"
                                + lstQuestions.get(i).strChoices[lstQuestions.get(i).iAnswer];
                    }
                }
            }
            int iPercent = iPass * 100 / 10;
            //Load next activity.
            Intent intent = new Intent(this, ShowResult.class);
            intent.putExtra("iPercent", iPercent);
            intent.putExtra("strFailedQ", strFailedQ);

            //String strTestType = new String();
            /*if(iLevel==0)
            strTestType = "Easy";
            else if(iLevel ==1)
            strTestType = "Medium";
            else
            strTestType = "Hard";
            */
            intent.putExtra("TestType", strTestType);
            startActivity(intent);
            return;
        }

        //Fetch next question.
        NextQuestion();

        //Update UI
        ((TextView) findViewById(R.id.textView2)).setText("Question " + (iQuestion + 1) + " of 10");
        chkSex.clearCheck();

    }
}

From source file:org.odk.collect.android.activities.GeoTraceGoogleMapActivity.java

private void startGeoTrace() {
    RadioGroup rb = (RadioGroup) traceSettingsView.findViewById(R.id.radio_group);
    int radioButtonID = rb.getCheckedRadioButtonId();
    View radioButton = rb.findViewById(radioButtonID);
    int idx = rb.indexOfChild(radioButton);
    beenPaused = true;//from   w ww  .j  a  va2 s  . c om
    TRACE_MODE = idx;
    if (TRACE_MODE == 0) {
        setupManualMode();
    } else if (TRACE_MODE == 1) {
        setupAutomaticMode();
    } else {

    }
    play_button.setVisibility(View.GONE);
    pause_button.setVisibility(View.VISIBLE);

}

From source file:org.odk.collect.android.activities.GeoTraceOsmMapActivity.java

private void startGeoTrace() {
    RadioGroup rb = (RadioGroup) traceSettingsView.findViewById(R.id.radio_group);
    int radioButtonID = rb.getCheckedRadioButtonId();
    View radioButton = rb.findViewById(radioButtonID);
    int idx = rb.indexOfChild(radioButton);
    beenPaused = true;/*from   w ww.  j ava2 s .  c  o m*/
    traceMode = idx;
    if (traceMode == 0) {
        setupManualMode();
    } else if (traceMode == 1) {
        setupAutomaticMode();
    } else {
        reset_trace_settings();
    }
    playButton.setVisibility(View.GONE);
    clearButton.setEnabled(false);
    pauseButton.setVisibility(View.VISIBLE);

}