0

I have 2 images, one stands behind another and is a yellow background, that stands out by 20dp, it is a frame. The front image is main image.

On click, i want main image to appear in the center of "yellow frame".

<ImageView

    android:id="@+id/yellow_bkg"
    android:background="#ffe467"
    android:layout_centerVertical="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
     />

<ImageView    
    android:id="@+id/card"
    android:background="@drawable/card"
    android:layout_alignRight="@+id/border"   //how to change this in code?
    android:layout_alignEnd="@+id/border"//how to change this in code?
    android:layout_margin="20dp" />

But the effect should happen in code, i.e. dynamically when i do click or touch on card imageView.

ERJAN
  • 23,696
  • 23
  • 72
  • 146

1 Answers1

1

You can use LayoutParams to do that!

ImageView card = (ImageView) findViewById(R.id.card);

Since you have

android:layout_alignRight="@+id/border"
android:layout_alignEnd="@+id/border"

I assume you are inside a RelativeLayout

RelativeLayout.LayoutParams layoutParams;
layoutParams = new RelativeLayout.LayoutParams(width, height);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);

and inside your image view onClickListner

card.setLayoutParams(layoutParams);
Kushal Sharma
  • 5,978
  • 5
  • 25
  • 41
  • @Sharma, ok, but when i create new relative layout params, my imageView is already inside another relativeLayout, if i do card.setLayoutParams, how does it affect the actual relative layout my imageView is sitting in? – ERJAN Sep 23 '15 at 05:59
  • It does not affect your actual relative layout. Actually the kind of attributes that can be set on a view is related to it's parent view. You would want to use LayoutParams of the same type as your parent view. So that you can use those attributes you require.. like in your case you want to use CENTER_IN_PARENT – Kushal Sharma Sep 23 '15 at 06:10