Added Clickable hashtags & usernames
This commit is contained in:
NudeDude 2018-03-19 20:41:38 +01:00
parent 30e69cdde6
commit f9ac7f6d7c
8 changed files with 54 additions and 17 deletions

View File

@ -242,9 +242,13 @@ public class MainActivity extends AppCompatActivity implements
String search = trend.getData().getTrendname(position); String search = trend.getData().getTrendname(position);
Intent intent = new Intent(this, SearchPage.class); Intent intent = new Intent(this, SearchPage.class);
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putString("search", search); if(search.startsWith("#")) {
if(search.startsWith("#"))
bundle.putString("Addition", search); bundle.putString("Addition", search);
bundle.putString("search", search);
} else {
search = '\"'+ search + '\"';
bundle.putString("search", search);
}
intent.putExtras(bundle); intent.putExtras(bundle);
startActivity(intent); startActivity(intent);
} }

View File

@ -86,6 +86,7 @@ public class MainPage extends AsyncTask<Integer, Void, Integer> {
trendsAdapter.getData().setTrends( mTwitter.getTrends(woeid) ); trendsAdapter.getData().setTrends( mTwitter.getTrends(woeid) );
else else
trendsAdapter = new TrendRecycler(new TrendDatabase(mTwitter.getTrends(woeid),ui.get()), ui.get()); trendsAdapter = new TrendRecycler(new TrendDatabase(mTwitter.getTrends(woeid),ui.get()), ui.get());
trendsAdapter.setColor(font);
break; break;
case MENT: case MENT:

View File

@ -9,9 +9,12 @@ import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout; import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableStringBuilder; import android.text.SpannableStringBuilder;
import android.text.Spanned; import android.text.Spanned;
import android.text.style.ForegroundColorSpan; import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.ImageView; import android.widget.ImageView;
@ -31,6 +34,7 @@ import org.nuclearfog.twidda.database.TweetDatabase;
import org.nuclearfog.twidda.R; import org.nuclearfog.twidda.R;
import org.nuclearfog.twidda.viewadapter.TimelineRecycler; import org.nuclearfog.twidda.viewadapter.TimelineRecycler;
import org.nuclearfog.twidda.window.ColorPreferences; import org.nuclearfog.twidda.window.ColorPreferences;
import org.nuclearfog.twidda.window.SearchPage;
import org.nuclearfog.twidda.window.TweetDetail; import org.nuclearfog.twidda.window.TweetDetail;
import org.nuclearfog.twidda.backend.listitems.*; import org.nuclearfog.twidda.backend.listitems.*;
import org.nuclearfog.twidda.window.UserProfile; import org.nuclearfog.twidda.window.UserProfile;
@ -173,7 +177,6 @@ public class StatusLoader extends AsyncTask<Long, Void, Long> implements View.On
TweetDetail connect = ui.get(); TweetDetail connect = ui.get();
if(connect == null) if(connect == null)
return; return;
final Context c = connect;
TextView tweet = (TextView)connect.findViewById(R.id.tweet_detailed); TextView tweet = (TextView)connect.findViewById(R.id.tweet_detailed);
TextView username = (TextView)connect.findViewById(R.id.usernamedetail); TextView username = (TextView)connect.findViewById(R.id.usernamedetail);
TextView scrName = (TextView)connect.findViewById(R.id.scrnamedetail); TextView scrName = (TextView)connect.findViewById(R.id.scrnamedetail);
@ -192,6 +195,7 @@ public class StatusLoader extends AsyncTask<Long, Void, Long> implements View.On
Button mediabutton = (Button)connect.findViewById(R.id.image_attach); Button mediabutton = (Button)connect.findViewById(R.id.image_attach);
if(mode == LOAD_TWEET) { if(mode == LOAD_TWEET) {
tweet.setMovementMethod(LinkMovementMethod.getInstance());
tweet.setText(highlight(tweetStr)); tweet.setText(highlight(tweetStr));
username.setText(usernameStr); username.setText(usernameStr);
scrName.setText(scrNameStr); scrName.setText(scrNameStr);
@ -245,11 +249,11 @@ public class StatusLoader extends AsyncTask<Long, Void, Long> implements View.On
txtAns.setText(ansStr); txtAns.setText(ansStr);
} }
else if(mode == DELETE) { else if(mode == DELETE) {
Toast.makeText(c, "Tweet gelöscht", Toast.LENGTH_LONG).show(); Toast.makeText(ui.get(), "Tweet gelöscht", Toast.LENGTH_LONG).show();
((TweetDetail)c).finish(); ui.get().finish();
} }
else { else {
Toast.makeText(c, "Fehler beim Laden: "+errMSG, Toast.LENGTH_LONG).show(); Toast.makeText(ui.get(), "Fehler beim Laden: "+errMSG, Toast.LENGTH_LONG).show();
if(ansReload.isRefreshing()) { if(ansReload.isRefreshing()) {
ansReload.setRefreshing(false); ansReload.setRefreshing(false);
} }
@ -272,8 +276,8 @@ public class StatusLoader extends AsyncTask<Long, Void, Long> implements View.On
return output.toString(); return output.toString();
} }
private SpannableStringBuilder highlight(String tweet) { private Spannable highlight(String tweet) {
SpannableStringBuilder sTweet = new SpannableStringBuilder(tweet); Spannable sTweet = new SpannableStringBuilder(tweet);
int start = 0; int start = 0;
boolean marked = false; boolean marked = false;
for(int i = 0 ; i < tweet.length() ; i++) { for(int i = 0 ; i < tweet.length() ; i++) {
@ -296,19 +300,44 @@ public class StatusLoader extends AsyncTask<Long, Void, Long> implements View.On
case '!': case '!':
case '?': case '?':
case '-': case '-':
if(marked) { if(marked && start != i-1) {
sTweet.setSpan(new ForegroundColorSpan(highlight),start,i, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); sTweet = spanning(sTweet, start, i);
} }
marked = false; marked = false;
break; break;
} }
} }
if(marked) { if(marked && start != tweet.length()-1) {
sTweet.setSpan(new ForegroundColorSpan(highlight),start,tweet.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); sTweet = spanning(sTweet, start, tweet.length());
} }
return sTweet; return sTweet;
} }
private Spannable spanning(Spannable sTweet, final int start, final int end) {
sTweet.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
TextView tv = (TextView) widget;
Spanned s = (Spanned) tv.getText();
String search = s.subSequence(start, end).toString();
Intent intent = new Intent(ui.get(), SearchPage.class);
Bundle bundle = new Bundle();
if(search.startsWith("#"))
bundle.putString("Addition", search);
bundle.putString("search", search);
intent.putExtras(bundle);
ui.get().startActivity(intent);
}
@Override
public void updateDrawState(TextPaint ds){
ds.setColor(highlight);
ds.setUnderlineText(false);
}
},start,end,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return sTweet;
}
private void setIcons(Button favoriteButton, Button retweetButton) { private void setIcons(Button favoriteButton, Button retweetButton) {
if(favorited) if(favorited)
favoriteButton.setBackgroundResource(R.drawable.favorite_enabled); favoriteButton.setBackgroundResource(R.drawable.favorite_enabled);

View File

@ -36,7 +36,7 @@ import twitter4j.conf.ConfigurationBuilder;
public class TwitterEngine { public class TwitterEngine {
private final String TWITTER_CONSUMER_KEY = "1JwXJbVrvGWrc9SSKPnnEWslJ"; private final String TWITTER_CONSUMER_KEY = "1JwXJbVrvGWrc9SSKPnnEWslJ";
private final String TWITTER_CONSUMER_SECRET = "GET YOUR OWN!"; private final String TWITTER_CONSUMER_SECRET = "INSERT SECRET"; // TODO
private static TwitterEngine mTwitter; private static TwitterEngine mTwitter;
private static long twitterID; private static long twitterID;

View File

@ -80,6 +80,7 @@ public class SearchPage extends AppCompatActivity implements UserRecycler.OnItem
public boolean onCreateOptionsMenu(Menu m) { public boolean onCreateOptionsMenu(Menu m) {
getMenuInflater().inflate(R.menu.search, m); getMenuInflater().inflate(R.menu.search, m);
SearchView searchQuery = (SearchView)m.findItem(R.id.new_search).getActionView(); SearchView searchQuery = (SearchView)m.findItem(R.id.new_search).getActionView();
searchQuery.setQueryHint(search);
searchQuery.setOnQueryTextListener(new SearchView.OnQueryTextListener() { searchQuery.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override @Override
public boolean onQueryTextSubmit(String s) { public boolean onQueryTextSubmit(String s) {

View File

@ -97,13 +97,13 @@
android:id="@+id/load_text" android:id="@+id/load_text"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/load_factor" /> android:text="@string/load" />
<CheckBox <CheckBox
android:id="@+id/toggleImg" android:id="@+id/toggleImg"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="20dp" android:layout_marginStart="10dp"
android:text="@string/image" /> android:text="@string/image" />
</LinearLayout> </LinearLayout>

View File

@ -44,4 +44,5 @@
<string name="profile_locked">Privates Profil</string> <string name="profile_locked">Privates Profil</string>
<string name="load_factor">Ladefaktor</string> <string name="load_factor">Ladefaktor</string>
<string name="followback">Folgt dir</string> <string name="followback">Folgt dir</string>
<string name="load">Anzahl Tweets</string>
</resources> </resources>

View File

@ -3,8 +3,9 @@
<item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/soylentgreen</item> <item name="colorAccent">@color/soylentgreen</item>
<item name="android:windowAnimationStyle">@style/TransactionPending</item>
<item name="android:colorBackground">@color/DarkBlue</item> <item name="android:colorBackground">@color/DarkBlue</item>
<item name="android:windowAnimationStyle">@style/TransactionPending</item>
<item name="android:statusBarColor">@android:color/black</item>
</style> </style>
<style name="Transparency" parent="AppTheme"> <style name="Transparency" parent="AppTheme">