Android Open Source - wally Text Link Builder






From Project

Back to project page wally.

License

The source code is released under:

Apache License

If you think the Android project wally listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2014 Freddie (Musenkishi) Lust-Hed
 *//w  w  w.  ja  va 2  s  .  co  m
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.musenkishi.wally.util;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.view.View;

/**
 * Builder class for Spannable Strings. By default they are coloured light blue without underline.
 * No one likes underlines on links.
 *
 * Created by Freddie (Musenkishi) Lust-Hed on 2014-04-23.
 */
public class TextLinkBuilder {

    private int start = 0;
    private int end = 0;

    private String text;
    private int color = 0xFF0E84FC; //default color which is light blue
    private OnTextClickedListener onTextClickedListener;

    private Context context;
    private String font = null;
    private String TAG = "TextLinkBuilder";

    public interface OnTextClickedListener{
        void onClick(View textView);
    }

    /**
     * IMPORTANT: In order for the click listener to work, you must call
     * setMovementMethod(LinkMovementMethod.getInstance()) on the TextView before you
     * set the Spannable String
     *
     * @param context
     * @param text text to be used in TextView.
     * @param match text to match in text. First occurrence will be selected.
     */
    public TextLinkBuilder(Context context, String text, String match) {
        this.context = context;
        this.text = text;

        int start = text.indexOf(match);
        int end = match.length() + start;

        if (start == -1){
            throw new StringIndexOutOfBoundsException("text is not a substring. Are you sure the strings match?");
        }

        this.start = start;
        this.end = end;
    }

    /**
     * IMPORTANT: In order for the click listener to work you must call
     * setMovementMethod(LinkMovementMethod.getInstance()) on the TextView before you
     * set the Spannable String
     *
     * @param context
     * @param textResourseId resource id of text to be used in TextView.
     * @param matchReourseId resource id of text to match in textResourceId. First occurrence will be selected.
     */
    public TextLinkBuilder(Context context, int textResourseId, int matchReourseId) {
        this.context = context;
        text = context.getString(textResourseId);

        String match = context.getString(matchReourseId);

        int start = text.indexOf(match);
        int end = match.length() + start;

        if (start == -1){
            throw new StringIndexOutOfBoundsException("text is not a substring");
        }

        this.start = start;
        this.end = end;
    }


    public TextLinkBuilder font(String font){
        this.font = font;
        return this;
    }

    public TextLinkBuilder color(int color){
        this.color = color;
        return this;
    }

    public TextLinkBuilder onClick(OnTextClickedListener onTextClickedListener){
        this.onTextClickedListener = onTextClickedListener;
        return this;
    }

