I have a fragment which I am using as a navigational menu between my activities.
Each of the icons is in a seperate linearlayout with its own ID. I'm trying to implement an onclick listener in the fragment for each linearlayout so that when it is clicked the appropriate activity is opened and the background of the linear layout as well as the image in the imageview are changed to signify the click. However when I click on the linearlayout nothing happens. I already set the clickable attribute to true so that is not the issue. This is my code:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_menu, container, false);
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
LinearLayout betslayout = (LinearLayout) this.getView().findViewById(R.id.betnowlayout);
betslayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LinearLayout layout = (LinearLayout) v;
layout.setBackgroundResource(R.drawable.selected);
ImageView image = (ImageView) layout.findViewById(R.id.betnowimageview);
image.setImageResource(R.drawable.footballsell);
LinearLayout parent = (LinearLayout) v.getParent();
LinearLayout mybetslayout = (LinearLayout) parent.findViewById(R.id.viewbetslayout);
LinearLayout rankingslayout = (LinearLayout) parent.findViewById(R.id.rankingslayout);
LinearLayout coinfrenzylayout = (LinearLayout) parent.findViewById(R.id.coinfrenzylayout);
LinearLayout shoplayout = (LinearLayout) parent.findViewById(R.id.shoplayout);
mybetslayout.setBackgroundResource(R.drawable.notselected);
rankingslayout.setBackgroundResource(R.drawable.notselected);
coinfrenzylayout.setBackgroundResource(R.drawable.notselected);
shoplayout.setBackgroundResource(R.drawable.notselected);
ImageView mybetsimage = (ImageView) mybetslayout.findViewById(R.id.betnowimageview);
ImageView rankingsimage = (ImageView) rankingslayout.findViewById(R.id.rankingsimageview);
ImageView coinfrenzyimage = (ImageView) coinfrenzylayout.findViewById(R.id.coinfrenzyimageview);
ImageView shopimage = (ImageView) shoplayout.findViewById(R.id.shopimageview);
mybetsimage.setImageResource(R.drawable.chipp);
rankingsimage.setImageResource(R.drawable.medal);
coinfrenzyimage.setImageResource(R.drawable.clover);
shopimage.setImageResource(R.drawable.moneybag);
Intent i = new Intent(getActivity(),AllGameslistActivity.class);
startActivity(i);
getActivity().finish();
}
});
}
The problem was in this line of code
return inflater.inflate(R.layout.fragment_menu, container, false);
It should instead be :
return myfragment;
This is the proper way to do this: