Example usage for android.widget TextView getContext

List of usage examples for android.widget TextView getContext

Introduction

In this page you can find the example usage for android.widget TextView getContext.

Prototype

@ViewDebug.CapturedViewProperty
public final Context getContext() 

Source Link

Document

Returns the context the view is running in, through which it can access the current theme, resources, etc.

Usage

From source file:de.spiritcroc.modular_remote.Util.java

/**
 * @return//from   w  w w . j av  a2  s  .  c  o  m
 * User input if input allowed. null if invalid
 */
public static String getUserInput(TextView textView, boolean emptyForbidden) {
    String input = textView.getText().toString();
    if (input.equals("")) {
        if (emptyForbidden) {
            Context context = textView.getContext();
            textView.setError(context.getString(R.string.error_should_not_be_empty));
            return null;
        } else {
            return input;
        }
    }
    for (String forbidden : FORBIDDEN_SUBSTRINGS) {
        if (input.contains(forbidden)) {
            Context context = textView.getContext();
            String errorMsg = context.getString(R.string.error_should_not_contain_s, forbidden);
            textView.setError(errorMsg);
            return null;
        }
    }
    return input;
}

From source file:com.ruesga.rview.misc.Formatter.java

@BindingAdapter("fileStatus")
public static void toFileStatus(TextView view, ChangeDetailsFragment.FileItemModel item) {
    if (item == null) {
        view.setText(null);//  w  w w  .  j  av  a 2 s  .  c o  m
        return;
    }

    String status = "";
    String txt = "";
    if (item.info != null) {
        if (item.info.status.equals(FileStatus.R)) {
            status = "[" + view.getContext().getString(R.string.file_status_renamed) + "] ";
        } else if (item.info.status.equals(FileStatus.C)) {
            status = "[" + view.getContext().getString(R.string.file_status_copied) + "] ";
        } else if (item.info.status.equals(FileStatus.W)) {
            status = "[" + view.getContext().getString(R.string.file_status_rewritten) + "] ";
        }
        txt = status;
        if (!TextUtils.isEmpty(item.info.oldPath)) {
            txt += item.info.oldPath + " \u2192 ";
        }
    }
    txt += item.file.startsWith("/") ? item.file.substring(1) : item.file;
    view.setText(txt);
}

From source file:de.vanita5.twittnuker.util.ThemeUtils.java

public static void initTextView(TextView view) {
    if (view.isInEditMode())
        return;/*w ww. j av a  2 s .  c o m*/
    final Context context = view.getContext();
    //        view.setLinkTextColor(ThemeUtils.getUserLinkTextColor(context));
    //        view.setHighlightColor(ThemeUtils.getUserHighlightColor(context));
    view.setTypeface(ThemeUtils.getUserTypeface(context, view.getTypeface()));
}

From source file:Main.java

