Like I have in one of my projects:
a) Create animations
public Animation getEditModeAnimation() {
TranslateAnimation animation = new TranslateAnimation(0,
convertDpToPixel(57, this.context), 0, 0);
animation.setDuration(300);
animation.setFillAfter(true);
return animation;
}
public Animation getNonEditModeAnimation() {
TranslateAnimation animation = new TranslateAnimation(
convertDpToPixel(57, this.context), 0, 0, 0);
animation.setDuration(300);
animation.setFillAfter(true);
return animation;
}
b) Combine with your UI
component:
Animation editAnimation = getEditModeAnimation();
yourUIComponent.setAnimation(editAnimation);
Animation nonEditAnimation = getNonEditModeAnimation();
yourUIComponent.setAnimation(nonEditAnimation);
c) Helper method (if needed): //simply converts dp to px - pretty convenient
public static float convertDpToPixel(float dp, Context context) {
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * (metrics.densityDpi / 160f);
return px;
}
d) Plus some flags which will denote the state of a component, so you will know when to use one or another mode
public enum MODES {
EDIT_MODE(1), NON_EDIT_MODE(0);
private int mode_identifier;
private MODES(int mode_identifier) {
this.mode_identifier = mode_identifier;
}
}
e) And a global variable to hold the current state
private MODES yourUIComponentState;
f) To create some UI component dynamically:
Button yourUIComponent = new Button(yourContextReferenceHere);
TextView yourUIComponent = new TextView(yourContextReferenceHere);
RelativeLayout(yourUIComponent) = new RelativeLayout(yourContextReferenceHere);
....
g) To create some UI component in XML:
<Button android:id="@+id/yourUIComponent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView android:id="@+id/yourUIComponent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
h) To reference UI components created inside an Activity:
Button yourUIComponent = (Button) findViewById(R.id.yourUIComponent);
TextView yourUIComponent = (TextView) findViewById(R.id.yourUIComponent);