Search code examples
javaandroidandroid-fragmentsspannablestring

Android set clickable text to go one fragment to another fragment


I need to do something like this. Suppose I have 2 fragments A and B.There is a text which can be clickable in fragment A and when user click this text , he can go to fragment B. This example helped me to do it but I think it does not work for fragment. So please tell me a way to solve this problem.

public class myClaimsFragment extends Fragment {

   TextView requestNewClaim;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View newClaimRequest = inflater.inflate(R.layout.activity_my_claims, container, false);


        SpannableString ss = new SpannableString("Request");
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View textView) {
                Intent intent = new Intent(getActivity(),LoginActivity.class);
                startActivity(intent);
            }
        };
        ss.setSpan(clickableSpan , 0,ss.length() , Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        requestNewClaim =(TextView) newClaimRequest.findViewById(R.id.requestHere);
        requestNewClaim.setText(ss.toString());
        requestNewClaim.setMovementMethod(LinkMovementMethod.getInstance());

        return newClaimRequest;
    }

}

Layout XML

        <LinearLayout
            android:id="@+id/view2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:orientation="vertical">



            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/requestHere"
                android:clickable="true"
                android:textSize="20dp"
                android:textStyle="bold"
                android:textColor="#000000"
                android:layout_marginLeft="20dp"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"/>

        </LinearLayout>

Solution

  • If LoginActivity is a fragment class then it would be okay if you use setOnClickListener on textview. But for fragment change you have to change Intent to fragmentTransaction,

    Use something like,

    textview.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                        getFragmentManager().beginTransaction().replace(R.id.container, new LoginActivity() ).addToBackStack("").commit();
    });
    

    But, if you want to use SpannableString then do like this,

    ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View textView) {
                getFragmentManager().beginTransaction().replace(R.id.container, new LoginActivity() ).addToBackStack("").commit();
            }
        };
    

    Here, R.id.container is the fragment of your main activity layout in which new view will be replaced.