Step 1. Add the JitPack repository to your build file
Add it in your root build.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.hendraanggrian:socialview:0.1'
}
<dependency>
<groupId>com.github.hendraanggrian</groupId>
<artifactId>socialview</artifactId>
<version>0.1</version>
</dependency>
libraryDependencies += "com.github.hendraanggrian" % "socialview" % "0.1"
:dependencies [[com.github.hendraanggrian/socialview "0.1"]]
TextView and EditText with hashtag, mention, and hyperlink support.
repositories {
mavenCentral()
google()
}
dependencies {
// base widgets
implementation "com.hendraanggrian.appcompat:socialview:$version"
// auto-complete widgets
implementation "com.hendraanggrian.appcompat:socialview-autocomplete:$version"
}
Core library contains SocialTextView
, SocialEditText
and helper class
applies these behavior into any TextView
.
<com.hendraanggrian.appcompat.socialview.widget.SocialTextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#hashtag and @mention."
app:socialFlags="hashtag|mention"
app:hashtagColor="@color/blue"
app:mentionColor="@color/red"/>
See attrs.xml for full list of available attributes.
Modify its state and set listeners programmatically.
textView.setMentionEnabled(false);
textView.setHashtagColor(Color.RED);
textView.setOnHashtagClickListener(new SocialView.OnClickListener() {
@Override
public void invoke(SocialView socialView, String s) {
// do something
}
});
Extended library comes with SocialAutoCompleteTextView
.
<com.hendraanggrian.appcompat.socialview.widget.SocialAutoCompleteTextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="What's on your mind?"
app:socialFlags="hyperlink"
app:hyperlinkColor="@color/green"/>
To display suggestions, it is required to setHashtagAdapter()
and setMentionAdapter()
.
ArrayAdapter<Hashtag> hashtagAdapter = new HashtagAdapter(getContext());
hashtagAdapter.add(new Hashtag("follow"));
hashtagAdapter.add(new Hashtag("followme", 1000));
hashtagAdapter.add(new Hashtag("followmeorillkillyou", 500));
textView.setHashtagAdapter(hashtagAdapter);
ArrayAdapter<Mention> mentionAdapter = new MentionAdapter(getContext());
mentionAdapter.add(new Mention("dirtyhobo"));
mentionAdapter.add(new Mention("hobo", "Regular Hobo", R.mipmap.ic_launcher));
mentionAdapter.add(new Mention("hendraanggrian", "Hendra Anggrian",
"https://avatars0.githubusercontent.com/u/11507430?v=3&s=460"));
textView.setMentionAdapter(mentionAdapter);
To customize hashtag or mention adapter, create a custom adapter using
customized SocialAdapter
or write your own ArrayAdapter
.
Custom adapters are experimental, see sample for example.
public class Person {
public final String name;
public Person(String name) {
this.name = name;
}
}
// easier
public class PersonAdapter extends SocialAdapter<Person> {
public PersonAdapter(@NonNull Context context) {
super(context, R.layout.item_person, R.id.textview_person);
}
@Override
public String convertToString(Person $receiver) {
return $receiver.name;
}
@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
// ...
}
}
// this works too
public class PersonAdapter extends ArrayAdapter<Person> {
// your own adapter layout, view holder, data binding
// and of course, filtering logic
}
Then, use the custom adapter.
ArrayAdapter<Person> adapter = new PersonAdapter(getContext());
adapter.add(personA);
adapter.add(personB);
textView.setMentionAdapter(adapter);