ERJAN
ERJAN

Reputation: 24500

How to change layout:alignEnd, alignComponent attributes programatically

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.

Upvotes: 0

Views: 1128

Answers (1)

Kushal Sharma
Kushal Sharma

Reputation: 5973

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);

Upvotes: 1

Related Questions