1
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ImageView img=new ImageView(this);
img.setImageResource(R.drawable.img);
img.setScaleType(ImageView.ScaleType(CENTER_CROP));
setContentView(img) }

I want to set the size of image view in Java and I could not find the syntax working.

2
  • Why can't you use a regular layout file? Commented Jan 9, 2017 at 9:03
  • see my code and u can add height and width also. Commented Jan 9, 2017 at 11:26

3 Answers 3

2

Try code(edit as per your view names):

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LinearLayout picLL = new LinearLayout(this);
    picLL.layout(0, 0, 100, 100);
    picLL.setLayoutParams(new LayoutParams(100, 100));
    picLL.setOrientation(LinearLayout.HORIZONTAL);
    ImageView myImage = new ImageView(this);
    myImage.setImageResource(R.drawable.ic_launcher);
    picLL.addView(myImage);
    setContentView(picLL);
}

And set height or width:

myImageView..getLayoutParams().height = your_size_value;
myImageView..getLayoutParams().width = your_size_value;
Sign up to request clarification or add additional context in comments.

Comments

0
 <ImageView
    android:id="@+id/imageView1"
    android:layout_margin="20dp"
    android:src="@drawable/stop" 
    android:layout_width="50dp"
    android:layout_height="50dp"/> 

change width and height attribute with your values.

5 Comments

I must say, the question says to create ImageView using java not using XML. Try to update your question.
ImageView image = (ImageView) findViewById(R.id.test_image); Bitmap bMap = BitmapFactory.decodeFile("/sdcard/test2.png"); image.setImageBitmap(bMap);
If we need to resize a Bitmap, we can call the createScaledBitmap method to resize any bitmap to our desired width and height:
// Load a bitmap from the drawable folder Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.my_image); // Resize the bitmap to 150x100 (width x height) Bitmap bMapScaled = Bitmap.createScaledBitmap(bMap, 150, 100, true); // Loads the resized Bitmap into an ImageView ImageView image = (ImageView) findViewById(R.id.test_image); image.setImageBitmap(bMapScaled);
@KpAbhijith: if you have more code to offer that is relevant to the question, please put it in your answer in a formatted block. It is nearly unreadable in comments, and may be deleted there. Once you have updated the question, please delete the comments to keep things tidy. Thank you.
0

//Linear layout setting here

LinearLayout linearLayout= new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.bull);
imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 
LayoutParams.WRAP_CONTENT));
linearLayout.addView(imageView);
setContentView(linearLayout);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.