Denys Karpov
Denys Karpov

Reputation: 43

how change position of element in .xml file from java code

how change position of element in .xml file from java code for example i need change position after some event

view.setX
view.setY

Perhaps for briefly example

Upvotes: 0

Views: 1835

Answers (1)

Daniel S. Fowler
Daniel S. Fowler

Reputation: 2033

If a button is on an AbsoluteLayout this code will randomly move it when the button is clicked.

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsoluteLayout;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener{
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.button1).setOnClickListener(this);
      }
      public void onClick(View arg0) {
        Button btn = (Button)arg0;
        AbsoluteLayout.LayoutParams params = ((AbsoluteLayout.LayoutParams) btn.getLayoutParams());
        params.x = (int)(Math.random()*200);
        params.y = (int)(Math.random()*200);
        btn.setLayoutParams(params); 
      }
}

Upvotes: 1

Related Questions