This video shows the code to resize the ImageView by setting its layout parameters (width and height) to match parent. In this method it toggles the size between the match_parent to the initial size set by the user. In this example it uses Constraint Layout, however, same concept can be used for other layouts such as Frame Layout, Relative Layout, etc.
I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: programmerworld1990@gmail.com
Complete source code and other details:
package com.programmerworld.resizeimageview;
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
private int height, width;
private boolean booleanMATCH_PARENT = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
}
public void OnClickImageView(View view){
if (booleanMATCH_PARENT){
booleanMATCH_PARENT = false;
imageView.setLayoutParams(new ConstraintLayout.LayoutParams(width, height));
}else {
height = imageView.getHeight();
width = imageView.getWidth();
booleanMATCH_PARENT = true;
imageView.setLayoutParams(new ConstraintLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="212dp"
android:layout_height="165dp"
android:onClick="OnClickImageView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/flower_foreground" />
</androidx.constraintlayout.widget.ConstraintLayout>
Screenshots:
Excerpt:
The content presents a tutorial on how to modify the size of an ImageView by setting its layout parameters to match the parent. This method toggles the size between the match_parent and the initial size set by the user. The lesson uses Constraint Layout as an example but can be applied to other layouts like Frame Layout, Relative Layout, etc. The provided source code is written in the Java programming language and is used in context with Android’s AppCompatActivity. The code demonstration is accessible via a video link, with additional support available through a contact link and email.