user2401554
user2401554

Reputation: 121

How to change imageview margins dynamically based on button click in Android

I am trying to change imageview position based on button click event, I tried using below code, if click button1 ic_lancher image is showing one position, and again I click button2 here I just changed left margin, but image is not moving, but in button2 on click if gave different background means that time it's moving perfectly, if I give same background in both button on clicks, just changing position of image means its not working good, first where image displayed if I click another buttons also image is not moving, it's present in same position. How to solve this?

public class MainActivity extends Activity {
Button b1,b2;
ImageView arrow;
float screenHeight,screenWidth,screendensity;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
      requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        DisplayMetrics displaymetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
         screenHeight = displaymetrics.heightPixels;
         screenWidth = displaymetrics.widthPixels;
    setContentView(R.layout.activity_main);

    b1=(Button)findViewById(R.id.button1);
    b1.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            // TODO Auto-generated method stub
              arrow=(ImageView)findViewById(R.id.imageView1);
              arrow.setBackgroundResource(R.drawable.ic_launcher);
              arrow.setVisibility(View.VISIBLE);
              RelativeLayout.LayoutParams layoutarrow = (RelativeLayout.LayoutParams)arrow.getLayoutParams();    
              layoutarrow.leftMargin= (int)(120*(screenWidth/1024));
              layoutarrow.bottomMargin= (int)(235*(screenHeight/600));
              layoutarrow.width= (int)(50*(screenWidth/1024));
              layoutarrow.height= (int)(20*(screenHeight/600));
        }
    });

    b2=(Button)findViewById(R.id.button2);
    b2.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            // TODO Auto-generated method stub


            arrow=(ImageView)findViewById(R.id.imageView1);
              arrow.setBackgroundResource(R.drawable.ic_launcher);
              arrow.setVisibility(View.VISIBLE);
              RelativeLayout.LayoutParams layoutarrow = (RelativeLayout.LayoutParams)arrow.getLayoutParams();    
              layoutarrow.leftMargin= (int)(500*(screenWidth/1024));
              layoutarrow.bottomMargin= (int)(235*(screenHeight/600));
              layoutarrow.width= (int)(50*(screenWidth/1024));
              layoutarrow.height= (int)(20*(screenHeight/600));
        }
    });
}
}

Upvotes: 0

Views: 650

Answers (2)

T_V
T_V

Reputation: 17580

Try with this-

b2.setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {
        // TODO Auto-generated method stub


        arrow=(ImageView)findViewById(R.id.imageView1);
          arrow.setBackgroundResource(R.drawable.ic_launcher);
          arrow.setVisibility(View.VISIBLE);


          RelativeLayout.LayoutParams layoutarrow = 
                            new RelativeLayout.LayoutParams((int)(50*(screenWidth/1024)),(int)(20*(screenHeight/600));
                    layoutarrow.setMargins((int)(500*(screenWidth/1024)), 0, 0,(int)(235*(screenHeight/600)));

                    arrow.setLayoutParams(layoutarrow);
    }
});

Upvotes: 1

Chintan Rathod
Chintan Rathod

Reputation: 26034

You need to add lines below your code in both onClick method.

arrow.setLayoutParams(layoutarrow);

This would be like

public void onClick(View arg0) {
     // TODO Auto-generated method stub
    arrow=(ImageView)findViewById(R.id.imageView1);
    arrow.setBackgroundResource(R.drawable.ic_launcher);
    arrow.setVisibility(View.VISIBLE);
    RelativeLayout.LayoutParams layoutarrow = (RelativeLayout.LayoutParams)arrow.getLayoutParams();    
    layoutarrow.leftMargin= (int)(120*(screenWidth/1024));
    layoutarrow.bottomMargin= (int)(235*(screenHeight/600));
    layoutarrow.width= (int)(50*(screenWidth/1024));
    layoutarrow.height= (int)(20*(screenHeight/600));
    arrow.setLayoutParams(layoutarrow);
}

Upvotes: 0

Related Questions