    public SpannableString build(){
        SpannableString spannableString = new SpannableString(text);
        TextClickableSpan clickableSpan = new TextClickableSpan(color) {
            @Override
            public void onClick(View textView) {
                if (onTextClickedListener != null) {
                    onTextClickedListener.onClick(textView);
                }
            }
        };
        if (font != null) {
            spannableString.setSpan(new TypefaceSpan(context, font), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        spannableString.setSpan(clickableSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        return spannableString;
    }



}




Java Source Code List

com.musenkishi.wally.activities.ImageDetailsActivity.java
com.musenkishi.wally.activities.MainActivity.java
com.musenkishi.wally.adapters.RecyclerImagesAdapter.java
com.musenkishi.wally.adapters.RecyclerSavedImagesAdapter.java
com.musenkishi.wally.adapters.SmartFragmentPagerAdapter.java
com.musenkishi.wally.adapters.SmartFragmentStatePagerAdapter.java
com.musenkishi.wally.anim.BaseItemAnimator.java
com.musenkishi.wally.anim.ScaleInOutItemAnimator.java
com.musenkishi.wally.anim.interpolator.EaseInOutBezierInterpolator.java
com.musenkishi.wally.anim.interpolator.FastOutLinearInInterpolator.java
com.musenkishi.wally.anim.interpolator.FastOutSlowInInterpolator.java
com.musenkishi.wally.anim.interpolator.LinearOutSlowInInterpolator.java
com.musenkishi.wally.base.BaseActivity.java
com.musenkishi.wally.base.BaseFragment.java
com.musenkishi.wally.base.GridFragment.java
com.musenkishi.wally.base.WallyApplication.java
com.musenkishi.wally.dataprovider.DataProvider.java
com.musenkishi.wally.dataprovider.FileManager.java
com.musenkishi.wally.dataprovider.NetworkDataProvider.java
com.musenkishi.wally.dataprovider.SharedPreferencesDataProvider.java
com.musenkishi.wally.dataprovider.models.DataProviderError.java
com.musenkishi.wally.dataprovider.models.SaveImageRequest.java
com.musenkishi.wally.dataprovider.okhttp.OkHttpStreamFetcher.java
com.musenkishi.wally.dataprovider.okhttp.OkHttpUrlLoader.java
com.musenkishi.wally.dataprovider.util.Parser.java
com.musenkishi.wally.fragments.CustomResolutionDialogFragment.java
com.musenkishi.wally.fragments.FilterDialogFragment.java
com.musenkishi.wally.fragments.ImageZoomFragment.java
com.musenkishi.wally.fragments.LatestFragment.java
com.musenkishi.wally.fragments.MaterialDialogFragment.java
com.musenkishi.wally.fragments.RandomImagesFragment.java
com.musenkishi.wally.fragments.SavedImagesFragment.java
com.musenkishi.wally.fragments.SearchFragment.java
com.musenkishi.wally.fragments.ToplistFragment.java
com.musenkishi.wally.models.Author.java
com.musenkishi.wally.models.ExceptionReporter.java
com.musenkishi.wally.models.Filter.java
com.musenkishi.wally.models.ImagePage.java
com.musenkishi.wally.models.Image.java
com.musenkishi.wally.models.ListFilterGroup.java
com.musenkishi.wally.models.Rating.java
com.musenkishi.wally.models.SavedImageData.java
com.musenkishi.wally.models.Size.java
com.musenkishi.wally.models.Tag.java
com.musenkishi.wally.models.filters.FilterAspectRatioKeys.java
com.musenkishi.wally.models.filters.FilterBoardsKeys.java
com.musenkishi.wally.models.filters.FilterBoards.java
com.musenkishi.wally.models.filters.FilterGroup.java
com.musenkishi.wally.models.filters.FilterGroupsStructure.java
com.musenkishi.wally.models.filters.FilterPurityKeys.java
com.musenkishi.wally.models.filters.FilterPurity.java
com.musenkishi.wally.models.filters.FilterResOptKeys.java
com.musenkishi.wally.models.filters.FilterResOpt.java
com.musenkishi.wally.models.filters.FilterResolutionKeys.java
com.musenkishi.wally.models.filters.FilterTimeSpanKeys.java
com.musenkishi.wally.muzei.WallyArtSource.java
com.musenkishi.wally.notification.NotificationProvider.java
com.musenkishi.wally.observers.FileChangeReceiver.java
com.musenkishi.wally.observers.FiltersChangeReceiver.java
com.musenkishi.wally.util.Blur.java
com.musenkishi.wally.util.PaletteLoader.java
com.musenkishi.wally.util.PaletteRequest.java
com.musenkishi.wally.util.SparseBooleanArrayParcelable.java
com.musenkishi.wally.util.TextClickableSpan.java
com.musenkishi.wally.util.TextLinkBuilder.java
com.musenkishi.wally.util.TypefaceSpan.java
com.musenkishi.wally.views.AutoGridView.java
com.musenkishi.wally.views.GridRecyclerView.java
com.musenkishi.wally.views.ObservableScrollView.java
com.musenkishi.wally.views.TabBarView.java
com.musenkishi.wally.views.TabView.java
com.musenkishi.wally.views.swipeclearlayout.SwipeClearLayout.java
net.margaritov.preference.colorpicker.dialog.ColorPickerDialogFragment.java
net.margaritov.preference.colorpicker.drawable.AlphaPatternDrawable.java
net.margaritov.preference.colorpicker.preference.ColorPickerPreference.java
net.margaritov.preference.colorpicker.view.ColorPanelView.java
net.margaritov.preference.colorpicker.view.ColorPickerView.java
nl.codesoup.cubicbezier.CubicBezierInterpolator.java