Giter VIP home page Giter VIP logo

socialview's Introduction

socialview

Download Build Status GitHub license

demo

Android TextView and EditText with hashtag, mention, and hyperlink support. Comes in 2 packages:

  • core - comes with SocialTextView, SocialEditText and SocialViewImpl to attach any TextView.
  • commons - extended core library with SocialAutoCompleteTextView to display suggestions as you type.

Download

repositories {
    google()
    jcenter()
}

dependencies {
    compile 'com.hendraanggrian:socialview-core:0.18'
    compile 'com.hendraanggrian:socialview-commons:0.18' // optional
}

Core

demo_core1 demo_core2 demo_core3

Write SocialTextView or SocialEditText in xml.

<com.hendraanggrian.widget.SocialTextView
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="#hashtag and @mention."
    app:socialEnabled="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 Function2<SocialView, String, Unit>() {
    @Override
    public Unit invoke(SocialView socialView, String s) {
        // do something
        return null;
    }
});

Any TextView or subclasses of TextView can be made social, see SocialTextView.kt for example.

Commons

demo_commons1 demo_commons2 demo_commons3

Write SocialAutoCompleteTextView in xml.

<com.hendraanggrian.widget.SocialAutoCompleteTextView
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="What's on your mind?"
    app:socialEnabled="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.

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);

License

Copyright 2016 Hendra Anggrian

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.

socialview's People

Contributors

hanggrian avatar

Watchers

Balu Here avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.