Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move from one fragment to another fragment on click of an ImageView in Android?

Tags:

I have an ImageView. I want to move from one fragment to another fragment on a click of an Imageview, the same way like we can move from one activity to another using

Intent i=new Intent(MainActivity.this,SecondActivity.class); startActivity(i); 

How can I do this? Can anyone explain to me step by step?

My codes are as follows:

mycontacts.class

public class mycontacts extends Fragment {     public mycontacts() {     }      @Override     public View onCreateView(LayoutInflater inflater, ViewGroup container,         Bundle savedInstanceState) {         final View v = super.getView(position, convertView, parent);         ImageView purple=(ImageView)v.findViewById(R.id.imageView1);         purple.setOnClickListener(new OnClickListener() {              @Override             public void onClick(View v) {             // TODO Auto-generated method stub             //how to go to tasks fragment from here???             }         });         return view;      }  } 

tasks.class

public class tasks extends Fragment {     public tasks() {      }      @Override     public View onCreateView(LayoutInflater inflater, ViewGroup container,         Bundle savedInstanceState) {          View view = inflater.inflate(R.layout.fragment_layout_one, container,             false);          return view;     } } 
like image 996
anu_r Avatar asked Apr 22 '14 06:04

anu_r


People also ask

How do I move fragments from one fragment to another in Kotlin?

Step 1 − Create a new project in Android Studio, go to File ⇉ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. Step 3 − Create two FragmentActivity and add the codes which are given below.

What is the use of FragmentManager in Android?

FragmentManager is the class responsible for performing actions on your app's fragments, such as adding, removing, or replacing them, and adding them to the back stack.


1 Answers

purple.setOnClickListener(new OnClickListener() {     @Override     public void onClick(View v) {         Fragment fragment = new tasks();         FragmentManager fragmentManager = getActivity().getSupportFragmentManager();         FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();         fragmentTransaction.replace(R.id.content_frame, fragment);         fragmentTransaction.addToBackStack(null);         fragmentTransaction.commit();     } }); 

You write the above code...there we are replacing R.id.content_frame with our fragment.

like image 107
GvSharma Avatar answered Sep 18 '22 08:09

GvSharma