public static void setClickable(final TextView textView) {
    textView.setMovementMethod(LinkMovementMethod.getInstance());
    Spannable sp = (Spannable) textView.getText();
    ImageSpan[] images = sp.getSpans(0, textView.getText().length(), ImageSpan.class);

    for (ImageSpan span : images) {
        final String image_src = span.getSource();
        final int start = sp.getSpanStart(span);
        final int end = sp.getSpanEnd(span);

        ClickableSpan click_span = new ClickableSpan() {
            @Override/*from w  ww.jav  a  2 s .  c  o m*/
            public void onClick(View widget) {
                String[] strs = image_src.split("/");
                String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/LilyClient/"
                        + strs[strs.length - 2] + "-" + strs[strs.length - 1];

                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.setType("image/*");
                intent.setDataAndType(Uri.fromFile(new File(filePath)), "image/*");
                textView.getContext().startActivity(intent);

            }
        };
        ClickableSpan[] click_spans = sp.getSpans(start, end, ClickableSpan.class);
        if (click_spans.length != 0) {
            for (ClickableSpan c_span : click_spans) {
                sp.removeSpan(c_span);
            }
        }
        sp.setSpan(click_span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
}

From source file:im.zico.fancy.common.widget.HackyMovementMethod.java

@Override
public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
    if (mGray == null) {
        mGray = new BackgroundColorSpan(
                ContextCompat.getColor(widget.getContext(), R.color.alpha_spannable_pressed));
    }//from  w w  w  .j  a va2s  . c  om

    mIsLinkHit = false;

    int action = event.getAction();

    if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_UP) {
        int x = (int) event.getX();
        int y = (int) event.getY();
        x -= widget.getTotalPaddingLeft();
        y -= widget.getTotalPaddingTop();
        x += widget.getScrollX();
        y += widget.getScrollY();

        int line = widget.getLayout().getLineForVertical(y);
        int offset = widget.getLayout().getOffsetForHorizontal(line, x);

        ClickableSpan[] spans = buffer.getSpans(offset, offset, ClickableSpan.class);

        if (spans.length != 0) {
            int start = buffer.getSpanStart(spans[0]);
            int end = buffer.getSpanEnd(spans[0]);
            mIsLinkHit = true;
            if (action == MotionEvent.ACTION_DOWN) {
                buffer.setSpan(mGray, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            } else if (action == MotionEvent.ACTION_UP) {
                spans[0].onClick(widget);
                buffer.removeSpan(mGray);
            }
            return true;
        }
    } else {
        buffer.removeSpan(mGray);
    }

    return Touch.onTouchEvent(widget, buffer, event);
}

From source file:com.ruesga.rview.misc.Formatter.java

@BindingAdapter({ "changeStatus", "currentRevision" })
public static void toChangeStatus(TextView view, ChangeInfo change, boolean currentRevision) {
    if (change == null) {
        view.setText(null);/* w  w  w  .ja  v a 2s.  c  om*/
        return;
    }

    if (!currentRevision) {
        view.setText(R.string.change_statuses_not_current);
    } else if (ChangeStatus.NEW.equals(change.status)) {
        if (change.submittable) {
            view.setText(R.string.change_statuses_ready_to_submit);
        } else if (!change.mergeable) {
            view.setText(R.string.change_statuses_not_mergeable);
        } else {
            String neededLabel = ModelHelper.checkNeedsLabel(change.labels);
            if (neededLabel != null) {
                view.setText(view.getContext().getString(R.string.change_statuses_needs_label, neededLabel));
            } else {
                view.setText(R.string.menu_open);
            }
        }
    } else if (ChangeStatus.SUBMITTED.equals(change.status)) {
        view.setText(R.string.change_statuses_submitted);
    } else if (ChangeStatus.MERGED.equals(change.status)) {
        view.setText(R.string.menu_merged);
    } else if (ChangeStatus.ABANDONED.equals(change.status)) {
        view.setText(R.string.menu_abandoned);
    } else if (ChangeStatus.DRAFT.equals(change.status)) {
        view.setText(R.string.menu_draft);
    } else {
        view.setText(null);
    }
}

From source file:com.ibm.mil.readyapps.physio.fragments.library.VideoFragment.java

private void setupView(View view) {
    Exercise exercise = DataManager.getCurrentPatient().getSelectedExercise();

    TextView exerciseName = (TextView) view.findViewById(R.id.exercise_name);
    exerciseName.setTypeface(AndroidUtils.robotoRegular(exerciseName.getContext()));
    exerciseName.setText(exercise.getDescription());

    List<Routine> routines = DataManager.getCurrentPatient().getRoutines();
    Routine routine = DataManager.getCurrentPatient().getSelectedRoutine();
    int routineIndex = routines.indexOf(routine);
    TextView routineName = (TextView) view.findViewById(R.id.routine_name);
    routineName.setText((routineIndex + 1) + " of " + routines.size());

    TextView minutesValue = (TextView) view.findViewById(R.id.minutes_value);
    minutesValue.setTypeface(AndroidUtils.robotoBold(minutesValue.getContext()));
    minutesValue.setText(Utils.zeroPaddedNumber(exercise.getMinutes(), 2));
    TextView repetitionsValue = (TextView) view.findViewById(R.id.repetitions_value);
    repetitionsValue.setTypeface(AndroidUtils.robotoBold(repetitionsValue.getContext()));
    repetitionsValue.setText(Utils.zeroPaddedNumber(exercise.getRepetitions(), 2));
    TextView setsValue = (TextView) view.findViewById(R.id.sets_value);
    setsValue.setTypeface(AndroidUtils.robotoBold(setsValue.getContext()));
    setsValue.setText(Utils.zeroPaddedNumber(exercise.getSets(), 2));
    TextView toolsValue = (TextView) view.findViewById(R.id.tools);
    toolsValue.setText(exercise.getTools());

    TextView endRoutine = (TextView) view.findViewById(R.id.end_routine_button);
    endRoutine.setTypeface(AndroidUtils.robotoRegular(endRoutine.getContext()));
    endRoutine.setOnClickListener(new View.OnClickListener() {
        @Override//from  ww w . ja  va 2s .co  m
        public void onClick(View v) {
            getFragmentManager().beginTransaction().replace(R.id.fragment_container, new EndRoutineFragment())
                    .addToBackStack(null).commit();
        }
    });

    TextView nextExercise = (TextView) view.findViewById(R.id.next_exercise_button);
    nextExercise.setTypeface(AndroidUtils.robotoRegular(nextExercise.getContext()));

    nextExercise.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Exercise nextExercise = nextExercise();
            if (nextExercise != null) {
                DataManager.getCurrentPatient().setSelectedExercise(nextExercise);
                showNextExerciseDialog();
            } else {
                showFinalExerciseDialog();
            }
        }
    });

    YouTubePlayerSupportFragment player = new YouTubePlayerSupportFragment();
    player.initialize(YOUTUBE_API_KEY, this);
    getChildFragmentManager().beginTransaction().add(R.id.youtube_container, player).commit();
}

