How to implement hashtag inside TextView? What I want to do is implement linkable hashtag inside textview. Then user can click on it (hashtag) and switch to another fragment. This is my layout and fragment.
Layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainFragment" >
<TextView
android:id="@+id/txtHashtag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Iena I #love you !!!"/>
</RelativeLayout>
Fragment
package com.xxxx;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.xxxxx.R;
public class MainFragment extends Fragment{
private TextView txtHashtag;
public MainFragment() {
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = LayoutInflater.from(getActivity()).inflate(R.layout.activity_main_fragment,
null);
txtHashtag = (TextView) v.findViewById(R.id.txtHashtag);
return v;
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
}
}
Please advice. Thank you.
Whatever view you want it to be reacting with clicks, you have to attach an onClickListener to it just like you implemented on button (assuming you have understood mechanics of button clicks).
therefore, it is possible to have it like this:
As for the method to go to the said fragment, ensure you have that fragment configured to receive parameters during instantiation as such:
then, your ViewPager adapter needs to be configured as such that it can instantiate the fragments on demand like so:
Provided the ViewPager is public static in your FragmentActivity object, you can access it from your MainFragment like this:
With this in mind, you may now create the method to add and swap fragment as follows:
You can use this simple library HashTagHelper
It has very simple usage:
Try below code to match TextView text that start with #:
Use this, Clickable Span in TextView
You can implement more than one span in a textView, so simply write a function to do that and call it for each
#HashTag