1

I have an ImageView in my app. My question is how I can set params to it?

ImageView header = new ImageView(getActivity());

I used this way but I face with classCastException

ImageView header = new ImageView(getActivity());
header.setImageResource(R.drawable.ahsan_hadis_img);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(200, 1500);
header.setLayoutParams(layoutParams);
Sufian
  • 6,405
  • 16
  • 66
  • 120
Erfan Eghterafi
  • 4,344
  • 1
  • 33
  • 44

2 Answers2

1

Try adding this in your XML file:

<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/linearLayout"
    android:orientation="vertical">

</LinearLayout>

Add this in your JAVA file

LinearLayout.LayoutParams params =
                    new LinearLayout.LayoutParams(
                            android.widget.LinearLayout.LayoutParams.WRAP_CONTENT,
                            android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);

ImageView subImage = new ImageView(this);
subImage.setLayoutParams(params);              
layout.addView(subImage);
Sufian
  • 6,405
  • 16
  • 66
  • 120
Dilavar Malek
  • 1,157
  • 11
  • 18
  • It will work if I have imageView in xml file but my problem is there is no xml file for this ImageView. In fact I create it in .java file – Erfan Eghterafi May 28 '15 at 08:04
0

ClassCastException might occur if you are adding your ImageView to a layout other than LinearLayout. Choose your LayourParams class according to the ImageView's parent.

Andras Balázs Lajtha
  • 2,576
  • 25
  • 32