-2

I want to to have the height of an ImageView equal to it's width. I already read something about using "app:..." in activity.xml. But, it says that it couldn't find the namespace. After that, I just found a code which didn't worked for me.

Does anybody has ideas or suggestions to fix this issue? If it has to be done programmatically and it would be good if has an example for this using Kotlin.

Dino
  • 7,779
  • 12
  • 46
  • 85
Schmidi
  • 3
  • 5

2 Answers2

0

If you want to have the height of an ImageView equal to it's width,

Programmatically [Dynamic way]:

int widthOfImage = image_view.getLayoutParams().width;
image_view.getLayoutParams().height = widthOfImage ;

refer to this answer

you can do it in Hard-coded way:

  <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/ic_launcher_background"/>

or you can define background like this image_background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#003778" />
<corners android:radius="6dp" />
<size android:width="100dp"
    android:height="100dp"/>
</shape>

and add it as background in imageView. [remember it is also hard-coded]

Makarand
  • 983
  • 9
  • 27
0

You have to use this custome view inside you xml file.

Example

<YourPackageName.SquareImageView
    android:id="@+id/squareImageview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/ic_launcher_background"/>

Now you have to bind this view in you code. Like Fragment or Activity whatever you have used.

Exmple (This is code in java you can change it in kotlin) :-

SquareImageView imageView = findViewById(R.id. squareImageview);

Prayag Gediya
  • 1,078
  • 2
  • 11
  • 20