-1

This is the code for the fragment. Please help me place a button and onClick it should go to the respective activity

class boards extends Fragment {

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.boards, container, false);

}

}
Vishal Vaishnav
  • 3,346
  • 3
  • 26
  • 57
Siddarth
  • 1
  • 1

2 Answers2

0

Inside onViewCreated method:

View view = inflater.inflate(R.layout.boards, container, false);
Button button = (Button) view.findViewById(R.id.buttonId);
button.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v){
        // do something
        startActivity(new Intent(getActivity(), YourActivity.class));
    } 
}); 
return view;
Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53
0

you need to implement a call back interface

interface code :

public interface FragmentCallback {
   void changeActivity();
}

inside fragment:

FragmentCallback mListener;

void setListener(FragmentCallback listener){
    mListener=listener;
}

inside activity:

public class Activity extends AppCompatActivity implements FragmentCallback{

Boards fragment=new Boards();  //class name shoud be capital i.e Boards

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);
        fragment.setListener(this);
    }

@Override
void changeActivity(){
   Intent intent =new Intent(this,newActivity.class);
   startActivity(intent);
}
}

use the below code to change activity from fragment :

if(mListener!=null)
   mListener.changeActivity()

below code will do the task