-2

A few ImageViews have been already created in xml eg:

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/blue_dot1"
        android:src="@drawable/checkers_blue1"
        android:layout_alignBottom="@+id/y1"
        android:layout_alignLeft="@+id/x1"
        android:layout_alignStart="@+id/x1"/>

Is it possible to update the xml later on programatically in Java for the alignbottom, left and start variables (so that the blue_dot1 image can be moved along the x/y grid of images)

For example I am going to make a method which has a X and Y arg to allow movement in a simple grid, its just the xml part i cant figure out.

user3652876
  • 3
  • 1
  • 5

3 Answers3

1

yes this is very much possible. but you are not modifying the XML instead you are modifying properties of a specific view:

check this question

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);

button.setLayoutParams(params); //causes layout update
Community
  • 1
  • 1
Rahul Tiwari
  • 6,851
  • 3
  • 49
  • 78
0

You can not change the xml-file. But you can set LayoutParams of your view programatically.

    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
    params.removeRule(RelativeLayout.ALIGN_BOTTOM);
AlbAtNf
  • 3,859
  • 3
  • 25
  • 29
0

No, modifying the XML is not possible to do from Java code. However, you can modify the Views layout properties programatically even after the XML properties have been declared and the view has been drawn. Its fairly simple to do. You just get a reference to that image view:

ImageView myImg = (ImageView)findViewById(R.id.myImg);
myImg.setLayoutParams([Your params here]);
Devsil
  • 598
  • 3
  • 16