[android-developers] how to deflate/hide(invisible) ViewStub from Menu (onOptionsItemSelected)?

2011-11-04 Thread Abhi
I keep getting NullPointerException trying to deflate or make
invisible the ViewStub from my UI. I just wanted to be sure I am doing
it right.

I am inflating my ViewStub in onItemLongClick method of GalleryView by
doing the following:

@Override
public boolean onItemLongClick(AdapterView? arg0, View
viu, int arg2,
long arg3) {
Toast.makeText(GalleryView.this, New item added to
Favorites, Toast.LENGTH_SHORT).show();


favsCount++;

   //checking to see if ViewStub is already inflated or
not
if(!stubvis){
stub = (ViewStub) findViewById(R.id.stub1);
stub.inflate();
stubvis = true;
trayUP = true;
}

return true;
}

});

and then in onPrepareOptionsMenu() I am adding the menu item based on
the visibility of ViewStub. If inflated and visible, I create a menu
item to hide it, otherwise, a menu item to show/make visible.

@Override

public boolean onPrepareOptionsMenu(Menu menu) {

menu.clear();


if(trayUP) {

menu.add(0, HIDETRAY, 0, Hide Favorites Tray);

} else {

menu.add(0, SHOWTRAY, 0, Show Favorites Tray);

}

return super.onPrepareOptionsMenu(menu);

}
Next, in onOptionsItemSelected(), I am writing the two cases based on
the menu item selection. Case 1 when the tray is not visible, so I
make it visible. Case 2 when it is visible, so I hide it by doing the
following:

@Override
public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {

case HIDETRAY:

Log.v(TAG, Hiding Favs Tray);
findViewById(R.id.stub1).setVisibility(View.GONE);
trayUP = false;

case SHOWTRAY:

Log.v(TAG, Showing Favs Tray);
findViewById(R.id.stub1).setVisibility(View.VISIBLE);
trayUP = true;

}

return true;
}

I know I am making a silly mistake somewhere. And my mind is too
saturated to think straight at the moment. Need help :(

Thanks,

Ab

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


Re: [android-developers] how to deflate/hide(invisible) ViewStub from Menu (onOptionsItemSelected)?

2011-11-04 Thread Kostya Vasilyev

Inflating a view stub has two important properties:

1) It's irreversible: once inflated, the stub's content is always there
2) The stub's content replaces the stub.

Sounds like you need to replace the stub with include and toggle its 
contents' visibility as needed.


-- Kostya

05.11.2011 2:07, Abhi пишет:

I keep getting NullPointerException trying to deflate or make
invisible the ViewStub from my UI. I just wanted to be sure I am doing
it right.

I am inflating my ViewStub in onItemLongClick method of GalleryView by
doing the following:

@Override
 public boolean onItemLongClick(AdapterView?  arg0, View
viu, int arg2,
 long arg3) {
 Toast.makeText(GalleryView.this, New item added to
Favorites, Toast.LENGTH_SHORT).show();


 favsCount++;

//checking to see if ViewStub is already inflated or
not
 if(!stubvis){
 stub = (ViewStub) findViewById(R.id.stub1);
 stub.inflate();
 stubvis = true;
 trayUP = true;
 }

 return true;
 }

 });

and then in onPrepareOptionsMenu() I am adding the menu item based on
the visibility of ViewStub. If inflated and visible, I create a menu
item to hide it, otherwise, a menu item to show/make visible.

@Override

public boolean onPrepareOptionsMenu(Menu menu) {

menu.clear();


if(trayUP) {

menu.add(0, HIDETRAY, 0, Hide Favorites Tray);

} else {

menu.add(0, SHOWTRAY, 0, Show Favorites Tray);

}

return super.onPrepareOptionsMenu(menu);

}
Next, in onOptionsItemSelected(), I am writing the two cases based on
the menu item selection. Case 1 when the tray is not visible, so I
make it visible. Case 2 when it is visible, so I hide it by doing the
following:

@Override
public boolean onOptionsItemSelected(MenuItem item) {

 switch (item.getItemId()) {

 case HIDETRAY:

 Log.v(TAG, Hiding Favs Tray);
 findViewById(R.id.stub1).setVisibility(View.GONE);
 trayUP = false;

 case SHOWTRAY:

 Log.v(TAG, Showing Favs Tray);
 findViewById(R.id.stub1).setVisibility(View.VISIBLE);
 trayUP = true;

 }

 return true;
}

I know I am making a silly mistake somewhere. And my mind is too
saturated to think straight at the moment. Need help :(

Thanks,

Ab



--
Kostya Vasilyev

--
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en