Skip to content

Instantly share code, notes, and snippets.

@Mariuxtheone
Last active December 26, 2015 12:39
Show Gist options
  • Save Mariuxtheone/7152335 to your computer and use it in GitHub Desktop.
Save Mariuxtheone/7152335 to your computer and use it in GitHub Desktop.
Use a ViewSwitcher to put two widgets where only one can fit. This is useful if you want to save space in your UI but don't want to give up multiple features.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ViewSwitcher
android:id="@+id/viewSwitcher"
android:layout_width="fill_parent"
android:layout_height="100dp" >
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/btnText1" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
</ListView>
</ViewSwitcher>
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/commutateViewSwitcherButton" />
</LinearLayout>
public class OnClickListenerActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
commutateViewSwitcherButton.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
final ViewSwitcher viewSwitcher = (ViewSwitcher) findViewById(R.id.viewSwitcher);
// Create and Animations on the ViewSwitcher to make it beautifully slide
Animation animationOut = AnimationUtils.loadAnimation(
getApplicationContext(),android.R.anim.slide_out_right);
Animation animationIn = AnimationUtils.loadAnimation(
getApplicationContext(), android.R.anim.slide_in_left);
//set Animations
viewSwitcher.getNextView().startAnimation(animationIn);
viewSwitcher.setOutAnimation(animationOut);
// Show next widget
viewSwitcher.showNext();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment