开发者

Android Twitter Application Development and Usage of TextView and Linkify

I'm on developing a twitter kind of Application where in I want that the user would be displayed the timelines and the Textview in the Lists 开发者_如何学编程require to perform clicks on (http://)URLs, (@)usernames, and (#)hasTags and I want to invoke custom methods over these actions, I have used the Linkify class and the actions but where of no use because the customization that i require cannot be incorporated.


I have a solution to the problem to check it out go to the below mentioned link http://www.orangeapple.org/?p=354


Here is my solution. The main idea is to split the text words and creating a TextView for each one, wrapping each line with horizontal LinearLayout and the lines into vertical LinearLayout:

private LinearLayout mDescription; // vertical LinearLayout 

    private void setDescriptionText(String twitterText){
    String[] splitted;

    String regexp = "(@[-a-zA-Z0-9_]*)|(#[-a-zA-Z0-9_]*)|(http://[-a-zA-Z0-9/._]*)|(https://[-a-zA-Z0-9/._]*)|( )";

    TextSplitter splitter = new TextSplitter(regexp);
    splitted = splitter.split(twitterText);

    TextView[] textViews = new TextView[splitted.length];
    for (int i = 0; i < splitted.length; i++) {
        final String str = splitted[i];
        TextView textView = new TextView(mDescription.getContext());  
        textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
        textViews[i] = textView;


        textView.setText(str);
        textView.setTypeface(roboReg);
        textView.setTextColor(Color.WHITE);

        if (str.startsWith("@")){
            textView.setTextColor(mLinkColor);
            textView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    startWebViewActivity("https://twitter.com/"+str.substring(1));
                }
            });
        }else if (str.startsWith("#")){
            textView.setTextColor(mLinkColor);
            textView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    startWebViewActivity("https://twitter.com/#!/search/?q="+str.substring(1) + "&src=hash");
                }
            });

        }else if (str.startsWith("http://") || str.startsWith("https://")){
            textView.setTextColor(mLinkColor);
            textView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    startWebViewActivity(str);
                }
            });
        }
    }

    int maxWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 210, getResources().getDisplayMetrics());
    populateText(mDescription, maxWidth , textViews, mDescription.getContext());

}

private void populateText(LinearLayout ll,int maxWidth, View[] views, Context mContext) {

    ll.removeAllViews();
    LinearLayout.LayoutParams params;
    LinearLayout newLL = new LinearLayout(mContext);
    newLL.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));
    newLL.setGravity(Gravity.LEFT);
    newLL.setOrientation(LinearLayout.HORIZONTAL);

    int widthSoFar = 0;

    for (int i = 0; i < views.length; i++) {
        LinearLayout LL = new LinearLayout(mContext);
        LL.setOrientation(LinearLayout.HORIZONTAL);
        LL.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
        LL.setLayoutParams(new ListView.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        views[i].measure(0, 0);
        params = new LinearLayout.LayoutParams(views[i].getMeasuredWidth(),
                LayoutParams.WRAP_CONTENT);
        LL.addView(views[i], params);
        LL.measure(0, 0);
        widthSoFar += views[i].getMeasuredWidth();// YOU MAY NEED TO ADD THE MARGINS
        if (widthSoFar >= maxWidth) {
            ll.addView(newLL);

            newLL = new LinearLayout(mContext);
            newLL.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));
            newLL.setOrientation(LinearLayout.HORIZONTAL);
            newLL.setGravity(Gravity.LEFT);
            params = new LinearLayout.LayoutParams(LL
                    .getMeasuredWidth(), LL.getMeasuredHeight());
            newLL.addView(LL, params);
            widthSoFar = LL.getMeasuredWidth();
        } else {
            newLL.addView(LL);
        }
    }
    ll.addView(newLL);
}

private class TextSplitter {

    private Pattern pattern;
    private boolean keep_delimiters;

    public TextSplitter(Pattern pattern, boolean keep_delimiters) {
        this.pattern = pattern;
        this.keep_delimiters = keep_delimiters;
    }

    public TextSplitter(String pattern, boolean keep_delimiters) {
        this(Pattern.compile(pattern == null ? "" : pattern), keep_delimiters);
    }


    public TextSplitter(String pattern) {
        this(pattern, true);
    }

    public String[] split(String text) {
        if (text == null) {
            text = "";
        }

        int last_match = 0;
        LinkedList<String> splitted = new LinkedList<String>();

        Matcher m = this.pattern.matcher(text);

        while (m.find()) {

            splitted.add(text.substring(last_match, m.start()));

            if (this.keep_delimiters) {
                splitted.add(m.group());
            }

            last_match = m.end();
        }

        splitted.add(text.substring(last_match));

        return splitted.toArray(new String[splitted.size()]);
    }

}


There are many addLinks() methods on Linkify, one of which may let you accomplish your aims, if your goal is to start an activity from those links.

You can also examine the source code to Linkify to see how you might create your own that meets your needs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