1

i have a view in relativeleayout , I want to change the place of the view to the middle of relativeleayout: what I do ?

the xml :

<RelativeLayout
    android:id="@+id/partieoption"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="6"
    android:orientation="horizontal" >

   <ImageView 
       android:id="@+id/photo2"
       android:src="@drawable/loglog"
       android:layout_width="50dp"
       android:layout_height="50dp"
   />
</RelativeLayout>

and

RelativeLayout partieoption = (RelativeLayout) findViewById(R.id.partieoption);
ImageView x = (ImageView) findViewById(R.id.photo2);
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
stevGates
  • 910
  • 2
  • 9
  • 19

4 Answers4

1

You'll need to create a RelativeLayout.LayoutParams for your ImageView with a CENTER_IN_PARENT rule, then apply it with setLayoutParams(ViewGroup.LayoutParams params).

Here's an example:

    ImageView imageView = (ImageView)findViewById(R.id.iv_image);
    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)imageView.getLayoutParams();
    layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
    imageView.setLayoutParams(layoutParams);
Rany Albeg Wein
  • 3,304
  • 3
  • 16
  • 26
0
 <ImageView 
                        android:id="@+id/photo2"
                        android:src="@drawable/loglog"
                        android:layout_width="50dp"
                        android:layout_height="50dp"
                        android:gravity="center"
                        android:layout_gravity="center"
 />
Shubham Chauhan
  • 929
  • 2
  • 7
  • 18
0

For moving the view, add this code

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
RelativeLayout partieoption = (RelativeLayout) findViewById(R.id.partieoption);
      ImageView iv = (ImageView) findViewById(R.id.photo2);
ObjectAnimator transAnimation= ObjectAnimator.ofFloat(iv, x, iv.getX(), width/2);
  transAnimation.setDuration(3000);
  transAnimation.start();
Nilesh Tiwari
  • 194
  • 2
  • 7
0

use the gravity attribute in your xml, like this:

android:layout_gravity="center"

in your case, i think will be like this:

<RelativeLayout
android:id="@+id/partieoption"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="6"
android:orientation="horizontal" >

<ImageView 
   android:id="@+id/photo2"
   android:src="@drawable/loglog"
   android:layout_width="50dp"
   android:layout_height="50dp"
   android:layout_gravity="center"
/>
</RelativeLayout>
Ricardo A.
  • 685
  • 2
  • 8
  • 35