rcode
rcode

Reputation: 49

Java Android Change Position of an ImageView dynamically

I am new in program Java/Android and got a small problem. I searched why my code doesn't work but I didn't find an answer - so here is my Problem:

I want to change the position of an ImageView at runtime by a Button. I have two buttons - left and right. But the curious thing is - only one is working (the button for moving right). If I click the button for moving left nothing changed.

I hope someone can help me because I searched a long time for this detail.

Tanks a lot for any help!

Here is the Code:

package de.androidnewcomer.bildattribute;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button leftButton=(Button)findViewById(R.id.buttonLeft);
    leftButton.setOnClickListener(this);
    Button rightButton=(Button)findViewById(R.id.buttonRight);
    rightButton.setOnClickListener(this);

}
@Override
public void onClick(View v) {
    switch(v.getId()) {
        case R.id.buttonLeft:
            moveLeft();
        case R.id.buttonRight:
            moveRight();
    }
}
private void moveLeft() {
    ImageView ball=(ImageView)findViewById(R.id.ball);
    FrameLayout.LayoutParams paramsLeft=(FrameLayout.LayoutParams)ball.getLayoutParams();
    paramsLeft.width=50;
    paramsLeft.height=50;
    paramsLeft.rightMargin +=20;
    ball.setLayoutParams(paramsLeft);
}
private void moveRight() {
    ImageView ball=(ImageView)findViewById(R.id.ball);
    FrameLayout.LayoutParams paramsRight=(FrameLayout.LayoutParams) ball.getLayoutParams();
    paramsRight.width=50;
    paramsRight.height=50;
    paramsRight.leftMargin +=20;
    ball.setLayoutParams(paramsRight);
}
}

And the XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="de.androidnewcomer.bildattribute.MainActivity">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:id="@+id/layout1">

    <Button
        android:text="Left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/buttonLeft"
        android:layout_gravity="bottom|left"
        android:layout_marginLeft="50dp" />

    <Button
        android:text="Right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/buttonRight"
        android:layout_gravity="bottom|right"
        tools:layout_marginRight="50dp" />

    <ImageView
        app:srcCompat="@drawable/image1"
        android:id="@+id/image1"
        android:layout_gravity="center_vertical|center_horizontal"
        android:layout_height="20dp"
        android:layout_width="20dp" />

</FrameLayout>

Upvotes: 2

Views: 1215

Answers (2)

Juan Martinez
Juan Martinez

Reputation: 770

Even though you need to modify you onClick method as it has been said, your moveLeft method should update the left margin (just as moveRight)

private void moveLeft() {
    ImageView ball=(ImageView)findViewById(R.id.ball);
    FrameLayout.LayoutParams paramsLeft=(FrameLayout.LayoutParams)ball.getLayoutParams();
    paramsLeft.width=50;
    paramsLeft.height=50;
    paramsLeft.leftMargin -=20;
    ball.setLayoutParams(paramsLeft);
}

You can experiment on your layout and see what happens if you add both left and right margin to an element, surely it is not what you expect.

Upvotes: 1

Doron Yakovlev Golani
Doron Yakovlev Golani

Reputation: 5470

The problem is in your onClick() - The break; statements are missing:

@Override
public void onClick(View v) {
    switch(v.getId()) {
        case R.id.buttonLeft:
            moveLeft();
            break; // MISSING
        case R.id.buttonRight:
            moveRight();
            break; // MISSING
    }
}

Without the break statement , when you hit buttonLeft, you perform both moveLeft() & moveRight() both , which result in no change in the layout.

Upvotes: 1

Related Questions