1

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>
Community
  • 1
  • 1
Tangle
  • 51
  • 1
  • 11

4 Answers4

2

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.

Exigente05
  • 2,161
  • 3
  • 22
  • 42
  • Can you please tell me ,did I do any other mistake any where?I put this code inside the onClick() method. But there was nothing happen. I run this code and click on the text,but nothing happen. – Tangle Jun 22 '15 at 08:13
  • no,But nothing happen. It gives a feeling like that I clicked on normal textView.Can you please tell me that am I rightly set the TextView as clickable?:( – Tangle Jun 22 '15 at 08:26
  • R u using SpannableString or just simple textView? – Exigente05 Jun 22 '15 at 08:30
  • yeah, I was just keep checking for any other mistake! But its nice that, finally you are able to make it working. :) – Exigente05 Jun 22 '15 at 08:39
1

You can not call Fragment via Intent. You need to replace your current fragment with new one.

Shoeb Siddique
  • 2,805
  • 1
  • 22
  • 42
  • _"You can not call Fragment via Intent..."_ I don't see any calling of fragments via `Intent`. The only `Intent` in OP's codes is for launching another activity that it's fine. – frogatto Jun 22 '15 at 08:53
1

you have to replace your fragment A to B, use this code

FragmentManager fm = getActivity.getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Fragment fragment = new FragmentB();
ft.replace(R.id.activity_main_content_fragment,fragment);
ft.commit();

In This code replace R.id.youframelayoutid, then it workable

If its code useful so please mark me my answer. :)

Mayank Sugandhi
  • 397
  • 1
  • 7
  • 22
  • Can you please tell me ,did I do any other mistake any where?I put this code inside the onClick() method. But there was nothing happen. I run this code and click on the text,but nothing happen. – Tangle Jun 22 '15 at 08:04
  • @Tangle :- in your code please find id of request here is first then on click functionality implement, i hope it helps. – Mayank Sugandhi Jun 22 '15 at 08:22
1

As an usual manner you should put a FrameLayout in your Activity's layout XML file. This FrameLayout acts like a placeholder for your Fragments. In other words, Fragment A can be pasted there, so is for Fragment B.

Okay, suppose you've added a FrameLayout in you activity's layout file. Pasting fragments on it and also replacing fragments should be done by the FragmentManager. Hence, you should grab a reference to a FragmentManger in your activity class. For getting this done ...

  • If you use Android Support Libraries, you should get a reference to FragmentManger by getSupportFragmentManager()

  • Otherwise, getFragmentManager()

In Android adding fragments and also replacing them are done in the form of a transaction. Thus you should inform the fragment manager that you would like to do a transaction. This could be done via:

FragmentTransaction transaction = fragmentManger.beginTransaction();

Now, you can apply all what you want on this transaction object. For instance, Adding a fragment could be done like this:

transaction.add(R.id.placeholder, new FragmentA() , "tag-frag-A");

For replacing ...

transaction.replace(R.id.placeholder, new FragmentB(), "tag-frag-B");

After you're done, you commit that transaction by calling

transaction.commit();

Notes:

  • FragmentManager acts like a container for your added fragments. You can search through your added fragments by their tag.

  • Device rotation does not remove added fragments in the FragmentManager. Thus in your onCreate method take care you've added any fragments only once.

  • You can add a Transaction to the back stack. This means that whenever user clicks on the Android back button this fragment will be removed from the state stack and also will be rolled back.

frogatto
  • 28,539
  • 11
  • 83
  • 129
  • Can you please tell me ,did I do any other mistake any where?I put this code inside the onClick() method. But there was nothing happen. I run this code and click on the text,but nothing happen. – Tangle Jun 22 '15 at 08:13