title: How to make a right to left animation in a layout
body: <p>I am involved with a slide right to left problem. I created it by "hide" and "visible" layout on button clicked. But now I want an animation so that when I clicked on button, a "right to left" animation will happen and when I click again on button, same layout will be "left to right" animation and work as slide layout.</p>
  
  <p>code</p>
  
  <pre><code>final RelativeLayout rightlayout = (RelativeLayout) findViewById(R.id.relativelayout2);
  rightlayout.setVisibility(View.GONE);
  
  Button rightbtn = (Button) findViewById(R.id.righttoleft);
  
  rightbtn.setOnClickListener(new View.OnClickListener() {
  
      @Override
      public void onClick(View v) {
          System.out.println("clickecd");
          if(keyright == 0) {
              //rightlayout.setVisibility(View.VISIBLE);
              keyright++;
              slideToLeft(rightlayout);
  
          } else
          {
              //rightlayout.setVisibility(View.GONE);
              TranslateAnimation animate = new TranslateAnimation(0,rightlayout.getWidth(),0,0);
              animate.setDuration(1000);
              animate.setFillAfter(true);
              rightlayout.startAnimation(animate);
              keyright--;
          }
  
      }
  });
  
  // To animate view slide out from left to right
  public void slideToRight(View view){
  TranslateAnimation animate = new TranslateAnimation(0,view.getWidth(),0,0);
  animate.setDuration(500);
  animate.setFillAfter(true);
  view.startAnimation(animate);
  
  }
  // To animate view slide out from right to left
  public void slideToLeft(View view){
  TranslateAnimation animate = new TranslateAnimation(0,-view.getWidth(),0,0);
  animate.setDuration(500);
  animate.setFillAfter(true);
  view.startAnimation(animate);
  
  }
  </code></pre>
  
  <p>xml:</p>
  
  <pre><code>&lt;RelativeLayout
      android:id="@+id/rightsidemenu"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentRight="true"
      android:layout_below="@+id/frameLayout1"
      android:gravity="right"
      android:orientation="vertical" &gt;
  
      &lt;Button
          android:id="@+id/righttoleft"
          style="?android:attr/buttonStyleSmall"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginTop="100dp"
          android:background="@drawable/right_switcher_expanded" /&gt;
  
      &lt;RelativeLayout
          android:id="@+id/relativelayout2"
          android:layout_width="50dp"
          android:layout_height="match_parent"
          android:layout_alignParentRight="true"
          android:layout_toRightOf="@+id/righttoleft"
          android:background="#EDEEF3"
          android:orientation="vertical" &gt;
  
          &lt;LinearLayout
              android:layout_width="wrap_content"
              android:layout_height="match_parent"
              android:layout_marginLeft="10dp"
              android:layout_marginRight="10dp"
              android:orientation="vertical"
              android:padding="5dp" &gt;
  
              &lt;RelativeLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_gravity="center"
                  android:layout_marginTop="10dp"
                  android:background="#ffffff"
                  android:gravity="right"
                  android:orientation="vertical" &gt;
  
                  &lt;FrameLayout
                      android:id="@+id/titleframe"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:background="#F4F4F4"
                      android:orientation="horizontal" &gt;
  
                      &lt;TextView
                          android:id="@+id/firsttitle"
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:layout_gravity="center_vertical"
                          android:text="Job and Qualification"
                          android:textColor="#56A91E" /&gt;
                  &lt;/FrameLayout&gt;
  
                  &lt;FrameLayout
                      android:id="@+id/firstframe1"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:layout_below="@+id/titleframe"
                      android:layout_margin="10dp"
                      android:orientation="horizontal" &gt;
  
                      &lt;TextView
                          android:id="@+id/textView81"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:text="Highest qualification" /&gt;
  
                      &lt;TextView
                          android:id="@+id/higesttxt"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:layout_gravity="right"
                          android:text="highest qualification" /&gt;
                  &lt;/FrameLayout&gt;
  
                  &lt;FrameLayout
                      android:id="@+id/thirdff"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:layout_below="@+id/firstframe1"
                      android:layout_margin="10dp" &gt;
  
                      &lt;TextView
                          android:id="@+id/textView10"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:text="Occupation" /&gt;
  
                      &lt;TextView
                          android:id="@+id/occupationtxt"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:layout_gravity="right"
                          android:text="occupation" /&gt;
                  &lt;/FrameLayout&gt;
  
                  &lt;FrameLayout
                      android:id="@+id/thirdfff"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:layout_below="@+id/thirdff"
                      android:layout_marginBottom="20dp"
                      android:layout_marginLeft="10dp"
                      android:layout_marginRight="10dp"
                      android:layout_marginTop="10dp" &gt;
  
                      &lt;TextView
                          android:id="@+id/textView100"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:text="CTC (Income)" /&gt;
  
                      &lt;TextView
                          android:id="@+id/incometxt"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:layout_gravity="right"
                          android:text="income" /&gt;
                  &lt;/FrameLayout&gt;
              &lt;/RelativeLayout&gt;
  
              &lt;RelativeLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_gravity="center"
                  android:layout_marginTop="10dp"
                  android:background="#ffffff"
                  android:gravity="right"
                  android:orientation="vertical" &gt;
  
                  &lt;FrameLayout
                      android:id="@+id/titleframe"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:background="#F4F4F4"
                      android:orientation="horizontal" &gt;
  
                      &lt;TextView
                          android:id="@+id/firsttitle"
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:layout_gravity="center_vertical"
                          android:text="Family Info"
                          android:textColor="#56A91E" /&gt;
                  &lt;/FrameLayout&gt;
  
                  &lt;FrameLayout
                      android:id="@+id/firstframe11"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:layout_below="@+id/titleframe"
                      android:layout_margin="10dp"
                      android:orientation="horizontal" &gt;
  
                      &lt;TextView
                          android:id="@+id/textView811"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:text="Have children" /&gt;
  
                      &lt;TextView
                          android:id="@+id/havechildrntxt"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:layout_gravity="right"
                          android:text="have children" /&gt;
                  &lt;/FrameLayout&gt;
  
                  &lt;FrameLayout
                      android:id="@+id/secondfff"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:layout_below="@+id/firstframe11"
                      android:layout_margin="10dp" &gt;
  
                      &lt;TextView
                          android:id="@+id/textView91"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:text="Father Alive" /&gt;
  
                      &lt;TextView
                          android:id="@+id/fathrtxt"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:layout_gravity="right"
                          android:text="Father Alive" /&gt;
                  &lt;/FrameLayout&gt;
  
                  &lt;FrameLayout
                      android:id="@+id/thirdfff"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:layout_below="@+id/secondfff"
                      android:layout_margin="10dp" &gt;
  
                      &lt;TextView
                          android:id="@+id/textView101"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:text="Mother Alive" /&gt;
  
                      &lt;TextView
                          android:id="@+id/mothertxt"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:layout_gravity="right"
                          android:text="Mother alive" /&gt;
                  &lt;/FrameLayout&gt;
  
                  &lt;FrameLayout
                      android:id="@+id/thirdffff"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:layout_below="@+id/thirdfff"
                      android:layout_marginBottom="10dp"
                      android:layout_marginLeft="10dp"
                      android:layout_marginRight="10dp"
                      android:layout_marginTop="10dp" &gt;
  
                      &lt;TextView
                          android:id="@+id/textView103"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:text="Brother" /&gt;
  
                      &lt;TextView
                          android:id="@+id/brothercounttxt"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:layout_gravity="right"
                          android:text="Brother" /&gt;
                  &lt;/FrameLayout&gt;
  
                  &lt;FrameLayout
                      android:id="@+id/thirdffff3"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:layout_below="@+id/thirdffff"
                      android:layout_marginLeft="10dp"
                      android:layout_marginRight="10dp" &gt;
  
                      &lt;TextView
                          android:id="@+id/textView104"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:text="Sister" /&gt;
  
                      &lt;TextView
                          android:id="@+id/sistercounttxt"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:layout_gravity="right"
                          android:text="Sister" /&gt;
                  &lt;/FrameLayout&gt;
              &lt;/RelativeLayout&gt;
          &lt;/LinearLayout&gt;
      &lt;/RelativeLayout&gt;
  </code></pre>