Example usage for android.widget RatingBar getRating

List of usage examples for android.widget RatingBar getRating

Introduction

In this page you can find the example usage for android.widget RatingBar getRating.

Prototype

public float getRating() 

Source Link

Document

Gets the current rating (number of stars filled).

Usage

From source file:com.aware.ui.ESM_UI.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    //      getActivity().getWindow().setType(WindowManager.LayoutParams.TYPE_PRIORITY_PHONE);
    //      getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    //        getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
    //        getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

    builder = new AlertDialog.Builder(getActivity());
    inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inputManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);

    TAG = Aware.getSetting(getActivity().getApplicationContext(), Aware_Preferences.DEBUG_TAG).length() > 0
            ? Aware.getSetting(getActivity().getApplicationContext(), Aware_Preferences.DEBUG_TAG)
            : TAG;/*from w  w  w. j  av a  2s  .  c  om*/

    Cursor visible_esm = getActivity().getContentResolver().query(ESM_Data.CONTENT_URI, null,
            ESM_Data.STATUS + "=" + ESM.STATUS_NEW, null, ESM_Data.TIMESTAMP + " ASC LIMIT 1");
    if (visible_esm != null && visible_esm.moveToFirst()) {
        esm_id = visible_esm.getInt(visible_esm.getColumnIndex(ESM_Data._ID));

        //Fixed: set the esm as not new anymore, to avoid displaying the same ESM twice due to changes in orientation
        ContentValues update_state = new ContentValues();
        update_state.put(ESM_Data.STATUS, ESM.STATUS_VISIBLE);
        getActivity().getContentResolver().update(ESM_Data.CONTENT_URI, update_state,
                ESM_Data._ID + "=" + esm_id, null);

        esm_type = visible_esm.getInt(visible_esm.getColumnIndex(ESM_Data.TYPE));
        expires_seconds = visible_esm.getInt(visible_esm.getColumnIndex(ESM_Data.EXPIRATION_THREASHOLD));

        builder.setTitle(visible_esm.getString(visible_esm.getColumnIndex(ESM_Data.TITLE)));

        View ui = null;
        switch (esm_type) {
        case ESM.TYPE_ESM_TEXT:
            ui = inflater.inflate(R.layout.esm_text, null);
            break;
        case ESM.TYPE_ESM_RADIO:
            ui = inflater.inflate(R.layout.esm_radio, null);
            break;
        case ESM.TYPE_ESM_CHECKBOX:
            ui = inflater.inflate(R.layout.esm_checkbox, null);
            break;
        case ESM.TYPE_ESM_LIKERT:
            ui = inflater.inflate(R.layout.esm_likert, null);
            break;
        case ESM.TYPE_ESM_QUICK_ANSWERS:
            ui = inflater.inflate(R.layout.esm_quick, null);
            break;
        }

        final View layout = ui;
        builder.setView(layout);
        current_dialog = builder.create();
        sContext = current_dialog.getContext();

        TextView esm_instructions = (TextView) layout.findViewById(R.id.esm_instructions);
        esm_instructions.setText(visible_esm.getString(visible_esm.getColumnIndex(ESM_Data.INSTRUCTIONS)));

        switch (esm_type) {
        case ESM.TYPE_ESM_TEXT:
            final EditText feedback = (EditText) layout.findViewById(R.id.esm_feedback);
            Button cancel_text = (Button) layout.findViewById(R.id.esm_cancel);
            cancel_text.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    inputManager.hideSoftInputFromWindow(feedback.getWindowToken(), 0);
                    current_dialog.cancel();
                }
            });
            Button submit_text = (Button) layout.findViewById(R.id.esm_submit);
            submit_text.setText(visible_esm.getString(visible_esm.getColumnIndex(ESM_Data.SUBMIT)));
            submit_text.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    inputManager.hideSoftInputFromWindow(feedback.getWindowToken(), 0);

                    if (expires_seconds > 0 && expire_monitor != null)
                        expire_monitor.cancel(true);

                    ContentValues rowData = new ContentValues();
                    rowData.put(ESM_Data.ANSWER_TIMESTAMP, System.currentTimeMillis());
                    rowData.put(ESM_Data.ANSWER, feedback.getText().toString());
                    rowData.put(ESM_Data.STATUS, ESM.STATUS_ANSWERED);

                    sContext.getContentResolver().update(ESM_Data.CONTENT_URI, rowData,
                            ESM_Data._ID + "=" + esm_id, null);

                    Intent answer = new Intent(ESM.ACTION_AWARE_ESM_ANSWERED);
                    getActivity().sendBroadcast(answer);

                    if (Aware.DEBUG)
                        Log.d(TAG, "Answer:" + rowData.toString());

                    current_dialog.dismiss();
                }
            });
            break;
        case ESM.TYPE_ESM_RADIO:
            try {
                final RadioGroup radioOptions = (RadioGroup) layout.findViewById(R.id.esm_radio);
                final JSONArray radios = new JSONArray(
                        visible_esm.getString(visible_esm.getColumnIndex(ESM_Data.RADIOS)));

                for (int i = 0; i < radios.length(); i++) {
                    final RadioButton radioOption = new RadioButton(getActivity());
                    radioOption.setId(i);
                    radioOption.setText(radios.getString(i));
                    radioOptions.addView(radioOption);

                    if (radios.getString(i).equals("Other")) {
                        radioOption.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                final Dialog editOther = new Dialog(getActivity());
                                editOther.setTitle("Can you be more specific, please?");
                                editOther.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
                                editOther.getWindow().setGravity(Gravity.TOP);
                                editOther.getWindow().setLayout(LayoutParams.MATCH_PARENT,
                                        LayoutParams.WRAP_CONTENT);

                                LinearLayout editor = new LinearLayout(getActivity());
                                editor.setOrientation(LinearLayout.VERTICAL);

                                editOther.setContentView(editor);
                                editOther.show();

                                final EditText otherText = new EditText(getActivity());
                                editor.addView(otherText);

                                Button confirm = new Button(getActivity());
                                confirm.setText("OK");
                                confirm.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        if (otherText.length() > 0)
                                            radioOption.setText(otherText.getText());
                                        inputManager.hideSoftInputFromWindow(otherText.getWindowToken(), 0);
                                        editOther.dismiss();
                                    }
                                });
                                editor.addView(confirm);
                            }
                        });
                    }
                }
                Button cancel_radio = (Button) layout.findViewById(R.id.esm_cancel);
                cancel_radio.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        current_dialog.cancel();
                    }
                });
                Button submit_radio = (Button) layout.findViewById(R.id.esm_submit);
                submit_radio.setText(visible_esm.getString(visible_esm.getColumnIndex(ESM_Data.SUBMIT)));
                submit_radio.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        if (expires_seconds > 0 && expire_monitor != null)
                            expire_monitor.cancel(true);

                        ContentValues rowData = new ContentValues();
                        rowData.put(ESM_Data.ANSWER_TIMESTAMP, System.currentTimeMillis());

                        RadioGroup radioOptions = (RadioGroup) layout.findViewById(R.id.esm_radio);
                        if (radioOptions.getCheckedRadioButtonId() != -1) {
                            RadioButton selected = (RadioButton) radioOptions
                                    .getChildAt(radioOptions.getCheckedRadioButtonId());
                            rowData.put(ESM_Data.ANSWER, selected.getText().toString());
                        }
                        rowData.put(ESM_Data.STATUS, ESM.STATUS_ANSWERED);

                        sContext.getContentResolver().update(ESM_Data.CONTENT_URI, rowData,
                                ESM_Data._ID + "=" + esm_id, null);

                        Intent answer = new Intent(ESM.ACTION_AWARE_ESM_ANSWERED);
                        getActivity().sendBroadcast(answer);

                        if (Aware.DEBUG)
                            Log.d(TAG, "Answer:" + rowData.toString());

                        current_dialog.dismiss();
                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }
            break;
        case ESM.TYPE_ESM_CHECKBOX:
            try {
                final LinearLayout checkboxes = (LinearLayout) layout.findViewById(R.id.esm_checkboxes);
                final JSONArray checks = new JSONArray(
                        visible_esm.getString(visible_esm.getColumnIndex(ESM_Data.CHECKBOXES)));

                for (int i = 0; i < checks.length(); i++) {
                    final CheckBox checked = new CheckBox(getActivity());
                    checked.setText(checks.getString(i));
                    checked.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(final CompoundButton buttonView, boolean isChecked) {
                            if (isChecked) {
                                if (buttonView.getText().equals("Other")) {
                                    checked.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            final Dialog editOther = new Dialog(getActivity());
                                            editOther.setTitle("Can you be more specific, please?");
                                            editOther.getWindow()
                                                    .setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
                                            editOther.getWindow().setGravity(Gravity.TOP);
                                            editOther.getWindow().setLayout(LayoutParams.MATCH_PARENT,
                                                    LayoutParams.WRAP_CONTENT);

                                            LinearLayout editor = new LinearLayout(getActivity());
                                            editor.setOrientation(LinearLayout.VERTICAL);
                                            editOther.setContentView(editor);
                                            editOther.show();

                                            final EditText otherText = new EditText(getActivity());
                                            editor.addView(otherText);

                                            Button confirm = new Button(getActivity());
                                            confirm.setText("OK");
                                            confirm.setOnClickListener(new View.OnClickListener() {
                                                @Override
                                                public void onClick(View v) {
                                                    if (otherText.length() > 0) {
                                                        inputManager.hideSoftInputFromWindow(
                                                                otherText.getWindowToken(), 0);
                                                        selected_options
                                                                .remove(buttonView.getText().toString());
                                                        checked.setText(otherText.getText());
                                                        selected_options.add(otherText.getText().toString());
                                                    }
                                                    editOther.dismiss();
                                                }
                                            });
                                            editor.addView(confirm);
                                        }
                                    });
                                } else {
                                    selected_options.add(buttonView.getText().toString());
                                }
                            } else {
                                selected_options.remove(buttonView.getText().toString());
                            }
                        }
                    });
                    checkboxes.addView(checked);
                }
                Button cancel_checkbox = (Button) layout.findViewById(R.id.esm_cancel);
                cancel_checkbox.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        current_dialog.cancel();
                    }
                });
                Button submit_checkbox = (Button) layout.findViewById(R.id.esm_submit);
                submit_checkbox.setText(visible_esm.getString(visible_esm.getColumnIndex(ESM_Data.SUBMIT)));
                submit_checkbox.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        if (expires_seconds > 0 && expire_monitor != null)
                            expire_monitor.cancel(true);

                        ContentValues rowData = new ContentValues();
                        rowData.put(ESM_Data.ANSWER_TIMESTAMP, System.currentTimeMillis());

                        if (selected_options.size() > 0) {
                            rowData.put(ESM_Data.ANSWER, selected_options.toString());
                        }

                        rowData.put(ESM_Data.STATUS, ESM.STATUS_ANSWERED);

                        sContext.getContentResolver().update(ESM_Data.CONTENT_URI, rowData,
                                ESM_Data._ID + "=" + esm_id, null);

                        Intent answer = new Intent(ESM.ACTION_AWARE_ESM_ANSWERED);
                        getActivity().sendBroadcast(answer);

                        if (Aware.DEBUG)
                            Log.d(TAG, "Answer:" + rowData.toString());

                        current_dialog.dismiss();
                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }
            break;
        case ESM.TYPE_ESM_LIKERT:
            final RatingBar ratingBar = (RatingBar) layout.findViewById(R.id.esm_likert);
            ratingBar.setMax(visible_esm.getInt(visible_esm.getColumnIndex(ESM_Data.LIKERT_MAX)));
            ratingBar.setStepSize(
                    (float) visible_esm.getDouble(visible_esm.getColumnIndex(ESM_Data.LIKERT_STEP)));
            ratingBar.setNumStars(visible_esm.getInt(visible_esm.getColumnIndex(ESM_Data.LIKERT_MAX)));

            TextView min_label = (TextView) layout.findViewById(R.id.esm_min);
            min_label.setText(visible_esm.getString(visible_esm.getColumnIndex(ESM_Data.LIKERT_MIN_LABEL)));

            TextView max_label = (TextView) layout.findViewById(R.id.esm_max);
            max_label.setText(visible_esm.getString(visible_esm.getColumnIndex(ESM_Data.LIKERT_MAX_LABEL)));

            Button cancel = (Button) layout.findViewById(R.id.esm_cancel);
            cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    current_dialog.cancel();
                }
            });
            Button submit = (Button) layout.findViewById(R.id.esm_submit);
            submit.setText(visible_esm.getString(visible_esm.getColumnIndex(ESM_Data.SUBMIT)));
            submit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (expires_seconds > 0 && expire_monitor != null)
                        expire_monitor.cancel(true);

                    ContentValues rowData = new ContentValues();
                    rowData.put(ESM_Data.ANSWER_TIMESTAMP, System.currentTimeMillis());
                    rowData.put(ESM_Data.ANSWER, ratingBar.getRating());
                    rowData.put(ESM_Data.STATUS, ESM.STATUS_ANSWERED);

                    sContext.getContentResolver().update(ESM_Data.CONTENT_URI, rowData,
                            ESM_Data._ID + "=" + esm_id, null);

                    Intent answer = new Intent(ESM.ACTION_AWARE_ESM_ANSWERED);
                    getActivity().sendBroadcast(answer);

                    if (Aware.DEBUG)
                        Log.d(TAG, "Answer:" + rowData.toString());

                    current_dialog.dismiss();
                }
            });
            break;
        case ESM.TYPE_ESM_QUICK_ANSWERS:
            try {
                final JSONArray answers = new JSONArray(
                        visible_esm.getString(visible_esm.getColumnIndex(ESM_Data.QUICK_ANSWERS)));
                final LinearLayout answersHolder = (LinearLayout) layout.findViewById(R.id.esm_answers);

                //If we have more than 3 possibilities, better that the UI is vertical for UX
                if (answers.length() > 3) {
                    answersHolder.setOrientation(LinearLayout.VERTICAL);
                }

                for (int i = 0; i < answers.length(); i++) {
                    final Button answer = new Button(getActivity());
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                            LayoutParams.WRAP_CONTENT, 1.0f);
                    answer.setLayoutParams(params);
                    answer.setText(answers.getString(i));
                    answer.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            if (expires_seconds > 0 && expire_monitor != null)
                                expire_monitor.cancel(true);

                            ContentValues rowData = new ContentValues();
                            rowData.put(ESM_Data.ANSWER_TIMESTAMP, System.currentTimeMillis());
                            rowData.put(ESM_Data.STATUS, ESM.STATUS_ANSWERED);
                            rowData.put(ESM_Data.ANSWER, (String) answer.getText());

                            sContext.getContentResolver().update(ESM_Data.CONTENT_URI, rowData,
                                    ESM_Data._ID + "=" + esm_id, null);

                            Intent answer = new Intent(ESM.ACTION_AWARE_ESM_ANSWERED);
                            getActivity().sendBroadcast(answer);

                            if (Aware.DEBUG)
                                Log.d(TAG, "Answer:" + rowData.toString());

                            current_dialog.dismiss();
                        }
                    });
                    answersHolder.addView(answer);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            break;
        }
    }
    if (visible_esm != null && !visible_esm.isClosed())
        visible_esm.close();

    //Start dialog visibility threshold
    if (expires_seconds > 0) {
        expire_monitor = new ESMExpireMonitor(System.currentTimeMillis(), expires_seconds, esm_id);
        expire_monitor.execute();
    }

    //Fixed: doesn't dismiss the dialog if touched outside or ghost touches
    current_dialog.setCanceledOnTouchOutside(false);

    return current_dialog;
}

