Reputation: 2180
I have created 2 different layouts for the same Fragment. One is for Portrait
and second for Landscape
. All the buttons ids are the same.
My fragment implements onClickListener
.
When I start my application, the onClick is working great. Doesn't matter if I start in landscape or portrait. When I change the orientation, the layout switches, but the onClick
isn't working anymore. It only works at start up.
public class Navigation extends Fragment implements View.OnClickListener {
private ImageButton addButton;
private ImageButton viewButton;
private ImageButton albumButton;
private ImageButton infoButton;
private Communicator comm;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.navigation_fragment_layout, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
addButton = (ImageButton) getActivity().findViewById(R.id.add_button);
addButton.setOnClickListener(this);
viewButton = (ImageButton) getActivity().findViewById(R.id.view_button);
viewButton.setOnClickListener(this);
albumButton = (ImageButton) getActivity().findViewById(R.id.album_button);
albumButton.setOnClickListener(this);
infoButton = (ImageButton) getActivity().findViewById(R.id.info_button);
infoButton.setOnClickListener(this);
comm = (Communicator) getActivity();
}
@Override
public void onStart() {
super.onStart();
}
@Override
public void onClick(View v) {
comm.onFragmentTouch(v.getId());
}
}
Upvotes: 1
Views: 341
Reputation: 1516
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
addButton = (ImageButton) getActivity().findViewById(R.id.add_button);
addButton.setOnClickListener(this);
viewButton = (ImageButton) getActivity().findViewById(R.id.view_button);
viewButton.setOnClickListener(this);
albumButton = (ImageButton) getActivity().findViewById(R.id.album_button);
albumButton.setOnClickListener(this);
infoButton = (ImageButton) getActivity().findViewById(R.id.info_button);
infoButton.setOnClickListener(this);
comm = (Communicator) getActivity();
}
here you are taking views from getActivity(); why youa re getting them from getActivity(). Take them from the below way
public class Navigation extends Fragment implements View.OnClickListener {
private ImageButton addButton;
private ImageButton viewButton;
private ImageButton albumButton;
private ImageButton infoButton;
private Communicator comm;
private View view;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.navigation_fragment_layout, container, `false);`
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
addButton = (ImageButton) view.findViewById(R.id.add_button);
addButton.setOnClickListener(this);
viewButton = (ImageButton) view.findViewById(R.id.view_button);
viewButton.setOnClickListener(this);
albumButton = (ImageButton) view.findViewById(R.id.album_button);
albumButton.setOnClickListener(this);
infoButton = (ImageButton) view.findViewById(R.id.info_button);
infoButton.setOnClickListener(this);
comm = (Communicator) this;
}
Let me know this works or not
Upvotes: 0