The behavior you are seeing is because of the way your onCreate()
is set up:
setContentView(R.layout.activity_main);
final LayoutInflater factory = getLayoutInflater();
final View v = factory.inflate(R.layout.activity_main, null);
When setContentView(int)
is called, R.layout.activity_main
is inflated and your activity's view is set.
Following this, you are inflating R.layout.activity_main
again:
final LayoutInflater factory = getLayoutInflater();
final View v = factory.inflate(R.layout.activity_main, null);
View v
is not the same view as the one already set by setContentView(R.layout.activity_main, null)
. Now, using v
, you initialize components:
textRequest = (TextView)v.findViewById(R.id.textRequest);
playButton = (ImageView)v.findViewById(R.id.playButton);
friendsButton = (ImageView)v.findViewById(R.id.friendsButton);
Again, these widgets do not correspond to the widgets you see in your activity. So, when you do the following:
friendsButton.setVisibility(View.INVISIBLE);
it does nothing to the activity view.
I hope you understand the problem here. Now, to the solution. There are two possible ways to fix this:
- Find widgets in the view set by
setContentView(R.layout.activity_main)
:
Remove:
final LayoutInflater factory = getLayoutInflater();
final View v = factory.inflate(R.layout.activity_main, null);
Use:
setContentView(R.layout.activity_main);
// Notice, we're not using v.findViewById(....)
textRequest = (TextView) findViewById(R.id.textRequest);
playButton = (ImageView) findViewById(R.id.playButton);
....
....
friendsButton = (ImageView) findViewById(R.id.friendsButton);
friendsButton.setVisibility(View.INVISIBLE); // Will work now
- Defer the call to
setContentView(R.layout.activity_main)
until the view has been inflated:
Keep your code as it is, but remove setContentView(R.layout.activity_main);
from the beginning. Place it after setting up all the widgets.
....
....
friendsButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
onFriendsButtonTouched();
return false;
}
});
// Use the inflated View `v`
setContentView(v);