From source file:com.example.android.ennis.barrett.popularmovies.DetailFragment.java

/**
 * Sets all the views to related id.// www .  j a v  a 2  s  .  co m
 * @param _id The id of the movie
 */
private void setDetails(long _id) {
    /*
     * get references
     */
    TextView title = (TextView) mRootView.findViewById(R.id.original_title);
    TextView overview = (TextView) mRootView.findViewById(R.id.overview);
    TextView date = (TextView) mRootView.findViewById(R.id.date);
    TextView voteAverage2 = (TextView) mRootView.findViewById(R.id.vote_average2);
    ImageView poster = (ImageView) mRootView.findViewById(R.id.poster);
    RatingBar voteAverage = (RatingBar) mRootView.findViewById(R.id.vote_average);
    LinearLayout videos = (LinearLayout) mRootView.findViewById(R.id.videos);
    LinearLayout reviews = (LinearLayout) mRootView.findViewById(R.id.reviews);
    CompoundButton isFavorite = (CompoundButton) mRootView.findViewById(R.id.favorite);

    ContentResolver contentResolver = getActivity().getContentResolver();

    /*
     * Queries the movies table
     */
    Cursor cursor = contentResolver.query(TMDbContract.Movies.URI, null, TMDbContract.Movies.ID + " = ?",
            new String[] { mID + "" }, null);
    cursor.moveToFirst();

    /*
     * sets most views to the movie
     */
    title.setText(cursor.getString(cursor.getColumnIndex(TMDbContract.Movies.ORIGINAL_TITLE)));

    overview.setText(cursor.getString(cursor.getColumnIndex(TMDbContract.Movies.OVERVIEW)));

    date.setText(cursor.getString(cursor.getColumnIndex(TMDbContract.Movies.RELEASE_DATE)));

    // Setups up the poster
    String posterURLString = "http://image.tmdb.org/t/p/w185/"
            + cursor.getString(cursor.getColumnIndex(TMDbContract.Movies.POSTER));
    Log.v(TAG, posterURLString);
    Picasso.with(getActivity()).load(posterURLString).into(poster);

    String bool = cursor.getString(cursor.getColumnIndex(TMDbContract.Movies.IS_FAVORITE));

    isFavorite.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ContentValues value = new ContentValues();
            String isFavorite = "0";
            if (((CompoundButton) v).isChecked()) {
                isFavorite = "1";
            }

            value.put(TMDbContract.Movies.IS_FAVORITE, isFavorite);
            int num = getActivity().getContentResolver().update(TMDbContract.Movies.URI, value,
                    TMDbContract.Movies._ID + " = ?", new String[] { Long.toString(mID) });
        }
    });

    //short circuit logic stops app from crashing..So don't reverse the expression
    if (bool != null && bool.equals("1")) {
        isFavorite.setChecked(true);
    } else {
        isFavorite.setChecked(false);
    }

    //Set up the RatingBar and the TextView with the rating
    float vote = cursor.getFloat(cursor.getColumnIndex(TMDbContract.Movies.VOTE_AVERAGE));
    voteAverage2.setText(vote + " / 10 ");
    vote /= 2;
    Log.v(TAG, vote + "");
    voteAverage.setRating(vote);
    Log.v(TAG, voteAverage.getRating() + "");

    /*
     * Set up the videos LinearLayout.
     * Queries the table and then creates TextViews to display the results
     */
    Cursor cursorVideos = contentResolver.query(TMDbContract.Videos.URI, null,
            TMDbContract.Videos.MOVIE_IDS + " = ?",
            new String[] { cursor.getString(cursor.getColumnIndex(TMDbContract.Movies.MOVIE_ID)) }, null);
    VideoCursorAdapter adapter = new VideoCursorAdapter(getActivity(), R.layout.video_card, cursorVideos);

    //loop to create TextViews to display the results
    for (int i = 0; i < adapter.getCount(); i++) {
        View view = adapter.getView(i, null, null);
        view.setOnClickListener(this);
        videos.addView(view);
    }

    /*
     * Set up the reviews LinearLayout.
     * Queries the table and then creates TextViews to display the results
     */
    Cursor cursorReviews = contentResolver.query(TMDbContract.Reviews.URI, null,
            TMDbContract.Reviews.MOVIE_IDS + " = ?",
            new String[] { cursor.getString(cursor.getColumnIndex(TMDbContract.Movies.MOVIE_ID)) }, null);

    if (cursorReviews.getCount() == 0) {
        mRootView.findViewById(R.id.reviews_header).setVisibility(View.GONE);
    } else {
        ReviewCursorAdapter adapter2 = new ReviewCursorAdapter(getActivity(), R.layout.review_card,
                cursorReviews);
        for (int i = 0; i < adapter2.getCount(); i++) {
            View view = adapter2.getView(i, null, null);
            reviews.addView(view);
        }
    }
}