beginner beginner - 6 months ago 126
Android Question

How to resize an ImageView Programmatically to match parent in CardView

I want to programmaticlly put image in existing imageView.
This is xml file where i have imageView and TextView:



<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="10dp"
card_view:cardCornerRadius="4dp">

<!--android:background="?android:selectableItemBackground" nam daje click efekt-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/slika_id"
android:layout_width="match_parent"
android:layout_height="150dp"
android:src="@drawable/bird_corella"
android:layout_centerHorizontal="true"
android:scaleType="fitXY"/>
<TextView
android:id="@+id/zivotinjaIme_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/slika_id"
android:gravity="center_horizontal"
android:textStyle="bold"
android:text="Test"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:background="@android:color/holo_blue_bright"
android:textColor="@android:color/black"
android:textSize="18sp"/>

</RelativeLayout>

</android.support.v7.widget.CardView>





Here is code from main_activity how i want to change it and its not working:



ImageView imageView = (ImageView) findViewById(R.id.slika_id);

Display display = getWindowManager().getDefaultDisplay();
int height =((display.getHeight()*20)/100); //30% of the screan
CardView.LayoutParams parms = new CardView.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,height);
imageView.setLayoutParams(parms);





When i use this code without cardView its working with little eddit:
Here is xml code with only imageView, and one LinearLayout:



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.imagesize.MainActivity">


<ImageView
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="150dp"
android:id="@+id/slikazivotinje"
android:src="@drawable/bird_corella"
android:scaleType="fitXY"
/>


</LinearLayout>





This is activity_main.java where i change programmaticlly image size successfully:

ImageView imageView = (ImageView) findViewById(R.id.slikazivotinje);

Display display = getWindowManager().getDefaultDisplay();
int height =((display.getHeight()*20)/100); //30% of the screan
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,height);
imageView.setLayoutParams(parms);




ImageView imageView = (ImageView) findViewById(R.id.slikazivotinje);

Display display = getWindowManager().getDefaultDisplay();
int height =((display.getHeight()*20)/100); //30% of the screan
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,height);
imageView.setLayoutParams(parms);





I think problem is someting with parent layers, in CardView i have

<android.support.v7.widget.CardView>
<RelativeLayout>
<ImageView />
<TextView/>
</RelativeLayout>
</android.support.v7.widget.CardView>

Anyone knows how to work this out?


Here is logCat:



FATAL EXCEPTION: main
Process: com.example.android.imagesize, PID: 23919
java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
at android.widget.RelativeLayout$DependencyGraph.findRoots(RelativeLayout.java:1755)
at android.widget.RelativeLayout$DependencyGraph.getSortedViews(RelativeLayout.java:1700)
at android.widget.RelativeLayout.sortChildren(RelativeLayout.java:381)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:389)
at android.view.View.measure(View.java:18796)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.CardView.onMeasure(CardView.java:209)
at android.view.View.measure(View.java:18796)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:18796)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)
at android.view.View.measure(View.java:18796)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18796)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1458)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:746)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:18796)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
at android.view.View.measure(View.java:18796)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2108)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1224)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1460)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1115)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6023)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5422)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
06-02 17:26:46.363 23919-23919/? I/Process: Sending signal. PID: 23919 SIG: 9




Answer

Your imageView is hosted in a RelativeLayout, not a Cardview. Change this

CardView.LayoutParams parms = new CardView.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,height);
    imageView.setLayoutParams(parms);

to this

ViewGroup.LayoutParams params = imageView.getLayoutParams();
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
params.height = height;
imageView.setLayoutParams(params);