Is there a way to hit a button, and have the same layout view but flipped horizontally/mirrored ? (with all it's components) or the only way is to create the same layout and rearrange the components?
-
I only know about rotation: http://stackoverflow.com/questions/1930963/rotating-a-view-in-android – fersarr Mar 07 '14 at 22:40
4 Answers
You can flip a layout by setting the View property scaleX
to -1. So View#setScaleX(1f)
is normal, and View#setScaleX(-1f)
is flipped (visually).
To do it pre-Honeycomb, you can use scale properties in the general Animation library. Just set the animation duration to 0 for immediate look (note, the elements will still be in the normal locations).

- 35,865
- 9
- 108
- 95
You can use setRotationY(float)
method to flip over the Y axis. If you would call yourView.setRotationY(180f)
it would flip it horizontally. Similarly, yourView.setRotationX(180f)
would flip it vertically.
Please note that it flips on a 3D space, so if you'd flip for 90f, your view will practically be invisible.

- 445
- 6
- 10
-
rotating instead of flipping view will also result in inside views to be vertically flipped. – Mudit Singh Sengar Nov 22 '19 at 12:40
if you have a button that flips the view horizontally or vertically, you should add 180f to the view's rotation so that the button flips the view on the first click and restore the rotation on the second click.
Because 360 rotation is equal to 0 rotation.
TextView myTextView=findViewById(R.id.my_text_view);
button.setOnClickListener(new View.OnClickListener(){
@Override
onClick(View v){
myTextView.setRotationX(myTextView.getRotationX()+180f); //horizontal flip
// myTextView.setRotationY(myTextView.getRotationY()+180f) //vertical flip
}
}

- 1,173
- 1
- 13
- 23