From source file:com.hyperaware.conference.android.fragment.HomeFragment.java

private void populateTimeGroups(SortedMap<DateRange, List<AgendaItem>> groups, ViewGroup time_groups) {
    final LayoutInflater inflater = getActivity().getLayoutInflater();

    for (Map.Entry<DateRange, List<AgendaItem>> entry : groups.entrySet()) {
        final ViewGroup sessions_group = (ViewGroup) inflater.inflate(R.layout.item_time_group_sessions,
                time_groups, false);/*from  ww w.ja  v  a  2  s  .co m*/
        time_groups.addView(sessions_group);

        final TextView tv_time = (TextView) sessions_group.findViewById(R.id.tv_time);
        final DateRange range = entry.getKey();
        sb.setLength(0);
        DateUtils.formatDateRange(tv_time.getContext(), formatter, range.start, range.end,
                DateUtils.FORMAT_SHOW_TIME, event.getTimezoneName());
        tv_time.setText(formatter.toString());

        final ViewGroup vg_sessions = (ViewGroup) sessions_group.findViewById(R.id.vg_sessions);
        vg_sessions.removeAllViews();
        for (final AgendaItem item : entry.getValue()) {
            final View session = inflater.inflate(R.layout.item_time_group_session, vg_sessions, false);
            vg_sessions.addView(session);
            final TextView tv_topic = (TextView) session.findViewById(R.id.tv_topic);
            tv_topic.setText(item.getTopic());
        }
    }
}

From source file:net.naonedbus.appwidget.HoraireWidgetProvider.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private int getHorairesCount(final Context context, final Bundle bundle) {

    synchronized (sLock) {
        if (sHoraireTextWidth == Integer.MIN_VALUE) {
            final TextView horaireTextView = new TextView(context);
            horaireTextView.setTextAppearance(horaireTextView.getContext(),
                    android.R.style.TextAppearance_Medium);

            final DateTime noon = new DateTime().withHourOfDay(12).withMinuteOfHour(00);
            final java.text.DateFormat timeFormat = DateFormat.getTimeFormat(context);
            horaireTextView.setText(//from www.  j  a v  a2 s  .  c  o  m
                    FormatUtils.formatTimeAmPm(context, timeFormat.format(noon.toDate())) + " \u2022  ");
            final int specY = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
            final int specX = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
            horaireTextView.measure(specX, specY);
            sHoraireTextWidth = horaireTextView.getMeasuredWidth();
        }
    }

    final Resources r = context.getResources();
    final int padding = r.getDimensionPixelSize(R.dimen.padding_small);

    final int minWidth = bundle.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);
    final int minWidthPixel = Math.round(
            TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, minWidth, r.getDisplayMetrics())) - padding;

    return (minWidthPixel / sHoraireTextWidth);
}

From source file:xyz.klinker.android.article.Utils.java

/**
 * Changes the text selection handle colors.
 *//*from   w  w  w  . j  a v a 2 s  . c  om*/
static void changeTextSelectionHandleColors(TextView textView, int color) {
    textView.setHighlightColor(Color.argb(40, Color.red(color), Color.green(color), Color.blue(color)));

    try {
        Field editorField = TextView.class.getDeclaredField("mEditor");
        if (!editorField.isAccessible()) {
            editorField.setAccessible(true);
        }

        Object editor = editorField.get(textView);
        Class<?> editorClass = editor.getClass();

        String[] handleNames = { "mSelectHandleLeft", "mSelectHandleRight", "mSelectHandleCenter" };
        String[] resNames = { "mTextSelectHandleLeftRes", "mTextSelectHandleRightRes", "mTextSelectHandleRes" };

        for (int i = 0; i < handleNames.length; i++) {
            Field handleField = editorClass.getDeclaredField(handleNames[i]);
            if (!handleField.isAccessible()) {
                handleField.setAccessible(true);
            }

            Drawable handleDrawable = (Drawable) handleField.get(editor);

            if (handleDrawable == null) {
                Field resField = TextView.class.getDeclaredField(resNames[i]);
                if (!resField.isAccessible()) {
                    resField.setAccessible(true);
                }
                int resId = resField.getInt(textView);
                handleDrawable = ContextCompat.getDrawable(textView.getContext(), resId);
            }

            if (handleDrawable != null) {
                Drawable drawable = handleDrawable.mutate();
                drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
                handleField.set(editor, drawable);
            }
        }
    } catch (Exception ignored) {
    }
}