1

Is it possible to remove view from screen? Android

For example I have some layout, which contains 3 layouts;

layout_1 layout_2 layout_3

Is it possible to click on layout_1, and move it away from screen, and you'll receive visible

layout_2 layout_3

Thanks in advance.

upd: The problem is, that I need to slide off this view smoothly. Looks like a sidebar in Facebook. But another thing is, that other two layouts after removing layout_1 should be stretched to fit parent layout.

Taras
  • 2,526
  • 3
  • 33
  • 63
  • What do you mean by smoothly. It fading out/sliding off screen? – IAmGroot Nov 22 '12 at 19:41
  • @Doomsknight sorry for poor English. Yes, I'm talking about sliding off. I'll update the question. Thanks – Taras Nov 22 '12 at 19:42
  • I dont know much about animating views Im afraid, but I believe this is what you are looking for. http://developer.android.com/reference/android/view/animation/TranslateAnimation.html Here is an SO question/answer http://stackoverflow.com/questions/4213393/translate-animation – IAmGroot Nov 22 '12 at 19:46

4 Answers4

5

Set the view an id.

then do

(LinearLayout) linLayout = (LinearLayout) findViewById(R.id.layout_1);
linLayout.setVisibility(View.GONE);

(Cast it to what ever type it is.)

Or do it directly with:

findViewById(R.id.layout_1).setVisibility(View.GONE);

Edit: Following your new info and the question/answer I have commented:

(LinearLayout) linLayout = (LinearLayout) findViewById(R.id.layout_1);
Animation animation = new TranslateAnimation(0, 500,0, 0); //May need to check the direction you want.
animation.setDuration(1000);
animation.setFillAfter(true);
linLayout.startAnimation(animation);
linLayout.setVisibility(View.GONE);

You might need something more advanced if you want the other 2 to take the space up gradually too.

IAmGroot
  • 13,760
  • 18
  • 84
  • 154
  • thanks for your answer, but there's one problem. I've described it in update. – Taras Nov 22 '12 at 19:39
  • @Division_Bell as long as you use `View.GONE` and your views are weighted, it will fill the space. Dont use `View.INVISIBLE` because it will still take space up. – IAmGroot Nov 22 '12 at 19:42
  • How views will be weighted? I need layout_2 to be the same size as before, and layout_3 streched to fit the free space. – Taras Nov 22 '12 at 19:47
  • Then just leave layout_2 as wrap content, or what ever it is at the moment, and change layout_3 to `match_parent` That should be enough. Animations: stackoverflow.com/questions/4213393/translate-animation – IAmGroot Nov 22 '12 at 19:49
  • @Division_Bell Ive added some animation code from the other question/answer. Im no expert on it though, so it might need tweaking. – IAmGroot Nov 22 '12 at 19:58
1

Yes, simply use removeView().

Sam
  • 86,580
  • 20
  • 181
  • 179
  • thanks for your answer, I've didn't explained in details, so please check update. – Taras Nov 22 '12 at 19:38
  • Ok, you can use the [SlidingDrawer](http://developer.android.com/reference/android/widget/SlidingDrawer.html) class, animate this transition yourself, or use can rewrite everything to use Fragments to take advantage of its transitions. – Sam Nov 22 '12 at 19:47
0

I'm assuming that you are wanting to completely change the view and function of the page. In this case you should either be starting a new activity or look into fragments and the fragment manager.

EDIT

Ok if you have the different Views as 3 different fragments you can use the fragment manager to inflate and remove them like so

FragmentTransaction ft_main = getFragmentManager().beginTransaction();
//note the following line sets the fragment Tag to "layout_1_fragment" you can
//use this to retrive and dismiss it later
ft_main.replace(R.id.WhateverViewYoureInflatingInto, new Layout_1_Fragment(), "layout_1_fragment");
ft_main.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft_main.commit();

to remove the fragment do the following:

FragmentTransaction ft_main = getFragmentManager().beginTransaction();
ft_main.remove(getFragmentManager().findFragmentByTag("layout_1_fragment"));
ft_main.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft_main.commit();

using this code you can dynamically inflate and remove fragments from your screen.

Note: the fragment isn't destroyed when you remove it so if you want to reattach the fragment I suggest checking to see if

getFragmentManager().findFragmentByTag("layout_1_fragment") == null

and if not reusing the existing fragment

chris-tulip
  • 1,840
  • 1
  • 15
  • 22
  • In my case I have layout_1 as a Fragment, layout_2 as a simple View and layout_3 as a Fragment as well. – Taras Nov 22 '12 at 19:41
0

Use an OnClickListener and View.setVisibility(int).

nhaarman
  • 98,571
  • 55
  • 246
  • 278