1

I have a buttonView1 and a customView1 that only draws a small circle. in my main_layout only buttonView1 is visible.

How can I programmatically display customView1 in center of buttonView1 when button is clicked.

attention: we dont want to change button background. we want to show a new view (customView or any other view like editText) on another view (button) when button is pressed.


layout code:

<RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/buttonView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="102dp"
        android:layout_marginTop="86dp"
        android:text="@string/button_text" />

</RelativeLayout>

activity code

    ///...
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mButton = (Button)findViewById(R.id.buttonView1);
            mButton.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.buttonView1:
                // do sth here
                break;

            default:
                break;
            }

        }
///...

2 Answers2

0
RelativeLayout.LayoutParams layoutParams = 
    (RelativeLayout.LayoutParams)positiveButton.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
yourView.setLayoutParams(layoutParams);
Lucian Novac
  • 1,255
  • 12
  • 18
0
public void MoveView() {

        float a = parent_view.getWidth() / 2;
        final ValueAnimator positionAnimator =
                ValueAnimator.ofFloat(current_view.getX(), a);
        positionAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
        positionAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float x = (Float) positionAnimator.getAnimatedValue();
                current_view.setX(x);
            }
        });

        positionAnimator.setDuration(100);
        positionAnimator.start();
    }

parent_view is the view in whose center you would like to place your current_view.

Hope that Helps.

Akshay Sahai
  • 2,121
  • 1
  • 17
  • 20