0

I want to open new activite using startActivity(new Intent(context, MyProfile.class)); when user clike on textview with # or @

here is my code

    class CalloutLinkProfile extends ClickableSpan {

    Context context;
    TextPaint textPaint;

    public CalloutLinkProfile(Context ctx) {
        super();
        context = ctx;

    }


    @Override
    public void updateDrawState(TextPaint ds) {
        textPaint = ds;
       // ds.setColor(0xff336699);

        ds.setARGB(255, 30, 144, 255);
        ds.setTypeface(Typeface.DEFAULT_BOLD);
        ds.setUnderlineText(false); // set to false to remove underline
    }

    @Override
    public void onClick(View widget) {
        TextView tv = (TextView) widget;
        Spanned s = (Spanned) tv.getText();
        int start = s.getSpanStart(this);
        int end = s.getSpanEnd(this);
        String theWord = s.subSequence(start + 1, end).toString();
        Toast.makeText(context, String.format("Here's a cool user: %s", theWord), 10).show();
        startActivity(new Intent(context, MyProfile.class));

    }

here is my complete code

public class FeedHomeAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<String> id;
private ArrayList<String> puName;
private ArrayList<String> pfName;

private SQLiteDatabase dataBaseall;



public FeedHomeAdapter(Context c, ArrayList<String> id, ArrayList<String> uname,

                       ArrayList<String> sid, ArrayList<String> svideo, ArrayList<String> isfollow, ArrayList<String> ischat) {
    this.mContext = c;

    this.id = id;
    this.puName = uname;

    this.isfollowing = isfollow;
    this.pischat = ischat;

}

public int getCount() {
    // TODO Auto-generated method stub
    return id.size();
}

public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

public long getItemId(int position) {

    // TODO Auto-generated method stub
    return position;
}

public ArrayList<int[]> getSpans(String body, char prefix) {
    ArrayList<int[]> spans = new ArrayList<int[]>();

    Pattern pattern = Pattern.compile(prefix + "\\w+");
    Matcher matcher = pattern.matcher(body);

    // Check all occurrences
    while (matcher.find()) {
        int[] currentSpan = new int[2];
        currentSpan[0] = matcher.start();
        currentSpan[1] = matcher.end();
        spans.add(currentSpan);
    }

    return spans;
}

public View getView(final int pos, View child, ViewGroup parent) {
    final Holder mHolder;
    if (child == null) {
        child = LayoutInflater.from(mContext).inflate(R.layout.chat_display_item, parent, false);
        LayoutInflater layoutInflater;
        layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        child = layoutInflater.inflate(R.layout.card_layout, null);

        mHolder = new Holder();
        mHolder.txt_uName = (TextView) child.findViewById(R.id.name);
        mHolder.txt_uName1 = (TextView) child.findViewById(R.id.name1);
        mHolder.txt_fName = (TextView) child.findViewById(R.id.fname);
        mHolder.txt_satus = (TextView) child.findViewById(R.id.note);


        mHolder.comment = (ImageButton) child.findViewById(R.id.replide);

        child.setTag(mHolder);

    } else {
        mHolder = (Holder) child.getTag();
    }
    mHolder.txt_satus.setHighlightColor(Color.WHITE);
    mHolder.txt_satus.setHighlightColor(Color.TRANSPARENT);



    ArrayList<int[]> hashtagSpans = getSpans(psatus.get(pos), '#');
    ArrayList<int[]> calloutSpans = getSpans(psatus.get(pos), '@');

    SpannableString commentsContent =
            new SpannableString(this.psatus.get(pos));

    for (int i = 0; i < hashtagSpans.size(); i++) {
        int[] span = hashtagSpans.get(i);
        int hashTagStart = span[0];
        int hashTagEnd = span[1];

        commentsContent.setSpan(new Hashtag(mContext),
                hashTagStart,
                hashTagEnd, 0);

    }
    for (int i = 0; i < calloutSpans.size(); i++) {
        int[] span = calloutSpans.get(i);
        int calloutStart = span[0];
        int calloutEnd = span[1];

        commentsContent.setSpan(new CalloutLinkProfile(mContext),
                calloutStart,
                calloutEnd, 0);

    }



    mHolder.txt_satus.setMovementMethod(LinkMovementMethod.getInstance());
    mHolder.txt_satus.setText(commentsContent);

    mHolder.txt_linktext.setText(statuslinktext.get(pos));
    mHolder.txt_time.setText(ptime.get(pos));








    return child;
}


public class Holder {
    TextView txt_id;
    TextView txt_satus;
    TextView txt_time;
    TextView txt_uName;
    TextView txt_uName1;

    LinearLayout error_d;
    RelativeLayout profile_view;


}


// to check if app is install
private boolean appInstalledOrNot(String uri) {
    PackageManager pm = mContext.getPackageManager();
    boolean app_installed = false;
    try {
        pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
        app_installed = true;
    } catch (PackageManager.NameNotFoundException e) {
        app_installed = false;
    }
    return app_installed;
}







class CalloutLinkProfile extends ClickableSpan {

    Context context;
    TextPaint textPaint;

    public CalloutLinkProfile(Context ctx) {
        super();
        context = ctx;

    }


    @Override
    public void updateDrawState(TextPaint ds) {
        textPaint = ds;
       // ds.setColor(0xff336699);

        ds.setARGB(255, 30, 144, 255);
        ds.setTypeface(Typeface.DEFAULT_BOLD);
        ds.setUnderlineText(false); // set to false to remove underline
    }

    @Override
    public void onClick(View widget) {
        TextView tv = (TextView) widget;
        Spanned s = (Spanned) tv.getText();
        int start = s.getSpanStart(this);
        int end = s.getSpanEnd(this);
        String theWord = s.subSequence(start + 1, end).toString();
        Toast.makeText(context, String.format("Here's a cool user: %s", theWord), 10).show();
        startActivity(new Intent(context, MyProfile.class));

    }



}

}

  • and what's the issue? – Blackbelt Nov 05 '16 at 08:59
  • can not start new startActivity when clicked on. – kengodmans Nov 05 '16 at 09:01
  • what do you mean ? onClick is not being called? Where did you set the clickablespan – Blackbelt Nov 05 '16 at 09:02
  • if i add startActivity(new Intent(context, MyProfile.class)); it show error: Cannot resolve method 'startActivity(android.content.Intent)'. – kengodmans Nov 05 '16 at 09:05
  • 1
    `context.startActivity(android.content.Intent)` – Blackbelt Nov 05 '16 at 09:06
  • Welcome to SO! I hope you'll find the site useful. In my experience, learning to use the site also helped me to grow personally as a developer. This is a great start to asking a good question. You have an idea about what you want to achieve and it makes sense intuitively, however it is missing an example of what you've tried together with the output you observed and the desired output. Please consider adding these such that other users can test your code and provide you with code in return that's useful for you. – Bobby Nov 05 '16 at 10:54
  • tanks. Is working – kengodmans Nov 05 '16 at 11:01

1 Answers1

0

Clicking TextView is not working?