Aya Aboud
Aya Aboud

Reputation: 379

how to resize an ImageView Programmatically?

I'm beginner in android and i have problem in resize image. size of image change every time(check that by line of print), but that not appear in actual image.

my code:

   import android.os.Bundle;
    import android.app.Activity;
    import android.content.DialogInterface.OnClickListener;
    import android.view.Menu;
    import android.view.View;
    import android.view.animation.*;
    import android.widget.Button;
    import android.widget.ImageView;

    public class MainActivity extends Activity {

        public static int state = 2;
        public static ImageView image;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            image = (ImageView) findViewById(R.id.imageView1);

            final Button button2 = (Button) findViewById(R.id.button2);
            button2.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {

                    System.out.println("1- "+image.getLayoutParams().height+", "+image.getLayoutParams().width);
                    image.getLayoutParams().height += 20;
                    image.getLayoutParams().width += 20;
                    System.out.println("2- "+image.getLayoutParams().height+", "+image.getLayoutParams().width);
                }
            });
        }
@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    }

Upvotes: 1

Views: 3074

Answers (1)

romtsn
romtsn

Reputation: 12002

You need to notify UI that your view bounds have changed. Just add this line image.requestLayout(); below you println method and it should work:

       button2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                System.out.println("1- "+image.getLayoutParams().height+", "+image.getLayoutParams().width);
                image.getLayoutParams().height += 20;
                image.getLayoutParams().width += 20;
                System.out.println("2- "+image.getLayoutParams().height+", "+image.getLayoutParams().width);
                image.requestLayout();
            }
        });

Upvotes: 3

Related Questions