Howdy

First of all english is not my first language but i will try my best.

Also... i am pretty sure my title choice was not the best so sorry for that.

Basically what i wanted to do is a menu with three ImageButtons but there is a tricky part (tricky for me at least) since every time i press one button that same button changes image (to a colored version instead of a grayed out image) and the other two change as well from colored version of their respective images to grayed out ones, actually only one of the other two will change since the purpose of this is to be able to activate only one at a time so it would not be possible to have the other two active at the same time.

Notice that this is not a menu on the top right corner but just a set of three ImageButtons on a activity or Fragment.

I already tried a lot of stuff to make that happen but so far no luck but i think i know why though i can't find a workaround for this since i am actually new in android dev.

what i tried was inside the setOnClickListener of any of those buttons such as:

eventsButton.setOnClickListener(
  new View.OnClickListener() {
     public void onClick(View view) {
        ImageButton eventsButton = (ImageButton) view.findViewById(R.id.eventsButton);
        eventsButton.setBackgroundResource(R.drawable.events_icon_active);
        eventsButton.setClickable(false);
     }
  }
);

i tried to add the functions to change the other imageButtons as well like:

eventsButton.setOnClickListener(
  new View.OnClickListener() {
     public void onClick(View view) {
        ImageButton eventsButton = (ImageButton) view.findViewById(R.id.eventsButton);
        eventsButton.setBackgroundResource(R.drawable.events_icon_inactive);
        eventsButton.setClickable(false);

        ImageButton contactsButton = (ImageButton) view.findViewById(R.id.contactsButton);
        contactsButton.setBackgroundResource(R.drawable.contacts_icon_inactive);
        contactsButton.setClickable(true);

        ImageButton interestsButton = (ImageButton) view.findViewById(R.id.interestsButton);
        interestsButton.setBackgroundResource(R.drawable.interests_icon_inactive);
        interestsButton.setClickable(true);
     }
  }
);

and i repeated that three time, always setting the other buttons clickable and setting their images to the inactive one (the grayed out one), also setting the button i click as no longer clickable.

But from what i gather i cant do any references to any other buttons inside the eventsButton.setOnClickListener like the buttons interestsButton or contactsButton, it will crash the app as soon as i touch any of those three buttons with the following error message:

Attempt to invoke virtual method 'void android.widget.ImageButton.setBackgroundResource(int)' on a null object reference


And it always point to the first line where i make a reference to another button other then the one used to start the setOnClickListener.

If you can just point me in the right direction i would be tremendously grateful.

All the best