Developing Android Twitter Applications and Using TextView and Linkify

I am developing a Twitter application in which I want the user to display timelines, and Textview in the lists requires clicks of ( http: //) URLs , (@) usernames and (#) hasTags, and I want to call custom methods for these actions, I used the Linkify class and actions, but did not use them, because a setting that I cannot turn on.

+4
source share
3 answers

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

+2
source

Here is my solution. The main idea is to split the text words and create a TextView for each of them, wrapping each line with a horizontal LinearLayout and lines in a 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()]); } } 
+2
source

There are many addLinks() methods on Linkify , one of which can allow you to fulfill your goals, if your goal is to run an action from these links.

You can also study the Linkify source code to find out how you can create your own that suits your needs.

0
source

Source: https://habr.com/ru/post/1304981/


All Articles